gpt4 book ai didi

java - 使用不同的邮件 api jar 降低 sendmail 性能 (javamail)

转载 作者:行者123 更新时间:2023-12-04 19:28:52 41 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





MimeMessage.saveChanges is really slow

(1 个回答)


3年前关闭。




我正在测试中所述的 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 次并获得了平均持续时间。
  • JavaMailApi 版本:1.4.0 持续时间 < 1 秒。
  • JavaMailApi 版本:1.4.2 持续时间 9-10 秒。
  • JavaMailApi 版本:1.5.2 持续时间 29-30 秒。
  • JavaMailApi 版本:1.5.6 持续时间 29-30 秒。
  • JavaMailApi 版本:1.6.0 持续时间 29-30 秒。

  • 使用的来源:
    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 邮件 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
    Sending mail.....completed in 391
    Average took: *391.0*

    Java 邮件 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
    Sending mail.....completed in 9423
    Average took: *9423.0*

    jvm 邮件调试输出

    Java 邮件 API 1.4.0
    DEBUG: setDebug: JavaMail version 1.4ea
    DEBUG: getProvider() returning
    javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
    mtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "192.168.0.X", port 25, isSSL
    false
    220 mail.******.com ESMTP Postfix
    DEBUG SMTP: connected to host "192.168.0.X", port: 25
    EHLO WIN-0RL58R42TTN
    250-mail.******.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: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "SIZE", arg "102400000"
    DEBUG SMTP: Found extension "VRFY", arg ""
    DEBUG SMTP: Found extension "ETRN", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
    DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "DSN", arg ""
    DEBUG SMTP: Attempt to authenticate
    AUTH LOGIN
    334 VXNlcm5hbWU6
    am9iaW4uZWxhbmppY2thbEBhbGNvbmVsZWN0cm9uaWNzLmNvbQ==
    334 UGFzc3dvcmQ6
    Sk9CSU5ANzc3TnM=
    235 2.7.0 Authentication successful
    DEBUG SMTP: use8bit false
    MAIL FROM:<sender@******.com>
    250 2.1.0 Ok
    RCPT TO:<receiver@******.com>
    250 2.1.5 Ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP: receiver@******.com
    DATA
    354 End data with <CR><LF>.<CR><LF>
    Date: Thu, 14 Dec 2017 14:17:46 +0530 (IST)
    From: sender@******.com
    To: receiver@******.com
    Message-ID: <396180261.01513241266298.JavaMail.sender@******
    .com>
    Subject: test3
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="----=_Part_0_2093176254.1513241266126"
    ------=_Part_0_2093176254.1513241266126
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: 7bit
    test
    ------=_Part_0_2093176254.1513241266126--
    .
    250 2.0.0 Ok: queued as 7E38D48F0411
    QUIT
    221 2.0.0 Bye

    Java 邮件 API 1.5.6
    DEBUG: setDebug: JavaMail version 1.5.6
    DEBUG: getProvider() returning
    javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
    mtp.SMTPTransport,Oracle]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "192.168.0.X", port 25, isSSL
    false
    220 mail.******.com ESMTP Postfix
    DEBUG SMTP: connected to host "192.168.0.X", port: 25
    EHLO WIN-0RL58R42TTN
    250-mail.******.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: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "SIZE", arg "102400000"
    DEBUG SMTP: Found extension "VRFY", arg ""
    DEBUG SMTP: Found extension "ETRN", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
    DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "DSN", arg ""
    DEBUG SMTP: protocolConnect login, host=192.168.0.X, user=sender@alc
    onelectronics.com, password=<non-null>
    DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN
    DIGEST-MD5 NTL
    M XOAUTH2
    DEBUG SMTP: Using mechanism LOGIN
    DEBUG SMTP: AUTH LOGIN command trace suppressed
    DEBUG SMTP: AUTH LOGIN succeeded
    DEBUG SMTP: use8bit false
    MAIL FROM:<sender@******.com>
    250 2.1.0 Ok
    RCPT TO:<receiver@******.com>
    250 2.1.5 Ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP: receiver@******.com
    DATA
    354 End data with <CR><LF>.<CR><LF>
    Date: Thu, 14 Dec 2017 14:18:03 +0530 (IST)
    From: sender@******.com
    To: receiver@******.com
    Message-ID: <1109371569.1.1513241292669@******.com>
    Subject: test3
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="----=_Part_0_1349393271.1513241283434"
    ------=_Part_0_1349393271.1513241283434
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: 7bit
    test
    ------=_Part_0_1349393271.1513241283434--
    .
    250 2.0.0 Ok: queued as D894D48F0411
    DEBUG SMTP: message successfully delivered to mail server
    QUIT
    221 2.0.0 Bye

    最佳答案

    我不相信只有一个原因让它变慢。您可能需要在示例下方更改一些默认值。
    In the 1.6 document of the mail api属性(property)mail.mime.address.usecanonicalhostname它指出:

    Use the InetAddress.getCanonicalHostName method to determine the host name in the InternetAddress.getLocalAddress method. With some network configurations, InetAddress.getCanonicalHostName may be slow or may return an address instead of a host name. In that case, setting this System property to false will cause the InetAddress.getHostName method to be used instead. The default is true.


    所以如果你用 false 改变默认值,它可能会改变它的性能。
    我还建议检查 java mail apis release notes找出一些线索。

    关于java - 使用不同的邮件 api jar 降低 sendmail 性能 (javamail),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807784/

    41 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com