JavaMail STARTTLS Error?

4.9k views Asked by At

I have the following code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;

public class Alpha{

public static void main (String[] args){

    final String username = "[email protected]";
    final String password = "mypassword";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smpt.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");


    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication (username, password);
        }
    });

    try{
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]"));
        message.setSubject("Test Email");
        message.setText("Message");
        Transport.send(message);

        System.out.println("The message was sent");


        } catch (MessagingException e){
            throw new RuntimeException(e);
    }


}

}

But keep getting this error:

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. p3sm47032789wjf.49 - gsmtp

at Alpha.main(Alpha.java:46)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. p3sm47032789wjf.49 - 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 Alpha.main(Alpha.java:40)

I've looked around and found an answer to a similar question with the line : props.put("mail.smpt.starttls.enable", "true");, but it doesnt work still, and I am at loss as on what to do!

1

There are 1 answers

2
M A On BEST ANSWER

You have a typo in the property name:

mail.smtp.starttls.enable

instead of

mail.smpt.starttls.enable