必须首先发出STARTTLS命令.mail.smtp.EnableSSL.enable已设置为true



我正在开发Java Spring,Maven,我正在尝试使用gmail id在gmail上发送邮件。我收到了这个错误:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 11sm885884pfp.38 - gsmtp

即使我已经设置了参数

mail.smtp.starttls.enable

真实。

这是我的功能:

public String sendMail(String to, String subject, String textMessage) throws IOException {
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    Session mailSession = Session.getInstance(props);
    mailSession.setDebug(true);
    Message simpleMessage = new MimeMessage(mailSession);
    InternetAddress fromAddress = from@gmail.com;
    InternetAddress toAddress = null;
    try {
        toAddress = new InternetAddress(to);
    } catch (AddressException e) {
        System.out.print("nError in Address of Sender or recievern");
        e.printStackTrace();
    }
    try {
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO, toAddress);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(textMessage);
        SMTPTransport t = (SMTPTransport) mailSession.getTransport("smtp");
        t.setStartTLS(true);
        t.connect("smtp.gmail.com", "from@gmail.com", "from.password!");
        t.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
        t.close();
    } catch (MessagingException e) {             
        System.out.print("nError in message Genrationn");
        e.printStackTrace();
    }
    return "success";
}

SO上的所有答案都告诉我们将mail.smtp.starttls.enable设置为true。这样做根本不起作用。

我还在pom中包含了javax.mail依赖项:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

使用spring时,最好合并它们的抽象,而不是javax.mail低级别API。事实上,您可以使用JavaMailSender及其实现JavaMailSenderImpl来实现相同的结果。请参见此示例。

最新更新