Monday, June 23, 2008
Basic JavaMail Code
Every time I have to use the JavaMail API I am reminded how much I don't like it. Could it be any harder to figure out how to send a simple email?
Here is an example of how to send an HTML email (minus the exception handling)...
In the end it looks fairly simple, but it took far too much time searching through the JavaMail docs to piece them together.
Here is an example of how to send an HTML email (minus the exception handling)...
Properties props = new Properties();
props.put("mail.smtp.host", "YOUR_HOST_NAME");
Session session = Session.getInstance(props, null);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("YOU@YOURHOST.COM"));
InternetAddress [] address = {new InternetAddress("PERSON@THEIRHOST.COM")};
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject("A Subject");
message.setSentDate(Calendar.getInstance().getTime());
message.setContent("<html><body>This is an HTML email.</body></html>", "text/html");
Transport.send(message);
In the end it looks fairly simple, but it took far too much time searching through the JavaMail docs to piece them together.