JavaMail STARTTLS错误?

时间:2014-11-16 12:08:01

标签: java parsing javamail starttls

我有以下代码:

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 = "myemail@gmail.com";
    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("emailfrom@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("email.to@gmail.com"));
        message.setSubject("Test Email");
        message.setText("Message");
        Transport.send(message);

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


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


}

}

但不断收到此错误:

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)

我环顾四周,找到了一个类似问题的答案:props.put(" mail.smpt.starttls.enable"," true") ;但是它仍然不起作用,我不知道该做什么!

1 个答案:

答案 0 :(得分:2)

您在属性名称中输入了错字:

mail.smtp.starttls.enable

而不是

mail.smpt.starttls.enable