03 Javamail Sendmail SOL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Javamail API, Solutions

Unit: Javamail
Topic: Send mail using the Javamail API

At the conclusion of this exercise, you will be able to:


• Send an e-mail using the Javamail API.

1. Development Objectives
Creating a J2EE Project and use the Javamail API to send an e-mail message. The
J2EE Project will contain an EAR application and a WAR application. The Web
Application will have a Servlet which will make use of the Javamail API Classes
to send an e-mail message to a SMTP Server. The Web Project also contains a
HTML form used to get the user input for receiver’s e-mail address, Subject and
Message Text.

2. Result
As a result of the exercise you will be able to compose and send an e-mail
message.

3. Prerequisites
3-1 You have configured the Java mail client service tab in the Visual
Administrator and have set the SMTP Server and sender’s details. (It is
necessary to restart the server once the java mail client service properties
are changed)
3-2 You have launched the Netweaver Developer Studio.
3-3 You have selected the J2EE perspective.

4. Overview Developing

For your convenience, you can start


developing with a predefined Project.
The graphics on the left shows the
J2EE Enterprise Application Project
(EAR) and Web Application Project
(WAR) imported into the Developer
Studio Workplace.

JavamailProjectEAR
The EAR Project which
contains the web project.
JavamailProjectWAR
The WAR Project which
contains the Servlet which uses the
Javamail API.
Javamail
The Servlet used for sending e-
mails using the Javamail API classes.
SendMail.html
HTML form to compose a
message. This form posts its data to
the Javamail Servlet.

4. Complete the Servlet code


4-1 Setup Project Build Path
Right Click on JavamailProjectWAR -> Properties ->Java Build Path-> ->
Add variable -> Select TSSAP_JAVAX_HOME -> Extend -> Choose
mail.jar
Add the following code in the doGet method of the Servlet within the try catch
block.
The variable ctx is of type InitialContext and out is of type PrintWriter.
4-2 Obtain the JNDI Context
//Step 1. Create an initialcontext
ctx = new InitialContext();

4-3 Lookup the Javamail Session


//Step 2. Lookup the JNDI name for resource type
//javax.mail.Session
Session session = (Session)
ctx.lookup("java:comp/env/mail/Email");

4-4 Check the SMTP Server and Sender’s details


/* Step 3. Retreive the properties set in the MailSession
mail.smtp.host contains the SMTP Mail Server name
mail.from contains the Sender's e-mail id
**/
Properties props = session.getProperties();
out.println ( "Mail from >> " +
props.getProperty("mail.from"));
out.println ( "SMTP Server >> " +
props.getProperty("mail.smtp.host"));
4-5 Create a Message and set the details
// Step 4.Create a new MimeMessage object (using the Session
// created above)
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress
(props.getProperty("mail.from")));

message.setRecipients(Message.RecipientType.TO, new
InternetAddress[] { new
InternetAddress(request.getParameter("Recipient")) });

message.setSubject(request.getParameter("Subject"));
message.setContent(request.getParameter("Message"),
"text/plain");
4-6 Send the message
// Step 5. Send the message

Transport.send(message);

out.println("<br><b>Thank you. Your message to " +


request.getParameter("Recipient") + " was successfully
sent.</b>");

5. Deploying the Application


5-1 Right Click on the JavamailProjectWAR Project and “Choose Build Web
Archive”.
5-2 Right Click on the JavamailProjectEAR Project and “Choose Build
Application Archive”.
5-3 Right Click on the JavamailProjectEAR.ear file and “Choose Deploy to J2EE
Engine”

6. Testing the Application


Open Web Browser and Navigate to this URL http://localhost:<http-
port>/Javamail/SendMail.html
Enter the details on the form and hit Ok to send the mail.

You might also like