Try this
Code:
public static synchronized void sendMail() {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = SelectedEmail;
String from = "fromAddress";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "Host IP";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiatee a message
Message msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Your Subject");
msg.setSentDate(new Date());
//for attachment
BodyPart messageBodyPart = new MimeBodyPart();
String body = "Body part here";
messageBodyPart.setText("any body text here");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(new File(path, fileName));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
//Send the message
Transport.send(msg);
} catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
Bookmarks