Total Pageviews

2016/04/06

[Java Mail] com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first.

Problem
I am using gmail as my testing mail server to test Java Mail code.
Here is my test code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailTest {

    public static void main(String[] args) {

        String host = "smtp.gmail.com";
        int port = 587;
        String userName = "xxx";
        String password = "xxx";

        String mailTo = "xxx@gmail.com";
        String subject = "Hello my friend~";

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host);
        sender.setPort(port);
        sender.setUsername(userName);
        sender.setPassword(password);

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setTo(mailTo);
            helper.setSubject(subject);
            helper.setText("test test");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        sender.send(message);
    }
}

But as I run this test code, it throw exception as bellows:
1
2
3
4
5
6
7
8
9
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. wb7sm14621901pab.3 - gsmtp

 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
 at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448)
 at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
 at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
 at albert.practice.mail.MailTest.main(MailTest.java:52)


How-to
We need to enables the use of the STARTTLS command to switch the connection to a TLS-protected connection before issuing any login commands. 

Therefore, the source code would be modified as following:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailTest {

    public static void main(String[] args) {

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

        String host = "smtp.gmail.com";
        int port = 587;
        String userName = "xxx";
        String password = "xxx";

        String mailTo = "xxx@gmail.com";
        String subject = "Hello my friend~";

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setJavaMailProperties(props);
        sender.setHost(host);
        sender.setPort(port);
        sender.setUsername(userName);
        sender.setPassword(password);

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setTo(mailTo);
            helper.setSubject(subject);
            helper.setText("test test");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        sender.send(message);
    }


Test successful !

Reference
[1] http://stackoverflow.com/questions/17581066/using-javamail-with-a-self-signed-certificate

1 comment:

Saket said...

Hi Albert,

It worked!
Thanks for detailed example.

It wasnt working when i was using mail.smtp.starttls.enable in .properties file directly.
props.put("mail.smtp.starttls.enable", "true");