Question

So can someone tell me how does Java behave when Transport.send() is provided with a NULL Session object? I see this error with Java Mail API, wherein an EMail is sent with a NULL MailSession object.

//Some code here
Session mailSession = null;

mailSession = getMailSession(); //This returns NULL
MimeMessage message = new MimeMessage(mailSession);

// some more code here to set FROM/To/Subject/content

Transport.send(message); //This still fires mail?!

P.S: Why am I not happy when it still works? Why bother? Well -actually the mail session I am establishing is with a James server. Since session is not established with James's JNDI, the server somehow sends the email to the "outside world" to the actual user id. This is test environment and is not intended to send email to a live user.

Was it helpful?

Solution

From the Java Mail source code:

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

If the message session is null then it creates a default session using the System.getProperties()

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