SMTP发送多封电子邮件

时间:2019-06-04 17:59:47

标签: email smtp javamail

我必须每月每个月1日向大约150个人发送提醒电子邮件。

似乎运行良好,但是这个月...我开始了解

Caused by: javax.mail.AuthenticationFailedException: 421 4.3.2 Service not active [BN6PR1801CA0007.namprd18.prod.outlook.com]

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965) ~[javax.mail-1.6.2.jar!/:1.6.2]
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876) ~[javax.mail-1.6.2.jar!/:1.6.2]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780) ~[javax.mail-1.6.2.jar!/:1.6.2]
at javax.mail.Service.connect(Service.java:366) ~[javax.mail-1.6.2.jar!/:1.6.2]

此错误消息突然出现。

它仍然可以很好地发送给少数人。

当我尝试大量用户时,它发送到大约10个人并抛出该错误。 然后,它在几分钟后发送其余电子邮件;但是,由于错误,它似乎无法正确结束循环。

--- application.properties ---

spring.mail.host=smtp.office365.com
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.username=xx@xxxx.com
spring.mail.password=xxxxxx
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtps.timeout=8000

---发送方法---

public void sendEmail(String tok, String email, int file, String msg, String to) throws UnsupportedEncodingException, MessagingException, FileNotFoundException
    {       
        FilePath fp = new FilePath();
        String token = tok;
        String recipientAddress = email;
        String subjectConfirm = "Confirm " + to;
        String subjectAlert = "Alert " +  to;
        String confirmationUrl = fp.getServer() + "/to/filesConfirm?token=" + token;
        String message = msg;

        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {

                mimeMessage.setRecipient(Message.RecipientType.TO, 
                        new InternetAddress(recipientAddress));
                mimeMessage.setFrom(new InternetAddress("xx@xxxx.com"));

                if (file > 0) 
                {
                    mimeMessage.setSubject(subjectConfirm);
                    mimeMessage.setContent(message + "<br>" + "<a target='_blank' href="+confirmationUrl+"><button>Confirm</button></a>"
                            + "<br>" + "If the button above doesn't work, please click or copy&paste the address below to 'Confirm'."
                            + "<br>" + confirmationUrl, "text/html;charset=UTF-8");
                }
                else
                {
                    mimeMessage.setText(message);
                    mimeMessage.setSubject(subjectAlert);
                }
            }
        }; this.mailSender.send(preparator);
    }

1 个答案:

答案 0 :(得分:0)

我已通过将主机切换到亚马逊来修复它