开发者

Using gmail as SMTP server in Java web app is slow

I was wondering if anyone might be able to explain to me why it's taking nearly 30 seconds each time my Java web app sends an email using Gmail's SMTP server? See the following timestamps:

13/04/2010-22:24:27:281 DEBUG test.service.impl.SynchronousEmailService - Before sending mail. 13/04/2010-22:24:52:625 DEBUG test.service.impl.SynchronousEmailService - After sending mail.

I'm using spring's JavaMailSender class with the following settings:

email.host=smtp.gmail.com

email.username=myaccount@gmail.com email.password=mypassword email.port=465 mail.smtp.auth.requi开发者_运维问答red=true

Note that the mail is getting sent and I'm receiving it fine, there's just this delay which is resulting in a slow experience for the application user.

If you know how I can diagnose the problem myself that would be good too :)


It might be that gmail uses this delay to prevent spammers from using their SMTP server from the "outside": if the SMTP is called from the actual webmail client it would not use this delay. In order to test this, you could open a telnet session to port 25 and do a manual SMTP session (Search the web for SMTP HELO for examples)


GMail uses SSL for connection. To validate SSL certificate properly certain time is needed. Exact time depends on complexity of the certificate chain and use and accessibility of CRLs and OCSP checking. It's very likely that Java checks OCSPs for the certificate(s) presented, and this can be slow.

Try running your application on different system (preferably in other subnet) to exclude influence of firewalls. Maybe some part of communication (either SMTP itself, or CRL retrieval or OCSP checking) is slowed down by the firewall.


Depending on your OS, you should have a proper mail system that takes care of actually sending mails outside your network.

For instance, Postfix can do the job nicely. The configuration of Postfix would be on another thread, but typically Postfix would accept mails from your LAN (accepting to be a relay for your LAN) and would transfer the mails to gmail.

This is a exemple as to how setup postfix to have gmail as relay.

Having a mail service taking care of the relay of Gmail mails allows your Java application to be freed almost instantly (Postfix absorbs the mail, store it in its queue and forward it to Gmail itself).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜