I fixed the class responsible for sending emails. It was like (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); Transport.send(msg); System.out.println("Sent!"); } catch (Exception e) { }
During my work, I set session to null , and to my complete surprise, the class still worked fine. It doesn't matter if I pass the null constructor to MimeMessage . This is not an exception or something else. In addition, the Transport.send() method includes the following lines:
240 Session s = (msg.session != null) ? msg.session : 241 Session.getDefaultInstance(System.getProperties(), null);
So, if the session is null , it just creates a new one using the system properties. What is the purpose of creating a session object? Why MimeMessage n't MimeMessage have a default constructor if it doesn't matter what you go through there?
I looked at some examples of using javax.mail, for example: an example from Google and an example from tutorialspoint , and both of them create a session object that seems pretty useless. Why would anyone do this?
source share