Sending email from a Gmail account can be done easily using the classes from the javax.mail package. Gmail supports both SSL and TLS as the protocol to make a secure connection from the email client to the email server. Both of these way of connecting to email server require that the user to be authenticated, hence these credentials need to be provided.


The below code provides implementation of sending email via a Gmail account both using SSL and TLS protocol, just change the username and password to that of yours account.


import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.livrona.comono.logger.Logger;
import com.livrona.comono.logger.LoggerFactory;

// Email Client using GMail
public class EmailerTest {
   private static final Logger log = LoggerFactory.createLogger(EmailerTest.class);

   // test 
   public static void main(String[] args) {
        sendViaSSL();
        sendViaTLS();
     }

  /**
   * Method to send email via SSL
   */
   public static void sendViaSSL() {

        // properties for Email host/port ssl
        Properties props = new Properties();
        props.put(“mail.smtp.host”, “smtp.gmail.com”);
        props.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
        props.put(“mail.smtp.port”, “465”);
        props.put(“mail.smtp.socketFactory.port”, “465”);
        //auth true
        props.put(“mail.smtp.auth”, “true”);

        // create the session with auth
        Session session = Session.getDefaultInstance(props, new
        javax.mail.Authenticator()  {
            protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(“xxx@gmail.com”, “the password”);
         }
      });

        try {

        // create the message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(“xxx@gmail.com”));
        
        message.setRecipients(Message.RecipientType.TO, 
           InternetAddress.parse(“yyy@somedomain.com”));
        
        message.setSubject(“Test SSL”);
        message.setText(“This is SSL test”);

        // send it
        Transport.send(message);
        log.debug(“SSL Email Sent”);

        } catch (MessagingException e) {
         log.error(“Failed to send Email : “ + e.getMessage(), e);
         }
     }

    // Send email using TLS
    public static void sendViaTLS() {

        Properties props = new Properties();
        props.put(“mail.smtp.auth”, “true”);
        props.put(“mail.smtp.starttls.enable”, “true”);
        String host = “smtp.gmail.com”;
        int port = 587;

        // set your username and password
        String username = “xxx@gmail.com”;
        String password = “the password”;

        // create the session
        Session session = Session.getDefaultInstance(props, new
           javax.mail.Authenticator()  {
               protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(“xxx@gmail.com”, “the password”);
             }
        });

        try {
        // create the message
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(“xxx@gmail.com”));

        message.setRecipients(Message.RecipientType.TO, 
          InternetAddress.parse(“yyy@somedomain.com”));

        message.setSubject(“Test TLS”);
        message.setText(“This is TLS test”);

        // create the transport
        Transport transport = session.getTransport(“smtp”);
        transport.connect(host, port, username, password);

        // sent it
        Transport.send(message);
        log.debug(“TLS Email Sent”);
     } 
     catch (MessagingException e) {
                log.error(“Failed to send Email : “ + e.getMessage(), e);
     }
    }
}