【问题标题】:Unable to send mail using Spring Framework无法使用 Spring Framework 发送邮件
【发布时间】:2017-07-28 22:07:49
【问题描述】:

通过发送电子邮件,我收到以下异常:

org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b10sm22671312wmi.34 - gsmtp

这是我用于发送电子邮件的代码:

MailRequest mailRequest = new MailRequest();
mailRequest.setSubject(messageByLocale.getMessage("mail.subject.forgetpassword"));
mailRequest.setTemplateName(messageByLocale.getMessage("mail.template.forgetpassword"));
mailRequest.setToEmail(tbNstyleloyalty.getEmail());
Map<String, Object> map = new HashMap<>();
map.put("tbNstyleloyalty", tbNstyleloyalty);
mailingConfig.sendEmail(mailRequest, map);

而我的sendEmail 方法是:

@Async
public void sendEmail(MailRequest mailRequest, Map<String, Object> model) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() 
    {
        @Override
        public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setTo(mailRequest.getToEmail());
                message.setSubject(mailRequest.getSubject());
                message.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templatePath + mailRequest.getTemplateName() + ".vm", ApplicationConstants.CHARSET_UTF8, model),true);
         }
    };
    this.javaMailSender.send(preparator);
}

请帮助我克服这个问题。

谢谢!

【问题讨论】:

  • 您使用的是哪个 Spring 版本?你使用 SpringBoot 吗?你能提供一些上下文,比如依赖项和 application.properties 吗?
  • 是的,我正在使用 spring boot ,你想要邮件的属性和依赖项吗?
  • 节省时间并选择Email Tools library
  • 非常感谢!!

标签: java spring email


【解决方案1】:

谢谢你们的回答。您给出的所有答案都是正确的,但我无法理解应该在哪里添加此属性。

最后我补充一下:

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

到我的属性文件,然后我就成功了。

【讨论】:

    【解决方案2】:

    我认为下面的代码很简单,可以用于邮件功能

    //headers
    import javax.mail.Message;
    import javax.mail.Message.RecipientType;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    //code
        Session mailSession = new SmtpImpl().getSession();
        Message simpleMessage = new MimeMessage(mailSession);
        MimeMultipart multipart = new MimeMultipart("related");
        BodyPart messageBodyPart = new MimeBodyPart();
        try {
            simpleMessage.setFrom(new InternetAddress("from address")));
            simpleMessage.addRecipient(RecipientType.TO, new InternetAddress("to address"));
    
            String SENDCC_GET =  "person1@gmail.com,person2.gmail.com";
            String [] SENDCC = SENDCC_GET.split(",");
            InternetAddress[] addressCC = new InternetAddress[SENDCC.length];
            for (int i = 0; i < SENDCC.length; i++) {
                addressCC[i] = new InternetAddress(SENDCC[i]);
            }
            //for bcc
            simpleMessage.setRecipients(Message.RecipientType.BCC, addressCC);
            //for cc
            //simpleMessage.setRecipients(Message.RecipientType.CC, addressCC);
    
            String message = null;
            String subject = null;
    
            subject = "email subject";
            simpleMessage.setSubject(subject);
            message = "message body";
            messageBodyPart.setContent(message, "text/html; charset=utf-8");
            multipart.addBodyPart(messageBodyPart);
            simpleMessage.setContent(multipart);
            Transport.send(simpleMessage);
    
        } catch (AddressException e) {
            LOGGER.error("******Error while sending - Email: Address Exception::: " + e);
        } catch (MessagingException e) {
            LOGGER.error("******Error while sending - Email: Messaging Exception::: " + e);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    
    
        //SmtpImpl.java
    
        public Session getSession(){
            Properties props = new Properties();
            props.put("mail.smtp.host", EmailUtilityParam.getSmtpHostName());
            props.put("mail.smtp.port", EmailUtilityParam.getSmtpPort());
            props.put("mail.smtp.user", EmailUtilityParam.getAuthenticationId());
            props.put("mail.smtp.password", EmailUtilityParam.getAuthenticationPassword());
            props.put("mail.debug", "false");
            Session mailSession = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(EmailUtilityParam
                            .getAuthenticationId(), EmailUtilityParam
                            .getAuthenticationPassword());
                }
            });
            return mailSession;
        }
    

    SMTP 开发工具可用于开发,并将其保留为默认值以测试其是否有效

    HOST = localhost
    PORT = 25
    USER(authentication id) = //empty string
    PASSWORD(authentication password) = //empty string
    

    【讨论】:

      【解决方案3】:

      您可能缺少两点中的任何一个:

      1. 在您的电子邮件请求中设置以下属性。

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

      1. 您可能正试图使用​​端口 25 上的邮件服务器通过未经身份验证的连接将邮件传递给第三方。您需要让您的 SMTP 客户端连接到经过身份验证的连接。 (查看您的端口号是否用于 TLS 连接)

      【讨论】:

      • 谢谢你,我正在使用 587 端口发送邮件
      【解决方案4】:

      我猜您的邮件服务器使用 STARTSSL 进行身份验证(正如异常消息所暗示的那样)。

      也许这篇文章有帮助: JavaMail smtp properties (for STARTTLS)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 2017-09-28
        • 2017-10-08
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多