59

I am running this simple example with my Gmail account, but its not working and giving the following error:

      send failed, exception: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. nv2sm4478384pbb.6      

Here is my code

   public class Email
{
   public static void main(String [] args)
   {

       Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.googlemail.com");
        props.put("mail.from", "myemail@gmail.com");
          Session session = Session.getInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO,
                              "myemail@hotmail.com");
            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);
        }
   }
}
DevDave
  • 6,700
  • 12
  • 65
  • 99
user1226162
  • 1,972
  • 8
  • 27
  • 42
  • 1
    Must issue a STARTTLS command first. nv2sm4478384pbb.6 – user1226162 May 09 '12 at 04:13
  • 11
    You need to add the following line: props.put("mail.smtp.starttls.enable", "true"); – Ripon Al Wasim Jan 07 '13 at 06:56
  • Dupe of http://stackoverflow.com/questions/10509699/must-issue-a-starttls-command-first – james.garriss Jan 18 '13 at 13:19
  • possible duplicate of [Must issue a STARTTLS command first. Sending email with Java and Google Apps](http://stackoverflow.com/questions/386083/must-issue-a-starttls-command-first-sending-email-with-java-and-google-apps) – Peter O. Jan 19 '13 at 03:43
  • see my answer here: http://stackoverflow.com/questions/26774057/how-to-send-emails-via-spring/29676871#29676871 maybe it'll help – steineron Apr 16 '15 at 13:59
  • checkout my answer here: http://stackoverflow.com/questions/26774057/how-to-send-emails-via-spring/29676871#29676871 I hope it'll help – steineron Apr 16 '15 at 14:01
  • checkout my answer here: http://stackoverflow.com/questions/26774057/how-to-send-emails-via-spring/29676871#29676871 I hope it'll help – steineron Apr 16 '15 at 14:03

9 Answers9

55

You are probably attempting to use Gmail's servers on port 25 to deliver mail to a third party over an unauthenticated connection. Gmail doesn't let you do this, because then anybody could use Gmail's servers to send mail to anybody else. This is called an open relay and was a common enabler of spam in the early days. Open relays are no longer acceptable on the Internet.

You will need to ask your SMTP client to connect to Gmail using an authenticated connection, probably on port 587.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Is there any email provider that can accept unauthenticated connection, those days ? – Error Jan 30 '16 at 14:19
  • 1
    Now to access mail from Any Java Client you might need to reduce security. Read https://support.google.com/accounts/answer/6010255 – Ashu Sep 29 '16 at 04:44
25

smtp port and socketFactory has to be change

    String to = "reciveremail@xxxx.xxx";
    String subject = "subject"
    String msg ="email text...."
    final String from ="senderemail@gmail.com"
    final  String password ="senderPassword"


    Properties props = new Properties();  
    props.setProperty("mail.transport.protocol", "smtp");     
    props.setProperty("mail.host", "smtp.gmail.com");  
    props.put("mail.smtp.auth", "true");  
    props.put("mail.smtp.port", "465");  
    props.put("mail.debug", "true");  
    props.put("mail.smtp.socketFactory.port", "465");  
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
    props.put("mail.smtp.socketFactory.fallback", "false");  
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {  
       return new PasswordAuthentication(from,password);  
   }  
   });  

   //session.setDebug(true);  
   Transport transport = session.getTransport();  
   InternetAddress addressFrom = new InternetAddress(from);  

   MimeMessage message = new MimeMessage(session);  
   message.setSender(addressFrom);  
   message.setSubject(subject);  
   message.setContent(msg, "text/plain");  
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  

   transport.connect();  
   Transport.send(message);  
   transport.close();
   }  

hope it will work for you..

Pradeep Maurya
  • 295
  • 1
  • 5
  • 11
25

Adding

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

solved my problem ;)

My problem was :

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u186sm7971862pfu.82 - 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 javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.example.sendmail.SendEmailExample2.main(SendEmailExample2.java:53)
6

I also faced the same issue while I was building email notification application. you just need to add one line. Below one saved my day.

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

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. h13-v6sm10627790pgp.13 - 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 javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.smruti.email.EmailProject.EmailSend.main(EmailSend.java:99)

Hope this helps you.

PyDevSRS
  • 1,715
  • 16
  • 17
3

Google now has a feature stating that it won't allow insecure devices to send emails. When I ran my program it came up with the error in the first post. I had to go into my account and allow insecure apps to send emails, which I did by clicking on my account, going into the security tab, and allowing insecure apps to use my gmail.

J Medeiros
  • 51
  • 2
1

I was also getting this error as I was setting the props.put("mail.smtp.starttls.enable", myMailConfig.StartTLS); and here myMailConfig.StartTLS was String and had a true value, So this String literal was the very first cause. So try to set it (boolean) true like props.put("mail.smtp.starttls.enable", true); or if its dynamic then make sure your variable is boolean or if its String then db value should be set as true.

0
    String username = "mail@google.com";
    String password = "some-password";
    String recipient = "myemail@hotmail.com");

    Properties props = new Properties();

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.from", "myemail@gmail.com");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", "587");
    props.setProperty("mail.debug", "true");

    Session session = Session.getInstance(props, null);
    MimeMessage msg = new MimeMessage(session);

    msg.setRecipients(Message.RecipientType.TO, recipient);
    msg.setSubject("JavaMail hello world example");
    msg.setSentDate(new Date());
    msg.setText("Hello, world!\n");

    Transport transport = session.getTransport("smtp");

    transport.connect(username, password);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
Vadim Ponomarev
  • 1,346
  • 9
  • 15
0

Try this code :

Properties props = new Properties();
                        props.put("mail.smtp.host", "smtp.gmail.com");
                        props.put("mail.smtp.socketFactory.port", "465");
                        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                        props.put("mail.smtp.auth", "true");
                        props.put("mail.smtp.prot", "465");

                        Session session = Session.getDefaultInstance(props,
                                new javax.mail.Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {

                                return new PasswordAuthentication("PUT THE MAIL SENDER HERE !", "PUT THE PASSWORD OF THE MAIL SENDER HERE !");
                            }
                        }
                        );
                        try {
                            Message message = new MimeMessage(session);
                            message.setFrom(new InternetAddress("PUT THE MAIL SENDER HERE !"));
                            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("PUT THE MAIL RECEIVER HERE !"));
                            message.setSubject("MAIL SUBJECT !");
                            message.setText("MAIL BODY !");
                            Transport.send(message);

                        } catch (Exception e) {
                            JOptionPane.showMessageDialog(null, e);
                        }

You have to less secure the security of the mail sender. if the problem persist I think It can be caused by the antivirus, try to disable it ..

Haddad
  • 107
  • 3
  • 9
0

Add this in file application.properties:

spring.mail.properties.mail.smtp.starttls.required=true

That works for me