Sending E-mail

There are mainly three steps involved in sending a email over the network:

Get the Session object

  1. Set the required properties
  2. Get password authentication
// Set the required properties
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");

// Get the Session object on password authenticator
Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(userName, password);

Create default MimeMessage object and set the information

  1. Set from and to addresses
  2. Set subject of the email
  3. Set the content of the email
// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(fromAddr));

// Set To: header field of the header.

// Set Subject: header field
message.setSubject(subject+" - SimpleMailThruTLS");

// Now set the actual message

Send the message using Transport.send(message)

Footer with Map