mail
Sending emails with JavaMail
Sending a simple text message
// 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();
}
Alternatively, instead using:
msg.setText("This is the text for this simple demo using JavaMail.");
you can use next to set the message content:
msg.setContent("This is the text for this simple demo using JavaMail.", "text/plain");
Checking an email address
Here is a little trick to check, using a regular expression, if an email address is well formed:
Pattern rfc2822 = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
if(rfc2822.matcher(EMAIL_ADDRESS).matches()) {
// Well formed email
}
Multipart messages
...
// 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 messages
...
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1>Sample</h1><p>This is a sample HTML part</p>", "text/html");
...
Attaching images within the HTML code
...
// 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);
...
Related Article:
Reference: Sending emails with Java from our JCG partner Antonio Santiago at the “A Curious Animal” Blog
