Sei sulla pagina 1di 31

Java Mail Siddhartha Bhattacharya

JavaMail Overview

Contents:
Javamail API Related Protocols Javamail API Core Classes Javamail Client Service Using the Javamail API

SAP AG 2004 / Siddhartha Bhattacharya / Java Mail

JavaMail: Unit Objectives

After completing this unit, you will be able to:

Understand the Javamail API


Understand different protocols related to sending and receiving of mails Understand the Javamail API Core Classes Understand the Javamail Client Service 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: Understand the Javamail API

SAP AG 2004 / Siddhartha Bhattacharya / Java Mail

JavaMail API

Javamail is an API for reading, composing and sending electronic messages (e-mails). 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: Understand the Javamail API

SAP AG 2004 / Siddhartha Bhattacharya / Java Mail

JavaMail: Topic Objectives

After completing the next session, you will be able to: 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: 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: 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

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

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

JavaMail: Topic Summary

You should now be able to: 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: 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

JavaMail: Topic Summary

You should now be able to: 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: 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

Programming model : Getting SMTP Host and Sender


Getting the SMTP Host and Sender
Retrieve the properties SMTP Host and Senders 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

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

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: Understand how send a message

SAP AG 2004 / Siddhartha Bhattacharya / Java Mail

JavaMail: Unit Summary

You should now be able to: Understand the Javamail API Understand different protocols related to sending and receiving of mails Understand the Javamail Client Service Understand how send a message

SAP AG 2004 / Siddhartha Bhattacharya / Java Mail

Potrebbero piacerti anche