Send email with SMTPS (eg. Google GMail)

1. Send email with SMTPS

It’s not uncommon that the outgoing mail needs to be encrypted using the SMTPS protocol. It’s the case for GMail for example. You need Javamail 1.4 to use the SMTPS protocol.

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;


public class SimpleSSLMail {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "myaccount@gmail.com";
    private static final String SMTP_AUTH_PWD  = "mypwd";

    public static void main(String[] args) throws Exception{
       new SimpleSSLMail().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("Testing SMTP-SSL");
        message.setContent("This is a test", "text/plain");

        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("maixuanviet.com@gmail.com"));

        transport.connect
          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
}

Even you send to correct credentials, it’s possible that you get the javax.mail.AuthenticationFailedException exception from Gmail. If it’s the case, you may need to explicitly enable “less secure apps” setting in your Gmail account, see https://support.google.com/accounts/answer/6010255>Answer from Google.

2. Settings for well known mail providers

Yahoo
– Incoming Mail Server – pop.mail.yahoo.com (POP3 – port 110)
– Outgoing Mail Server – smtp.mail.yahoo.com (SMPTP – port 25)
GMail
– Incoming Mail Server – pop.gmail.com (POP3S SSL enabled, port 995)
– Outgoing Mail Server – gmail.com (SMPTS SSL enabled, port 465)

Done! Happy Coding!

Related posts:

Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Check if it is a Sparse Matrix
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Xây dựng ứng dụng Client-Server với Socket trong Java
Abstract class và Interface trong Java
JUnit5 Programmatic Extension Registration with @RegisterExtension
Join and Split Arrays and Collections in Java
Base64 encoding và decoding trong Java 8
Java Program to Implement the One Time Pad Algorithm
Reversing a Linked List in Java
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Removing all Nulls from a List in Java
New Features in Java 13
Spring Boot - Enabling HTTPS
Java Program to Solve a Matching Problem for a Given Specific Case
Entity To DTO Conversion for a Spring REST API
Class Loaders in Java
How To Serialize and Deserialize Enums with Jackson
Spring Security – security none, filters none, access permitAll
A Guide to the finalize Method in Java
Spring MVC and the @ModelAttribute Annotation
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Hướng dẫn Java Design Pattern – DAO
Java Program to Find the GCD and LCM of two Numbers
Java Program to Implement Unrolled Linked List
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement the RSA Algorithm
Spring Boot - Exception Handling
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java 8 Stream API Analogies in Kotlin
Bootstrapping Hibernate 5 with Spring