Java Reference
In-Depth Information
Given the multitude of protocols and the difficulties of low-level programming, the
Java language provides the JavaMail API in order to simplify sending and retrieving
e-mails regardless of the underlying protocols. But the JavaMail API is not enough;
because it was designed to handle the transmission aspect of the message (connec-
tion parameter, source, destination, subject, and so on), the body of the message is
managed by JavaBeans Activation Framework ( JAF framework ). That is why, in
addition to the mail.jar library, you have to import the activation.jar library.
Sending an e-mail via the SMTP protocol
Sending an e-mail using JavaMail is done as follows:
1. Obtain the session object. This object encapsulates various information
such as the address of the mail server. The following code shows how to get
an object of type Session :
Properties prop = System.getProperties();
//serveurAddress is the host of you mail
server
prop.put("mail.smtp.host",
serveurAddress);
Session session =
Session.getDefaultInstance(prop,null);
2. Construct the message. To send an e-mail, it is necessary to define some
parameters such as the content of the e-mail, the sender, and destination. In
addition to these settings, you may need to specify the subject of your e-mail
anditsheader.Allthisismadepossiblethroughthe MimeMessage classthat
offers several methods to construct a message for a given session. The fol-
lowing code shows how to get an object of type MimeMessage and build a
mail to send:
Message msg = new MimeMessage(session);
msg.setFrom(new
InternetAddress("xxx-university@yahoo.fr"));
InternetAddress[] internetAddresses = new
InternetAddress[1];
internetAddresses[0] = new
Search WWH ::




Custom Search