Javamail example


Sending a simple text


// Common variables
String host = "your_smtp_server";
String from = "from_address";
String to = "to_address";

// Set properties
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");

// Get session
Session session = Session.getInstance(props);

try {
 // Instantiate a message
 Message msg = new MimeMessage(session);

 // Set the FROM message
 msg.setFrom(new InternetAddress(from));

 // The recipients can be more than one so we use an array but you can
 // use 'new InternetAddress(to)' for only one address.
 InternetAddress[] address = {new InternetAddress(to)};
 msg.setRecipients(Message.RecipientType.TO, address);

 // Set the message subject and date we sent it.
 msg.setSubject("Email from JavaMail test");
 msg.setSentDate(new Date());

 // Set message content
 msg.setText("This is the text for this simple demo using JavaMail.");

 // Send the message
 Transport.send(msg);
}
catch (MessagingException mex) {
 mex.printStackTrace();
}

One can use the following to set the content of the message and declare its Mime type instead of setting only the text:

 msg.setContent("This is the text for this simple demo using JavaMail.", "text/plain"); 

Multipart Messages

Sending messages that contain for example, HTML and rich text. Message composed of different parts.


...
// Here create two parts and set as message contect
// Create and fill first part
MimeBodyPart part1 = new MimeBodyPart();
part1.setText("This is part one of this multipart message.");

// Create and fill second part
MimeBodyPart part2 = new MimeBodyPart();
part2.setText("This is part two of this multipart message.");

// Create the Multipart.
Multipart mp = new MimeMultipart();
mp.addBodyPart(part1);
mp.addBodyPart(part2);

// Set the message's content
msg.setContent(mp);
...

Sending Attachments


...
// Create a new part for the attached file
MimeBodyPart part3 = new MimeBodyPart();

// Put a file in the second part
FileDataSource fds = new FileDataSource("THE_FILE_NAME");
part3.setDataHandler(new DataHandler(fds));
part3.setFileName(fds.getName());

// 'mp' is the previously created 'MimeMultipart' object
mp.addBodyPart(part3);

// 'msg' is the previously created 'Message' object
msg.setContent(mp);
...

HTML message

...
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1>Sample</h1><p>This is a sample HTML part</p>", "text/html");
...

Including Images in the HTML message

...
// Create and fill html part
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1>Sample</h1><p>This is a sample HTML part with an attached image</p>" +
    "<img src='cid:some_image_id'>", "text/html");

// Create a new part for the attached image and set the CID image identifier
MimeBodyPart imagePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("THE_IMAGE_FILE_NAME");
imagePart.setDataHandler(new DataHandler(fds));
imagePart.setHeader("Content-ID", "some_image_id");

mp.addBodyPart(htmlPart);
mp.addBodyPart(imagePart);
...


Excellent Alternative Mail API for Java
Apache Commons Email.
EVENTUAL ERRORS:
When sending an email to multiple email addresses, if one of those email addresses is invalid, the entire message might fail to be sent to anyone.
In order to fix this issue, set the system property mail.smtp.sendpartial to true.

Advertisements

One thought on “Javamail example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s