Gmail JavaSending 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.

Example Code

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.  
  1. package com.infuze.service.test;
  2. import java.util.Properties;
  3. import javax.mail.Message;
  4. import javax.mail.MessagingException;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10. import com.livrona.comono.logger.Logger;
  11. import com.livrona.comono.logger.LoggerFactory;
  12. public class EmailerTest {
  13.    private static final Logger log = LoggerFactory.createLogger(EmailerTest.class);
  14.     public static void main(String[] args) {
  15.         sendViaSSL();
  16.         sendViaTLS();
  17.      }
  18.    public static void sendViaSSL() {
  19.        // properties for Email host/port ssl
  20.        Properties props = new Properties();
  21.         props.put("mail.smtp.host", "smtp.gmail.com");
  22.         props.put("mail.smtp.socketFactory.class",
  23.             "javax.net.ssl.SSLSocketFactory");
  24.         props.put("mail.smtp.port", "465");
  25.         props.put("mail.smtp.socketFactory.port", "465");
  26.         //auth true
  27.         props.put("mail.smtp.auth", "true");
  28.         // create the session with auth
  29.         Session session = Session.getDefaultInstance(props, new
  30.         javax.mail.Authenticator()
  31.     {
  32.                 protected PasswordAuthentication
  33.                  getPasswordAuthentication() {
  34.         return new PasswordAuthentication("xxx@gmail.com", "the password");
  35.         }
  36.       });
  37.         try {
  38.         // create the message
  39.         Message message = new MimeMessage(session);
  40.         message.setFrom(new InternetAddress("xxx@gmail.com"));
  41.         message.setRecipients(Message.RecipientType.TO,
  42.            InternetAddress.parse("yyy@somedomain.com"));
  43.         message.setSubject("Test SSL");
  44.         message.setText("This is SSL test");
  45.         // send it
  46.         Transport.send(message);
  47.         log.debug("SSL Email Sent");
  48.         } catch (MessagingException e) {
  49.          log.error("Failed to send Email : " + e.getMessage(), e);
  50.          }
  51.         }
  52.         public static void sendViaTLS() {
  53.         Properties props = new Properties();
  54.         props.put("mail.smtp.auth", "true");
  55.         props.put("mail.smtp.starttls.enable", "true");
  56.         String host = "smtp.gmail.com";
  57.         int port = 587;
  58.         String username = "xxx@gmail.com";
  59.         String password = "the password";
  60.         // create the session
  61.         Session session = Session.getDefaultInstance(props, new
  62.            javax.mail.Authenticator()
  63.       {
  64.         protected PasswordAuthentication getPasswordAuthentication() {
  65.         return new PasswordAuthentication("xxx@gmail.com", "the password");
  66.         }
  67.         });
  68.         try {
  69.         // create the message
  70.         Message message = new MimeMessage(session);
  71.         message.setFrom(new InternetAddress("xxx@gmail.com"));
  72.         message.setRecipients(Message.RecipientType.TO,
  73.             InternetAddress.parse("yyy@somedomain.com"));
  74.         message.setSubject("Test TLS");
  75.         message.setText("This is TLS test");
  76.         // create the transport
  77.         Transport transport = session.getTransport("smtp");
  78.         transport.connect(host, port, username, password);
  79.         // sent it
  80.         Transport.send(message);
  81.         log.debug("TLS Email Sent");
  82.         } catch (MessagingException e) {
  83.                 log.error("Failed to send Email : " + e.getMessage(), e);
  84.                 }
  85.         }
  86. }