title: Java Send E-Mail Using Gmail SMTP
body: <p>Trying to send an email using my gmail account like bellow -</p>
  <pre><code>import org.junit.jupiter.api.Test;
  import java.util.*;
  import javax.mail.*;
  import javax.mail.internet.*;
  
  public class EMailTest {
  
      @Test
      public void sendEmail() {
          
          String to = &quot;to-email@markany.com&quot;;
          
          String from = &quot;from-email@gmail.com&quot;;
          
          String host = &quot;smtp.gmail.com&quot;;
          
          Properties properties = System.getProperties();
          properties.put(&quot;mail.smtp.host&quot;, host);
          properties.put(&quot;mail.smtp.port&quot;, &quot;465&quot;);
          properties.put(&quot;mail.smtp.ssl.enable&quot;, &quot;true&quot;);
          properties.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
          
          Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(from, &quot;from-email-password&quot;);
              }
          });
          session.setDebug(true);
  
          try {
              MimeMessage message = new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
              message.setSubject(&quot;This is the Subject Line!&quot;);
              message.setText(&quot;This is actual message&quot;);
              
              System.out.println(&quot;sending...&quot;);
              
              Transport.send(message);
              
              System.out.println(&quot;Sent message successfully....&quot;);
          } catch (MessagingException mex) {
              mex.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>But it required turning on the <strong>&quot;Less secure app access&quot;</strong> option in sender/from email account setting.
  <strong>And this option is no longer supported by Gmail</strong> -
  <a href="https://support.google.com/accounts/answer/6010255?hl=en" rel="nofollow noreferrer">https://support.google.com/accounts/answer/6010255?hl=en</a></p>
  <p>So, how to send email now in Java using Gmail?</p>