我正在测试sendmail示例,如中所述 https://javaee.github.io/javamail/FAQ。这是一个简单的邮件发送操作,没有SSL,也没有附件。它在任何地方都可以正常工作,但在客户端,当邮件api变得更新时,我们有一个去线性能。
服务器是windows,java 1.8.131,在所有测试用例中使用相同的邮件服务器和邮件帐户以及相同的地址和java源。唯一的区别是使用的mailapi。我用
运行程序java -cp javax.mail.1.X.X.jar; sendmailtesttool.jar SendMail
我为每个邮件api尝试了100次并获得平均持续时间。
使用的来源:
Properties properties = new Properties();
properties.put("mail.smtp.host", 192.168.0.X); // use default port25
Session session = Session.getInstance(properties);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(..);
msg.setSubject(..);
msg.setRecipients(..);
//no attachments
MultiPart mp = new MultiPart();
MimeBodyPart bp = new MimeBodyPart();
DataSource ds = ByteArrayDataSource("foo"..);
DataHandler dh = new DataHandler(ds);
bp.setDataHandler(dh);
mp.addBodyPart(bp);
msg.setContent(mp);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
msg.saveChanges(); // is really needed ? (no reply/forward)
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
我添加了测试工具的输出:
Java Mail Api 1.4.0
Sending mail.....start
Setting port to:25
Setting host to:192.168.0.213
Set additional session prop host to prevent HELO
Set additional session prop from to prevent EHLO
Session generated with authenticator took:63
Message generated took:47
Parse from address took:0
Set from address to message took:0
Set recipients took:0
Get all recipients took:0
Set cc, bcc took:0
Parse subject took:16
Set subject to message took:0
Set body text took:0
Set content took:0
Set sent date took:15
Transport generated took:32
Connected to transport took:93
Save changes took:0
Message send took:94
Transport closed took:0
Message id fetched took:0
发送邮件.....在391完成
平均值: 391.0
Java Mail Api 1.5.6
Sending mail.....start
Setting port to:25
Setting host to:192.168.0.213
Set additional session prop host to prevent HELO
Set additional session prop from to prevent EHLO
Session generated with authenticator took:62
Message generated took:63
Parse from address took:0
Set from address to message took:16
Set recipients took:0
Get all recipients took:0
Set cc, bcc took:0
Parse subject took:0
Set subject to message took:0
Set body text took:0
Set content took:0
Set sent date took:15
Transport generated took:32
Connected to transport took:9126
Save changes took:0
Message send took:47
Transport closed took:0
Message id fetched took:0
发送邮件.....在9423完成
平均值: 9423.0
jvm邮件调试输出
Java Mail API 1.4.0
DEBUG:setDebug:JavaMail version 1.4ea
DEBUG:getProvider()返回 javax.mail.Provider [TRANSPORT,SMTP,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems,Inc]
DEBUG SMTP:useEhlo true,useAuth true
DEBUG SMTP:尝试连接到主机" 192.168.0.X",端口25,isSSL 假
220 mail。******。com ESMTP Postfix
DEBUG SMTP:连接到主机" 192.168.0.X",端口:25
EHLO WIN-0RL58R42TTN
250的电子邮件。******。com
250 PIPELINING
250-SIZE 102400000
250 VRFY
250 ETRN
250 STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH = PLAIN LOGIN
250 ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP:找到扩展名" PIPELINING",arg""
DEBUG SMTP:找到扩展名" SIZE",arg" 102400000"
DEBUG SMTP:找到扩展名" VRFY",arg""
DEBUG SMTP:找到扩展名" ETRN",arg""
DEBUG SMTP:找到扩展名" STARTTLS",arg""
DEBUG SMTP:找到扩展名" AUTH",arg" PLAIN LOGIN"
DEBUG SMTP:找到扩展名" AUTH = PLAIN",arg"登录"
DEBUG SMTP:找到扩展名" ENHANCEDSTATUSCODES",arg""
DEBUG SMTP:找到扩展名" 8BITMIME",arg""
DEBUG SMTP:找到扩展名" DSN",arg""
DEBUG SMTP:尝试进行身份验证
AUTH LOGIN
334 VXNlcm5hbWU6
am9iaW4uZWxhbmppY2thbEBhbGNvbmVsZWN0cm9uaWNzLmNvbQ ==
334 UGFzc3dvcmQ6
Sk9CSU5ANzc3TnM =
235 2.7.0认证成功
DEBUG SMTP:use8bit false
MAIL FROM:
250 2.1.0好
RCPT TO:
250 2.1.5好
DEBUG SMTP:已验证的地址
DEBUG SMTP:receiver @ ******。com
DATA
354结束数据。
日期:星期四,2017年12月14日14:17:46 +0530(IST)
来自:sender @ ******。com
To:receiver @ ******。com
消息ID:< 396180261.01513241266298.JavaMail.sender @ ******
.COM>
主题:test3
MIME-Version:1.0
内容类型:multipart / mixed;
boundary="----=_Part_0_2093176254.1513241266126"
------ = _ Part_0_2093176254.1513241266126
Content-Type:text / plain;字符集= UTF-8
Content-Transfer-Encoding:7位
测试
------ = _ Part_0_2093176254.1513241266126 -
250 2.0.0好:排队为7E38D48F0411
QUIT
221 2.0.0再见
Java Mail API 1.5.6
DEBUG:setDebug:JavaMail 1.5.6版
DEBUG:getProvider()返回 javax.mail.Provider [TRANSPORT,SMTP,com.sun.mail.s
mtp.SMTPTransport,甲骨文]
DEBUG SMTP:useEhlo true,useAuth true
DEBUG SMTP:尝试连接到主机" 192.168.0.X",端口25,isSSL 假
220 mail。******。com ESMTP Postfix
DEBUG SMTP:连接到主机" 192.168.0.X",端口:25
EHLO WIN-0RL58R42TTN
250的电子邮件。******。com
250 PIPELINING
250-SIZE 102400000
250 VRFY
250 ETRN
250 STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH = PLAIN LOGIN
250 ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP:找到扩展名" PIPELINING",arg""
DEBUG SMTP:找到扩展名" SIZE",arg" 102400000"
DEBUG SMTP:找到扩展名" VRFY",arg""
DEBUG SMTP:找到扩展名" ETRN",arg""
DEBUG SMTP:找到扩展名" STARTTLS",arg""
DEBUG SMTP:找到扩展名" AUTH",arg" PLAIN LOGIN"
DEBUG SMTP:找到扩展名" AUTH = PLAIN",arg"登录"
DEBUG SMTP:找到扩展名" ENHANCEDSTATUSCODES",arg""
DEBUG SMTP:找到扩展名" 8BITMIME",arg""
DEBUG SMTP:找到扩展名" DSN",arg""
DEBUG SMTP:protocolConnect login,host = 192.168.0.X,user = sender @ alc
onelectronics.com,密码=
DEBUG SMTP:尝试使用机制进行身份验证:登录PLAIN DIGEST-MD5 NTL
M XOAUTH2
DEBUG SMTP:使用机制LOGIN
DEBUG SMTP:AUTH LOGIN命令跟踪被抑制
DEBUG SMTP:AUTH LOGIN成功
DEBUG SMTP:use8bit false
MAIL FROM:
250 2.1.0好
RCPT TO:
250 2.1.5好
DEBUG SMTP:已验证的地址
DEBUG SMTP:receiver @ ******。com
DATA
354结束数据。
日期:星期四,2017年12月14日14:18:03 +0530(IST)
来自:sender @ ******。com
To:receiver @ ******。com
消息ID:< 1109371569.1.1513241292669 @ ******。com>
主题:test3
MIME-Version:1.0
内容类型:multipart / mixed;
boundary="----=_Part_0_1349393271.1513241283434"
------ = _ Part_0_1349393271.1513241283434
Content-Type:text / plain;字符集= UTF-8
Content-Transfer-Encoding:7位
测试
------ = _ Part_0_1349393271.1513241283434 -
250 2.0.0好:排队为D894D48F0411
DEBUG SMTP:邮件已成功发送到邮件服务器
QUIT
221 2.0.0再见
答案 0 :(得分:3)
我不相信只有一个理由让它变慢。您可能需要在示例下面更改一些默认值。
In 1.6 document of the mail api.
使用InetAddress.getCanonicalHostName方法确定InternetAddress.getLocalAddress方法中的主机名。对于某些网络配置,InetAddress.getCanonicalHostName可能很慢或可能返回地址而不是主机名。在这种情况下,将此System属性设置为false将导致使用InetAddress.getHostName方法。默认值为true。
因此,如果您使用false更改默认值,则可能会更改其性能。
我还建议检查java mail apis release notes以找出一些线索。
答案 1 :(得分:1)
您的代码中存在错误。使用session.getTransport而不是session.getInstance获取Transport对象;我们不知道你的呼叫中的主机初始化位置transport.connect(host,..);如果您设置正确的属性并且没有任何DNS解决问题,则connect(用户,密码)应该足够。