简体   繁体   中英

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.

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. 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.

Suppose you want to set this option: "mail.smtp.ssl.checkserveridentity". If you use the System properties as your starting point, then you can restart your Java process with

-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.

They do NOT need to be System Properties. They can be java.util.Properties .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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