Java Reference
In-Depth Information
props.put(“mail.host”, host);
props.put(“mail.transport.protocol”, “smtp”);
Session session = Session.getDefaultInstance(props, null);
The mail.host environment property specifies the default mail server. In many cases the
servers for transport and store are the same machine. However, they can be specified separately
if this is not the case. For our purposes it does not matter, because you will only need access to
the transport service.
N OTE
You will need to change the mail host in the application to use your ISP's mail host.
Using the mail.transport.protocol property tells the Session what protocol to use as the
default transport provider. You specified smtp as the default transport, so the Session now
knows that whenever you use a transport, this is the service provider you want. This becomes
important later when you actually send the message, because you use a static method in the
Transport class to send, and you never specify what type of transport you want to use.
In the next code snippet, you create a message and prepare it to be shipped. There is quite a bit
more that can take place before a message is sent, but in this case we are only interested in the
bare necessities:
String to = “YourFriend@somewhere.com”;
String from = “MeMeMe@myhost.com”;
String subject = “JavaMail Rules!”;
String messageText =
“I am sending a message using the JavaMail API.\n”
+ “I can include any text that I want.”;
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
The first thing you might notice is the use of the MimeMessage class. It implements the
Message abstract class, and uses certain criteria to make sure the message adheres to the
Internet e-mail standards. It formats the message and message headers in the proper MIME
Search WWH ::




Custom Search