Java 邮件超时和连接超时处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2773108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:45:20  来源:igfitidea点击:

Java Mail timeout & connectiontimeout handling

javaexceptionexception-handlingemailjavamail

提问by Gnanam

I'm using JavaMail to send email requests to an SMTP server.

我正在使用 JavaMail 向 SMTP 服务器发送电子邮件请求。

I would like to set both "mail.smtp.connectiontimeout"and "mail.smtp.timeout"properties within my code.

我想在我的代码中设置“mail.smtp.connectiontimeout”“mail.smtp.timeout”属性。

Programmatically, I want to catch both when timeoutand/or connectiontimeoutoperations are reached in Java and handle things accordingly. Handling in the sense, I need to retry the same email once again the next time.

以编程方式,我想在 Java 中达到超时和/或连接超时操作时捕获并相应地处理事情。从某种意义上说,下次我需要再次重试同一封电子邮件。

How do I handle this in Java/JavaMail? Is it possible to catch & handle this timeout operations?

我如何在 Java/JavaMail 中处理这个问题?是否可以捕获并处理此超时操作?

EDIT

编辑

Also, is it possible to simulate/reproduce this timeout operation on my own assuming I've complete administration access to the SMTP server?

另外,假设我对 SMTP 服务器有完整的管理访问权限,是否可以自己模拟/重现此超时操作?

采纳答案by Janning

Answering your second question: On your test machine just DROP all outgoing connections to your SMTP Server with iptables:

回答您的第二个问题:在您的测试机器上,只需使用 iptables DROP 到您的 SMTP 服务器的所有传出连接:

   iptables -I OUTPUT 1 -p tcp -s 192.168.1.20 --dport 25 -j DROP

This way it does look like an unresponsive smtp server and you can test your exception handling.

这样它看起来就像一个没有响应的 smtp 服务器,你可以测试你的异常处理。

回答by Gnanam

All:

全部:

Am answering my question, after experiencing this on my own.

在我自己经历了这个之后,我正在回答我的问题。

How do I handle this in Java/JavaMail? Is it possible to catch & handle this timeout operations?

我如何在 Java/JavaMail 中处理这个问题?是否可以捕获并处理此超时操作?

Yes, it is automatically thrown as javax.mail.MessagingException.

是的,它会自动作为javax.mail.MessagingException抛出。

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)

This exception is thrown exactly at this line:

这个异常正是在这一行抛出:

Transport.connect();

运输.connect();

Only open question I've now is "Is it possible to simulate/reproduce this timeout operation on my own assuming I've complete administration access to the SMTP server?"

我现在唯一的悬而未决的问题是“假设我拥有对 SMTP 服务器的完整管理访问权限,是否可以自己模拟/重现此超时操作?

Any ideas from experts?

来自专家的任何想法?