javax.mail.Session 有什么用?

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

What is the use of javax.mail.Session?

javajavamailjavax.mail

提问by Bartek Maraszek

I was fixing a class responsible for sending emails. It looked like this (simplified):

我正在修复一个负责发送电子邮件的课程。它看起来像这样(简化):

/* ... */
Properties props = System.getProperties();
props.put("mail.smtp.host", A_VALID_IP_OF_MAIL_SERVER);
Session session = Session.getDefaultInstance(props, null);  

try {
    Message msg = new MimeMessage(session);
    /* msg.setFrom(); msg.addRecipient(); etc. */
    Transport.send(msg);
    System.out.println("Sent!");
}
catch (Exception e) { /* ... */ }
/* ... */

During my work I set sessionto nulland to my utter surprise the class still worked fine. It doesn't matter if I pass nullto MimeMessageconstructor. It doesn't throw an exception or anything. Moreover, the Transport.send()method includes the following lines:

在我的工作,我设置sessionnull和我绝对惊喜的类仍然能正常工作。如果我传递nullMimeMessage构造函数并不重要。它不会抛出异常或任何东西。此外,该Transport.send()方法包括以下几行:

240 Session s = (msg.session != null) ? msg.session : 241 Session.getDefaultInstance(System.getProperties(), null);

240 Session s = (msg.session != null) ? msg.session : 241 Session.getDefaultInstance(System.getProperties(), null);

So if the session is nullit just creates a new one using system properties. What is then the purpose of creating a Sessionobject at all? Why doesn't MimeMessagehave a default constructor if it doesn't matter what you pass in there?

因此,如果会话是,null它只是使用系统属性创建一个新会话。那么创建一个Session对象的目的是什么?MimeMessage如果传入的内容无关紧要,为什么没有默认构造函数?

I viewed a number of examples of using javax.mail, such as: example from Googleand example from tutorialspointand they both create a Sessionobject which seems pretty useless. Why would anyone do that?

我查看了许多使用 javax.mail 的示例,例如:来自 Google 的示例来自 tutorialspoint 的示例,它们都创建了一个Session看起来毫无用处的对象。为什么会有人这样做?

采纳答案by jmehrens

What is then the purpose of creating a Session object at all?

那么创建 Session 对象的目的是什么?

The session is the context of how you are going to interact with the mail host. This includes but not limited to debugging output from the mail host, timeouts, and authentication mechanisms. If you want to interact with the same mail host in different ways then the session is the object that holds this information.

会话是您将如何与邮件主机交互的上下文。这包括但不限于调试邮件主机的输出、超时和身份验证机制。如果您想以不同的方式与同一邮件主机交互,那么会话就是保存此信息的对象。

If a single JVM needs to connect to multiple mail servers you need two different sessions. This is explained in detail in the JavaMail FAQ:

如果单个 JVM 需要连接到多个邮件服务器,则需要两个不同的会话。这在JavaMail FAQ中有详细解释:

If some other code in the same JVM (e.g., in the same app server) has already created the default Session with their properties, you may end up using their Session and your properties will be ignored. This often explains why your property settings seem to be ignored. Always use Session.getInstance to avoid this problem.

如果同一 JVM 中的一些其他代码(例如,在同一应用服务器中)已经使用它们的属性创建了默认 Session,您可能最终会使用它们的 Session 并且您的属性将被忽略。这通常解释了为什么您的属性设置似乎被忽略了。始终使用 Session.getInstance 来避免此问题。

Most JavaMail examples fail the common mistakestest. Try to reference the JavaMail API sample programsSession.getDefaultInstanceis rarely the right choice for any code. Most code should use Session.getInstance. Including a default constructor for MimeMessagewould just encourage wrong behavior.

大多数 JavaMail 示例未通过常见错误测试。尝试引用JavaMail API 示例程序Session.getDefaultInstance很少是任何代码的正确选择。大多数代码应该使用Session.getInstance. 包含默认构造函数 forMimeMessage只会鼓励错误的行为。