简体   繁体   English

为什么JavaMail使用System.getProperties()?

[英]Why does JavaMail use System.getProperties() ?

Why do we need to add the properties like 为什么我们需要添加类似的属性

Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");


Session session = Session.getDefaultInstance(props, null);

to the system properties to send a mail. 到系统属性发送邮件。 Why should it be specifically system properties? 为什么它应该是特定的系统属性?

You don't actually need to add them to the system properties. 您实际上不需要将它们添加到系统属性中。

If you create a new Properties instance and populate it with your attributes it will still work just the same. 如果您创建一个新的Properties实例并使用您的属性填充它,它仍然可以正常工作。

As others have said, they don't need to be system properties. 正如其他人所说,他们不需要是系统属性。 But, the following may be the reason why many examples show it this way: The Java Mail package supports a large number of settings/debug options. 但是,以下可能是许多示例以这种方式显示的原因:Java Mail包支持大量设置/调试选项。 For example, https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html lists 50 different settings for the SMTP provider alone. 例如, https: //javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html仅为SMTP提供商列出了50种不同的设置。

Suppose you want to set this option: "mail.smtp.ssl.checkserveridentity". 假设您要设置此选项:“mail.smtp.ssl.checkserveridentity”。 If you use the System properties as your starting point, then you can restart your Java process with 如果使用系统属性作为起点,则可以使用以下命令重新启动Java进程

-Dmail.smtp.ssl.checkserveridentity=true 

to change the option. 更改选项。 If you build up your Properties object yourself from scratch, then you might need a code change to set the option. 如果您从头开始构建Properties对象,则可能需要更改代码来设置该选项。

They do NOT need to be System Properties. 它们不需要是系统属性。 They can be java.util.Properties . 它们可以是java.util.Properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM