How do I send an email in Java?
Author: Deron Eriksson
Description: This Java tutorial describes sending an email in Java.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
To test sending email, I'd like to keep things fairly simple and send an email to an email server on my local development machine. So, the first step is to acquire an email server for Windows XP. A quick Google search led me to download the free Standard Edition of MailEnable from http://www.mailenable.com/. I'm not a Windows administrator, but MailEnable was extremely easy for me to set up and use, which is definitely a plus. On my local network at home, I currently don't have a DNS server (I currently have about 8 computers hooked up to two Linksys routers, so maybe I should get one). Since I'm going to want to specify the SMTP (mailserver) host that i'd like to send mail to, I'm going to add an entry to my hosts file. For some strange reason, Microsoft buries this useful file (at least in XP) at C:\WINDOWS\system32\drivers\etc\hosts. This is similar to Oracle burying the tnsnames.ora file way down in the directory structure. Oh wait, I digress... In the hosts file, I added an entry for cakelycakes.com and pointed it to 127.0.0.1. What this means is that requests from my computer to cakelycakes.com will be sent to address 127.0.0.1, which is the local host. Notice that 'localhost' also goes to 127.0.0.1. I installed MailEnable Standard Edition, using cakelycakes.com as the host. I created a 'cakes' Post Office. My Domain Name is cakelycakes.com. I created a 'bigcakes' mailbox, a 'littlecakes' mailbox, and a Postmaster mailbox. Now that we have a local email server, let's write some JavaSW email code! This project has the following structure: This project utilizes the JavaMail API, which can be downloaded at http://java.sun.com/products/javamail/downloads/. The JavaMail API requires the JavaBeansW Activation Framework, which can be downloaded at http://java.sun.com/products/javabeans/jaf/downloads/index.html. I changed the names of mail.jar to mail-1.4.jar and activation.jar to activation-1.1.jar so that it's easier to keep track of the versions of the jarW libraries being used in the project. I added these jar files to the build path of my project. The project consists of one Java class, the EmailTest class, which is shown below. EmailTest.javapackage test; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailTest { public static void main(String[] args) { String mailSmtpHost = "cakelycakes.com"; String mailTo = "bigcakes@cakelycakes.com"; String mailCc = "littlecakes@cakelycakes.com"; String mailFrom = "me@here.there.everywhere"; String mailSubject = "Email from Java"; String mailText = "This is an email from Java"; sendEmail(mailTo, mailCc, mailFrom, mailSubject, mailText, mailSmtpHost); } public static void sendEmail(String to, String cc, String from, String subject, String text, String smtpHost) { try { Properties properties = new Properties(); properties.put("mail.smtp.host", smtpHost); Session emailSession = Session.getDefaultInstance(properties); Message emailMessage = new MimeMessage(emailSession); emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); emailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc)); emailMessage.setFrom(new InternetAddress(from)); emailMessage.setSubject(subject); emailMessage.setText(text); emailSession.setDebug(true); Transport.send(emailMessage); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } } The main method specifies the SMTP email server host to send the email to. This is the host that I specified in my hosts file and configured MailEnable to use. I specify the 'to' address, a 'cc' address, a 'from' address, the 'subject', and the 'text' (email body). An email 'Session' object is responsible for managing messages. Information such as the SMTP host is passed to the Session via a Properties object. A message object is created using the Session. In the EmailTest class, the 'to' recipient is set, the 'cc' recipient is set, the 'from' recipient is set, the 'subject' is set, and then the 'text' is set. Notice that the 'to' and 'cc' are both considered recipients. It is possible to include multiple 'to' and 'cc' addresses by adding more recipients. It's also possible to call the addRecipients method with an array of Addresses. To help us see what's going on, we can call the session object's setDebug() method with true as a parameter. This will output email debug information to standard output, although we can use another PrintStream to collect the debug information. Just in case you'd like to do this, it can be done in the following manner: ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); emailSession.setDebugOut(ps); ... Transport.send(emailMessage); // this is the code that actually sends the email message to the server ... System.out.println("DEBUG INFO: " + baos.toString()); In EmailTest, the call to the Transport.send() method with the email message as a parameter actually sends the message to the SMTP server. (Continued on page 2) Related Tutorials: |