Sending Email using javax.mail API

1 Flares Twitter 0 Facebook 1 Google+ 0 LinkedIn 0 Filament.io 1 Flares ×

The JavaMail standard extension in package javax.mail and javax.mail.internet (the latter contains classes that are specific to Internet email protocols) provides classes to send emails. Let us consider the example of how to send mail using the SMTP protocol.

Example : Send Mail using SMTP

  1. Create a java.util.Properties object to pass information about the mail server
  2. Load the Properties with hostname of the SMTP mail server, you can pass authentication information if the SMTP requires those.
  3. Create an email session object.
  4. Construct the Message from the Session object.
  5. Provide from, to and Subject in the Message.
  6. Set message text to the message body.
  7. Use Transport.send( ) to send the message

You can set CC, BCC etc also using the javax.mail.* API.