Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JavaMail Transport.send() a static method?

I'm revising code I did not write that uses JavaMail, and having a little trouble understanding why the JavaMail API is designed the way it is. I have the feeling that if I understood, I could be doing a better job.

We call:

transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);

So why is Eclipse warning me that this:

transport.send(message, message.getAllRecipients());

is a call to a static method?

Why am I getting a Transport object and providing settings that are specific to it if I can't use that object to send the message? How does the Transport class even know what server and other settings to use to send the message? It's working fine, which is hard to believe. What if I had instantiated Transport objects for two different servers; how would it know which one to use?

In the course of writing this question, I've discovered that I should really be calling:

transport.sendMessage(message, message.getAllRecipients());

So what is the purpose of the static Transport.send() method? Is this just poor design, or is there a reason it is this way?

like image 619
skiphoppy Avatar asked Feb 16 '10 16:02

skiphoppy


People also ask

Which protocol is used to receive the messages in JavaMail?

IMAP. IMAP is an acronym for Internet Message Access Protocol. IMAP is an advanced protocol for receiving messages. It provides support for multiple mail box for each user ,in addition to, mailbox can be shared by multiple users.

What is JavaMail used for?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.

How does Java Mail API work?

A: The JavaMail API is a set of abstract APIs that model a mail system. The API provides a platform independent and protocol independent framework to build Java technology based email client applications. The JavaMail API provides facilities for reading and sending email.


1 Answers

Transport.send() is basically a convenience method. Yes, if you're managing your own Transport instance, call sendMessage().

I'm not sure I consider it bad design, since frequently you don't care to manage the details of sending and monitoring transport. Crack open the send() method to see what it does for you. Other ways of naming or placing this method could be marginally better.

like image 125
Sean Owen Avatar answered Oct 18 '22 09:10

Sean Owen