Real'sHowTo AddThis Feed Button
Custom Search

Send email with SMTPS (eg. Google GMail) (Javamail)Tag(s): Networking

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(""));


The property
props.put("mail.smtps.quitwait", "false");
is required to get rid of a strange SSL exception :
javax.mail.MessagingException: Exception reading response;
nested exception is: Unsupported record version Unknown-50.49
The mail is sent but the exception is unwanted...

The property quitwait means

If set to false, the QUIT command is sent and the connection 
is immediately closed. If set to true (the default), causes 
the transport to wait for the response to the QUIT command.
ref :

This setting, mail.smtps.quitwait, is not required anymore! (july2007)

Settings for well known mail providers

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

blog comments powered by Disqus

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]