Java Reference
In-Depth Information
Discussion
A real email client allows the user considerably more control. Of course, it also requires
more work. In this recipe, I'll build a simple version of a mail sender, relying upon the
JavaMail API. This first example shows the steps of sending mail over SMTP, the standard
Internet mail protocol. The steps are listed in the sidebar.
IAN'S BASIC STEPS: SENDING EMAIL OVER SMTP
Here are the steps for sending email over SMTP:
1. Create a java.util.Properties object (see Like an Array, but More Dynamic ) to pass
information about the mail server, because the JavaMail API allows room for many set-
tings.
2. Load the Properties with at least the hostname of the SMTP mail server.
3. Create a Session object.
4. Create a Message from the Session object.
5. Set the From, To, CC addresses, and Subject in the Message .
6. Set the message text into the message body.
7. Finally, use the static method Transport.send() to send the message!
This API requires that you catch the MessagingException , which indicates some failure of
the transmission. Class Sender is shown in Example 17-3 .
Example 17-3. Sender.java
public
public class
class Sender
Sender {
/** The message recipient. */
protected
protected String message_recip = "spam-magnet@darwinsys.com" ;
/* What's it all about, Alfie? */
protected
protected String message_subject = "Re: your mail" ;
/** The message CC recipient. */
protected
protected String message_cc = "nobody@erewhon.com" ;
/** The message body */
protected
protected String message_body =
Search WWH ::




Custom Search