Java Mail
Siddhartha Bhattacharya
JavaMail Overview
Contents:
z Javamail API
z Related Protocols
z Javamail API Core Classes
z Javamail Client Service
z Using the Javamail API
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Unit Objectives
After completing this unit, you will be able to:
z Understand the Javamail API
z Understand different protocols related to sending
and receiving of mails
z Understand the Javamail API Core Classes
z Understand the Javamail Client Service
z Understand how send a message
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Objectives
After completing the next session, you will be able to:
z Understand the Javamail API
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail API
z Javamail is an API for reading, composing and sending
electronic messages (e-mails).
z Javamail API is designed to provide protocol-independent
access for sending and receiving mails.
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Summary
You should now be able to:
z Understand the Javamail API
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Objectives
After completing the next session, you will be able to:
z Understand different protocols related to sending
and receiving of mails
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Procotols
Some of the protocols used with the
Javamail API -
SMTP (Simple Mail Transfer Protocol)
POP (Post Office Protocol)
IMAP (Internet Message Access Protocol)
NNTP (Network News Transfer Protocol)
MIME (Multipurpose Internet Mail Extensions)
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
SMTP (Simple Mail Transfer Protocol)
What is the Simple Mail Transfer Protocol?
Mechanism of delivery of email
Routes messages through the internet to a mail server
Messages sent over the internet using SMTP can be
retrieved using either POP or IMAP
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
POP (Post Office Protocol)
What is the Post Office Protocol?
Mechanism for users to get their mails
Supports single mailbox for each user
Messages are downloaded by client email programs and
deleted from the server
Currently in version 3 and often called POP3
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
IMAP (Internet Message Access Protocol)
What is the Internet Message Access Protocol?
More advanced protocol for receiving messages stored on
a mail server.
Permits client email programs to access remote message
stores as if they were local.
Permits users to access messages from more than one
machine.
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
MIME (Multipart Internet Mail Extensions)
What is Multipart Internet Mail Extensions?
A specification for formatting non ASCII messages
Defines the content and format of what is transferred.
Many email client programs support MIME which enables
them to send audio, video and graphics files.
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
NNTP (Network News Transfer Protocol)
What is the Network News Transfer Protocol?
The protocol used to send, distribute, and retrieve USENET
messages.
USENET is a bulletin board service containing forums called
newsgroups that cover various interest groups
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Summary
You should now be able to:
z Understand different protocols related to sending
and receiving of mails
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Objectives
After completing the next session, you will be able to:
z Understand the Javamail API Core Classes
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail API: Package
Package
javax.mail
javax.mail.internet
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Once you have your message, you can set its parts, as Message implements the Part interface (with
MimeMessage implementing MimePart). The basic mechanism to set the content is the setContent()
method, with arguments for the content and the mime type:
message.setContent("Hello", "text/plain"); If, however, you know you are working with a MimeMessage
and your message is plain text, you can use its setText() method which only requires the actual content,
defaulting to the MIME type of text/plain:
message.setText("Hello");
JavaMail API: Core Classes
Core Classes
javax.mail.Session defines a basic mail Session. Uses the
java.util Properties Object to get information like mail server,
sender details, username, password etc.
javax.mail.internet.MimeMessage an email Message that
understands MIME types and headers.
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Once you have your message, you can set its parts, as Message implements the Part interface (with
MimeMessage implementing MimePart). The basic mechanism to set the content is the setContent()
method, with arguments for the content and the mime type:
message.setContent("Hello", "text/plain"); If, however, you know you are working with a MimeMessage
and your message is plain text, you can use its setText() method which only requires the actual content,
defaulting to the MIME type of text/plain:
message.setText("Hello");
JavaMail API: Core Classes
javax.mail.Address Used to specify email addresses
javax.mail.Authenticator Used to access protected resources
(mail server) via a username and password.
javax.mail.Transport This class speaks the protocol-specific
language for sending the message (usually SMTP).
javax.mail.Store and javax.mail.Folder Use Store to specify
which protocol (Ex. POP3) to use and Folder to read messages
(Ex. INBOX)
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Authenticator
To use the Authenticator, you subclass the abstract class and return a PasswordAuthentication instance
from the getPasswordAuthentication() method. You must register the Authenticator with the session when
created. Then, your Authenticator will be notified when authentication is necessary. You could popup a
window or read the username and password from a configuration file (though if not encrypted it is not
secure), returning them to the caller as a PasswordAuthentication object
Transport
Use the default version of the class by just calling the static send() method:
Transport.send(message);
Store and Folder Æ Used to READ MAILS
JavaMail: Topic Summary
You should now be able to:
z Understand the Javamail API Core Classes
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Objectives
After completing the next session, you will be able to:
z Understand the Java Mail Client Service
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Java Mail Client Service
The Java Mail Client Service …
Adds e-mail and news capabilities to applications deployed
on the SAP J2EE Engine.
Provides APIs for access to SMTP, POP3, IMAP4, and NNTP
mail servers, for transporting, storing, and accessing a
message or news on Internet or your network
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Java Mail Client Service
The Java Mail Client Service …
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
The values mail.from and mail.smtp.host can be retrieved in the code by looking up the mail Session
properties.
JavaMail: Topic Summary
You should now be able to:
z Understand the Javamail Client Service
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Objectives
After completing the next session, you will be able to:
z Understand how send a message
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Javamail: Sending a mail
Steps involved in sending a mail :
Create a mail Session providing the server details
Create the message to send
Provide the sender, receiver addresses
Send the message
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
Programming Model : Getting a Session
Getting a Session
Assume that the name “mail/Email” is already refined in the web.xml
deployment descriptor. The first step is to lookup the Session by this JNDI
name.
//Step 1. Create an initialcontext
ctx = new InitialContext();
//Step 2. Lookup the JNDI name for resource type
//javax.mail.Session
Session session =
(Session)ctx.lookup("java:comp/env/mail/Email");
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
The resource mail/Email is of type javax.mail.Session and is defined in the web.xml deployment
descriptor.
Programming model : Getting SMTP Host and Sender
Getting the SMTP Host and Sender
Retrieve the properties SMTP Host and Sender’s details from the Session.
/* 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"));
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
The values “mail.from” and “mail.smtp.host” are defined in the Java Mail Client Service tab in Visual
Administrator.
Programming model : Creating a Message
Creating a Message
Use the “mail.from” from the Session property.
Use the Receipient Details “Recipient”), Subject of the mail (“Subject”)
and the Message text (“Message”) from the form parameters.
// 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");
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
The Servlet uses the values posted from the HTML Page SendMail.html.
Programming model : Sending a Message
Sending a Message
Send the message to the SMTP Server.
// Step 5. Send the message
Transport.send(message);
out.println(“<br><b>Thank you. Your message to " +
request.getParameter("Recipient") + " was successfully
sent.</b>");
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Topic Summary
You should now be able to:
z Understand how send a message
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail
JavaMail: Unit Summary
You should now be able to:
z Understand the Javamail API
z Understand different protocols related to sending
and receiving of mails
z Understand the Javamail Client Service
z Understand how send a message
© SAP AG 2004 / Siddhartha Bhattacharya / Java Mail