无法使用JavaMail向Exchange服务器发送超过5封电子邮件

时间:2016-03-17 10:56:40

标签: email smtp javamail exchange-server

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

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服务器上的某些配置?

1 个答案:

答案 0 :(得分:0)

我们已经看到了与MS Exchange Server 2010完全相同的问题。我们必须通过发出以下Powershell命令来增加ReceiveConnector消息速率限制:

Set-ReceiveConnector "Client CLIENTNAME" -MessageRateLimit unlimited