Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

They do NOT need to be System Properties. They can be java.util.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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top