This article will describe how you can send emails pragmatically using the Java programming language. The example below includes a function you can use for easily sending emails from your Java application.
Prerequisites
If you have not already, be sure you download and install the latest Java Development Kit (JDK). You can find the download here.
unfortunately the javax.mail libraries are not included in the Java JDK, so you will have to download them separately.
The java mail library can be downloaded from the following location: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR
Once you have downloaded the java mail library, you will need to add the mail.jar file to your Java Build Path. mail.jar is included in the zip file you downloaded from the Oracle page above.
Assuming you are using Eclipse, you can do the following to add the library to your project:
- Extract the zip file you downloaded
- Open your project in Eclipse
- Right click on your project name in the Package Explorer pane
- Select Build Path -> Configure Build Path
- Select the Libraries Tab
- Click Add External JAR’s
- Browse to the Mail.Jar you extracted in Step 1
- Click Open
- Click Ok
Writing Code
Now that we have all of the prerequisites installed, we can start looking at our code. Below is an example application which sends an email using the javax.mail library. This application sends through an SMTP relay that requires TLS and authentication. Some mail relays do not require either of these things. You will probably want to add some additional code to make that configurable. In this application, it is hard code to use authentication and TLS.
We have added comments throughout the code to help explain each section.
//Import the required libraries for the send email function import javax.mail.*; import javax.mail.internet.*; import java.util.*; //Call the function to send your email. This is pre-populated for using Gmail as your SMTP server. sendEmail("smtp.gmail.com", //SMTP Server Address "587", //SMTP Port Number "true", //Enable Authorization "true", //Enable TLS "<YourAddress>@gmail.com", //Your SMTP Username "<YourPassword>", //Your SMTP Password "<From Address>", //Sender Address "<To Address>", //Recipient Address "<Subject>", //Message Subject "<Body>"); //Message Body //Below is the function for sending the email static void sendEmail( String smtpAddress, String smtpPort, String enableTLS, String enableAuth, final String username, final String password, String fromAddress, String toAddress, String mySubject, String myMessage){ Properties props = new Properties(); props.put("mail.smtp.starttls.enable", enableTLS); props.put("mail.smtp.auth", enableAuth); props.put("mail.smtp.host", smtpAddress); props.put("mail.smtp.port", smtpPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddress)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress)); message.setSubject(mySubject); message.setText(myMessage); Transport.send(message); System.out.print("Sent"); } catch (Exception e) { System.out.print(e); } }
Summary
At this point you have installed the Java JDK, the Java.mail programming library, and written a small application which can send email messages through an SMTP server that requires authentication and TLS. And you have a function you can call to make it easy to re-use the code.