使用java发送邮件时延迟套接字通信

时间:2016-06-23 07:59:36

标签: java windows sockets email localhost

在我的Windows 7系统上发送测试电子邮件时,总共有非常长的延迟 20秒。我已经尝试过不同的smtp服务器实现(jsmtpd,subethasmtp),但两者都显示相同的延迟。在Linux系统上运行相同的测试没有延迟。这是一个Windows localhost网络问题吗?

我的示例代码:

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.subethamail.wiser.Wiser;

public class C_MailerTest {
    public static void main( String[] args ) {
        System.setProperty( DefaultConfiguration.DEFAULT_LEVEL, Level.DEBUG.name() );
        Wiser wiser = new Wiser();
        wiser.start();

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setRecipient( Message.RecipientType.TO, new InternetAddress( "mail@yourcompany.com" ) );
                mimeMessage.setFrom( new InternetAddress( "mail@mycompany.com" ) );
                mimeMessage.setText( "test" );
            }
        };

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost( "127.0.0.1" );
        System.out.println( "sending mail" );
        sender.send(preparator);

        System.out.println( "mails received: " + wiser.getMessages().size() );
        wiser.stop();
    }
}

以下是日志(Windows 7企业版,Service Pack 1):

09:36:04.340 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: AUTH
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: DATA
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: EHLO
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: HELO
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: HELP
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: MAIL
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: NOOP
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: QUIT
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: RCPT
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: RSET
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: STARTTLS
09:36:04.341 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: VRFY
sending mail
09:36:04.444 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - SMTP connection count: 1
09:36:04.451 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 220 SWE-JPW.intern.src ESMTP SubEthaSMTP
09:36:04.455 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: EHLO SWE-JPW.intern.src
09:36:04.461 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250-SWE-JPW.intern.src
250-8BITMIME
250-STARTTLS
250-AUTH PLAIN LOGIN
250 Ok
09:36:04.481 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: MAIL FROM:<mail@mycompany.com>
09:36:04.481 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:04.484 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: RCPT TO:<mail@yourcompany.com>
09:36:04.484 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:09.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: NOOP
09:36:09.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:14.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: NOOP
09:36:14.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:19.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: NOOP
09:36:19.495 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:24.049 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: DATA
09:36:24.049 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 354 End data with <CR><LF>.<CR><LF>
09:36:24.051 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:36:24.055 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: QUIT
09:36:24.055 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 221 Bye
mails received: 1
09:36:24.058 [org.subethamail.smtp.server.SMTPServer] INFO  org.subethamail.smtp.server.SMTPServer - SMTP Server socket shut down.

有很多NOOP日志条目延迟了5秒钟。

在linux机器上运行相同测试的日志(ubuntu):

09:53:55.312 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: AUTH
09:53:55.313 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: DATA
09:53:55.313 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: EHLO
09:53:55.313 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: HELO
09:53:55.313 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: HELP
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: MAIL
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: NOOP
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: QUIT
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: RCPT
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: RSET
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: STARTTLS
09:53:55.314 [main] DEBUG org.subethamail.smtp.server.CommandHandler - Added command: VRFY
sending mail
09:53:55.395 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - SMTP connection count: 1
09:53:55.397 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 220 vm-gkls ESMTP SubEthaSMTP
09:53:55.399 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: EHLO vm-gkls
09:53:55.402 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250-vm-gkls
250-8BITMIME
250-STARTTLS
250-AUTH PLAIN LOGIN
250 Ok
09:53:55.422 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: MAIL FROM:<mail@mycompany.com>
09:53:55.422 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:53:55.423 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: RCPT TO:<mail@yourcompany.com>
09:53:55.423 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:53:55.423 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: DATA
09:53:55.423 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 354 End data with <CR><LF>.<CR><LF>
09:53:55.426 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 250 Ok
09:53:55.427 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Client: QUIT
09:53:55.427 [org.subethamail.smtp.server.ConnectionHandler] DEBUG org.subethamail.smtp.server.ConnectionHandler - Server: 221 Bye
mails received: 1
09:53:55.429 [org.subethamail.smtp.server.SMTPServer] INFO  org.subethamail.smtp.server.SMTPServer - SMTP Server socket shut down.

在Linux系统上绝对没有延迟。程序立即发送和接收邮件。

环境:

  • java 8(1.8.0-91)
  • spring 4.2.6
  • javax.mail 1.5.5
  • subethasmtp-wiser 1.2

0 个答案:

没有答案