Sending an email Programmatically using Java

This post includes a function you can use for sending emails from your Java application.

One thing to note is 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:

  1. Extract the zip file you downloaded
  2. Open your project in Eclipse
  3. Right click on your project name in the Package Explorer pane
  4. Select Build Path -> Configure Build Path
  5. Select the Libraries Tab
  6. Click Add External JAR’s
  7. Browse to the Mail.Jar you extracted in Step 1
  8. Click Open
  9. Click Ok