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 = "";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "";
    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("", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

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

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

             new InternetAddress(""));



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>Answer from Google.

2. Settings for well known mail providers

– Incoming Mail Server – (POP3 – port 110)
– Outgoing Mail Server – (SMPTP – port 25)
– Incoming Mail Server – (POP3S SSL enabled, port 995)
– Outgoing Mail Server – (SMPTS SSL enabled, port 465)

Done! Happy Coding!

Related posts:

Java Program to Implement SimpeBindings API
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Tips for dealing with HTTP-related problems
Java – File to Reader
XML Serialization and Deserialization with Jackson
Intro to the Jackson ObjectMapper
Java Program to Implement the Monoalphabetic Cypher
Java Program to Implement Solovay Strassen Primality Test Algorithm
Comparing Dates in Java
Map Serialization and Deserialization with Jackson
Jackson vs Gson
Guide to Spring Cloud Kubernetes
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Send email with authentication
Java Program to Create the Prufer Code for a Tree
Tính đa hình (Polymorphism) trong Java
A Guide to LinkedHashMap in Java
Getting Started with Forms in Spring MVC
Serialize Only Fields that meet a Custom Criteria with Jackson
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Implement Binomial Heap
Custom Error Pages with Spring MVC
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Tổng quan về ngôn ngữ lập trình java
Java Program to Implement Patricia Trie
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Introduction to Java Serialization
Read an Outlook MSG file
Java Program to Implement Dijkstra’s Algorithm using Priority Queue