TLS issue when sending to gmail through JavaMail

27,153

Solution 1

You need to enable STARTTLS. Add one more property to your configuration:

props.put("mail.smtp.starttls.enable", "true");

Solution 2

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class CopyOfSendMail {

private static String SMPT_HOSTNAME = "my smtp port no";
private static String USERNAME = "root";
private static String PASSWORD = "root";

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", SMPT_HOSTNAME);
    props.put("mail.from","[email protected]");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");

    Session session = Session.getInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USERNAME, PASSWORD);
        }
    });
    try {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO,
                          "[email protected]");
        msg.setSubject("JavaMail hello world example");
        msg.setSentDate(new Date());
        msg.setText("Hello, world!\n");
        Transport.send(msg);
     } catch (MessagingException mex) {
        System.out.println("send failed, exception: " + mex);
     }
    }
}

Solution 3

Here Mail Sender which is using MSN-SMTP service

My host is smtp.live.com and port is 587.

As given in official doc of Java Mail, here you can get more info about best Java Mail mechanism to send and receive mails.

Properties of Mail Client are:

  Properties mailProps = new Properties();
  mailProps.put("mail.smtp.user",mailID);
  mailProps.put("mail.smtp.host",host);
  mailProps.put("mail.smtp.auth", "true");
  mailProps.put("mail.smtp.port",port);
  mailProps.put("mail.smtp.starttls.enable", "true");
  Session mailSession = Session.getInstance(mailProps,null);

Sending mechanism is :

 SMTPTransport t=(SMTPTransport)mailSession.getTransport("smtp");
 System.out.println(" Taking protocol! ");
 t.connect(host, mailID, password);
 System.out.println(" Connection Successfull! ");
 t.sendMessage(mimMessage,mimMessage.getAllRecipients());

Note:
Code is running well on local Indian Server. But this is not responding at Azur Congo: Both are Linux server.

Error is:

 com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

Even if system property set manually:

 java -Dmail.smtp.starttls.enable=true SendAMai

also @Rob Harrop and @Brian 's points are ensured

if you're on Linux ensure that you have libnss3 and openssl installed
Share:
27,153

Related videos on Youtube

Brian
Author by

Brian

Updated on July 10, 2020

Comments

  • Brian
    Brian almost 4 years

    Turns out that JavaMail is a bit more frustrating than I thought it would be. I've looked at several examples online on how to send a simple SMTP email through Gmail's servers (but not through SSL). After trying several different examples of code, I keep concluding to the same example exception when I call transport.connect(). I keep getting this stack trace:

    Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
         at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
         at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
         at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
         at SendEmail.main(SendEmail.java:47)
    

    Can someone please tell me what I should add or do to fix this?

    Here is my code:

        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.host", "smtp.gmail.com");
        props.put("mail.user", "[email protected]");
        props.put("mail.password", "blah");
        props.put("mail.port", "587");
    
        Session mailSession = Session.getDefaultInstance(props, null);
        Transport transport = mailSession.getTransport();
    
        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("This is a test");
        message.setContent("This is a test", "text/plain");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    
        transport.connect();
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
    
  • Brian
    Brian almost 13 years
    Thanks that got rid of the STARTTLS problem, but I now I'm getting this exception. Can you help me? Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 mail.google.com/support/bin/answer.py?answer=14257 g2sm368231pbh.15... I checked the website that it included but I'm not finding any useful information.
  • Chilippso
    Chilippso almost 13 years
    Gah, yes, you'll also need to set the mail.smtp.auth property to true.
  • Brian
    Brian almost 13 years
    Thanks again! Nooooooow I'm getting this error... "failed to connect, no password specified?"... I know that I have provided the password as you can see from the code above. But is the password's properties tag named something else that I'm not providing correctly?
  • Chilippso
    Chilippso almost 13 years
    Ok, perhaps you should use an Authenticator rather than configuring the credentials. Remove both the mail.user and mail.password keys. Then instead of passing null as the second argument to Session.getDefaultInstance, create and pass an implementation of Authenticator that overrides the getPasswordAuthentication method - the details of this are easy.
  • Brian
    Brian almost 13 years
    Uhg, so this program works perfectly fine on my development desktop. But when I move it over to my server machine and run it, I'm getting this error: javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.io.IOException: Exception in startTLS using socket factory class null: host, port: smtp.gmail.com, 25; Exception: java.security.ProviderException: Could not initialize NSS com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:‌​1880) com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTranspor‌​t.java:648)... any ideas?
  • Chilippso
    Chilippso almost 13 years
    Did you change architecture when moving between the two environments? I think the NSS provider issue relates to the SSL configuration on the machine. If you're on Linux ensure that you have libnss3 and openssl installed.
  • Brian
    Brian almost 13 years
    The server is running on OpenSUSE, I think that OpenSSL is already installed. I'm going to try to figure out how to get libnss3 through the default repositories with YaST. Do you know if there are any other possibly important things I might need? Thanks for all your help on this by the way.
  • Brian
    Brian almost 13 years
    Never mind, I ended up finding the package I needed... all is good now! Thank you so much!