【问题标题】:Unable to send more than 5 emails to Exchange server using JavaMail无法使用 JavaMail 向 Exchange 服务器发送超过 5 封电子邮件
【发布时间】:2016-07-03 16:20:24
【问题描述】:

我有一种方法可以使用 JavaMail 通过 smtp 向交换服务器发送许多电子邮件,下面是我的代码,

public void sendMail(){
        final String host="host",port="587",username="mail1@local.local",password="password",from="";
        try {

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.ssl.trust", host);

            final String email = from;

            Authenticator authenticator = new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(username, password);
                }
            };

            Session session = Session.getInstance(props,authenticator);
            InternetAddress replyToAddress [] = new InternetAddress[1];
            replyToAddress[0] = new InternetAddress("mail1@local.local");
            Transport transport = session.getTransport("smtp");
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress("mail1@local.local"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("mail2@local.local"));
            mimeMessage.setSubject("Test");
            mimeMessage.setText("Hello Testing");

            mimeMessage.setReplyTo(replyToAddress);
            transport.send(mimeMessage);
            System.out.println("Email has been Sent Successfully to");
       } catch (MessagingException e) {
            e.printStackTrace();
        }

现在,当我应用循环并调用此函数 10 次时,只有前五封电子邮件被成功发送,对于其余的请求,我得到以下异常,

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    java.net.SocketException: Connection closed by remote host
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2157)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2144)
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1210)
    at javax.mail.Transport.send0(Transport.java:197)
    at javax.mail.Transport.send(Transport.java:124)

如果我增加请求计数,那么仍然只发送前五封电子邮件,其余的会抛出异常。

如果我将失败的请求放入某个队列并稍后重试,那么其中一些将被发送。

任何线索将不胜感激,我需要检查 Exchange 服务器上的某些配置吗?

【问题讨论】:

  • 您是否尝试将代码更改为only establish the connection once 以发送多条消息?
  • 首先,您应该阅读this JavaMail FAQ entry 并修复常见错误。重用连接发送多条消息可能会有所帮助。几乎可以肯定是 Exchange 服务器阻止了您,因为您在太短的时间内发送了太多邮件或创建了太多连接等等。

标签: email smtp jakarta-mail exchange-server


【解决方案1】:

我们在 MS Exchange Server 2010 上看到了同样的问题。我们不得不通过发出以下 Powershell 命令来提高 ReceiveConnector 消息速率限制:

Set-ReceiveConnector "Client CLIENTNAME" -MessageRateLimit unlimited

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2017-07-28
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多