Must issue a STARTTLS command first

Java

Java Problem Overview


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", "[email protected]");
	      Session session = Session.getInstance(props, null);

	    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);
	    }
   }
}

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

smtp port and socketFactory has to be change

    String to = "[email protected]";
    String subject = "subject"
    String msg ="email text...."
    final String from ="[email protected]"
    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..

Solution 3 - Java

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)

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

	String username = "[email protected]";
	String password = "some-password";
	String recipient = "[email protected]");

	Properties props = new Properties();

	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.from", "[email protected]");
	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();

Solution 7 - Java

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 ..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1226162View Question on Stackoverflow
Solution 1 - JavaGreg HewgillView Answer on Stackoverflow
Solution 2 - JavaPradeep MauryaView Answer on Stackoverflow
Solution 3 - JavaVasudeva KrishnanView Answer on Stackoverflow
Solution 4 - JavaPyDevSRSView Answer on Stackoverflow
Solution 5 - JavaJ MedeirosView Answer on Stackoverflow
Solution 6 - JavaVadim PonomarevView Answer on Stackoverflow
Solution 7 - JavaHaddadView Answer on Stackoverflow