Java 邮件:会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17852664/
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-11-01 17:48:17  来源:igfitidea点击:

Java Mail: Session

javaemailimapjavamail

提问by Kuchi

Below the code used to connect and perform operations on an IMAP Folder. So my question is about the javax.mail.Sessionwhich in this case would recreate every second (depending on the sleep time and runtime of checkInbox()).

下面是用于连接 IMAP 文件夹并对其执行操作的代码。所以我的问题是关于javax.mail.Session在这种情况下每秒重新创建哪个(取决于 checkInbox() 的睡眠时间和运行时间)。

I'm sure that this is not a good solution, especially polling on IMAP is kinda stupid but I couldn't get the IMAP listenerrunning.

我确信这不是一个好的解决方案,尤其是在 IMAP 上进行轮询有点愚蠢,但我无法IMAP listener运行。

Recreating the Session not every run might be a better solution but how do I know when a session is closedor can I close it on purpose? But there is nothing like Session.close()or is the Session than NULL? Or is there some defined timeout on a Session...

并非每次运行都重新创建会话可能是更好的解决方案,但我怎么知道何时session is closed或可以故意关闭它?但是没有什么Session.close()比 NULL 或 Session 之类的了?或者会话上是否有一些定义的超时......

Source:

来源

final String port = "993";

Properties prop = new Properties();

// I assume there is some redundancy here but this didn't cause any problems so far
prop.setProperty("mail.imaps.starttls.enable", "true");
prop.setProperty("mail.imaps.port", port);

/** This part can be removed
 * prop.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
 * prop.setProperty("mail.imaps.socketFactory.port", port); 
 * prop.setProperty("mail.imaps.socketFactory.fallback", "false"); 
 */
prop.setProperty("mail.imap.ssl.enable", "true");
prop.setProperty("mail.debug", "false");

// Create a session before you loop since the configuration doesn't change
Session session = Session.getInstance(prop);

// Nearly loop forever in Prod
while(true){

    // Check the INBOX and do some other stuff
    Store store = session.getStore("imaps");
    store.connect(host, user, pw);

    // ... the operations on the session ... 

    store.close();

// Sleep a bit try & catch removed
Thread.sleep(1000);
}

All in all I have to say it's really hard to find good examples and documentation for javax.mail (besides the APIand the FAQ)

总而言之,我不得不说真的很难为 javax.mail 找到好的示例和文档(除了APIFAQ

回答by Bill Shannon

The Session just manages configuration information; there's no need to close it. As long as your configuration doesn't change, you can create the Session once at the beginning and jsut keep using it.

Session 只是管理配置信息;没有必要关闭它。只要您的配置没有改变,您就可以在开始时创建一次 Session 并继续使用它。

Connections, on the other hand, are expensive and need to be managed carefully by the application. A connection is used for the Store and for every open Folder. A connection can be closed at any time, by the server or due to a network failure. If a connection is not being actively used, you should close it.

另一方面,连接很昂贵,需要由应用程序仔细管理。连接用于存储和每个打开的文件夹。连接可以随时被服务器关闭或由于网络故障而关闭。如果一个连接没有被主动使用,你应该关闭它。

Did you find the JavaMail spec and the sample applications on the JavaMail project page? They'll help with a lot of the simple issues, but connection management is a more advanced issue.

您是否在JavaMail 项目页面上找到了 JavaMail 规范和示例应用程序?他们会帮助解决很多简单的问题,但连接管理是一个更高级的问题。

Oh, and you can remove all that socket factory stuffand make your application simpler.

哦,你可以删除所有套接字工厂的东西,让你的应用程序更简单。