+ All Categories
Home > Documents > Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of...

Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of...

Date post: 23-Dec-2015
Category:
Upload: rosamond-wilkinson
View: 260 times
Download: 1 times
Share this document with a friend
Popular Tags:
38
Java Mail API Java Mail API Sending and Receiving Sending and Receiving Emails Emails
Transcript
Page 1: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Java Mail APIJava Mail APIJava Mail APIJava Mail API

Sending and Receiving EmailsSending and Receiving Emails

Page 2: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Mail API• Java Mail API is an easy and standard

way of sending and receiving emails• Java Mail API supports following

Protocols:– SMTP– POP– IMAP– MIME

Page 3: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

SMTP• Simple Mail Transfer Protocol

– Usually used for sending emails from clients

– Also used for relaying emails from one server to another

Page 4: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

SMTP

Page 5: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

POP• Post office protocol• Currently Version 3 in use (Pop3

RFC1939)• Used at client side to check the

emails that are received in the mailbox for a user

Page 6: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

IMAP• Stands for Internet Message Access

Protocol• Currently IMAP4 (RFC2060) in use• More advanced protocol for email

access that allows multiple folder management on server, periodic backups and several other advanced features.

Page 7: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

MIME• Multi-purpose Internet Mail

Extention• Defines the contents that are to be

transferred in an email

Page 8: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

What do you need to use Java Mail?

• You should have following two APIs– Java Mail API– JavaBeans Activation Framework(JAF)– JAF is included in JDK6

Page 9: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Mail API• As the name says, is used for

sending and receiving emails.• Mail API requires JavaBeans

Activation Framework

Page 10: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

JAF• JavaBeans Activation Framework

– Helps programmers to Determine the type of an arbitrary piece of data

– Encapsulates access to it– Discovers operations that can be

performed on it

Page 11: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Where to get?• Both APIs can be downloaded from

http://java.sun.com• Also placed on \\shares\teachers\

basit\shared docs\aip\APIs

Page 12: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

How to Install?• Un-zip the javamail.zip and jaf.zip

into some folder• Put mail.jar from javamail folder to

the classpath• Put activation.jar from jaf folder to

classpath

Page 13: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Java Mail classes• You must know following classes

before you start– Session– Message– Address– Transport– Store– Folder

Page 14: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.mail.Session• Defines a basic mail session• Everything in mail api works due

to this session

Page 15: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.mail.Message• Represents an email message that is

either created to be sent to the recipient or is received from someone.

• Message is an Abstract class• MimeMessage is the sub-class of

message that understands most MIME types and is most commonly used for email handling

Page 16: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.mail.Address• Represents an email addres• Address is an abstract class• javax.mail.Internet.InternetAddres

s is the sub-class of Address

Page 17: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Message Representation

MimeMessage

Headers

Session

Address[] To Address[] From

String subject Date sentDate

Message Content: String text

Properties

Page 18: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Transport• This class speaks the protocol

specific language to send messages. (Usually SMTP)

Page 19: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Store and Folder• When receiving email

– We create a session– Connect to a store with our username

and password– Get specific folders (usually inbox)– And start receiving Message objects

Page 20: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Sending an Email

//Create properties objectProperties p = System.getProperties();p.put("mail.smtp.host", "202.125.140.71");

// Get sessionSession s = Session.getDefaultInstance(p,null);

Page 21: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Sending an Email (cont)

Message m = new MimeMessage(s);

InternetAddress to = new InternetAddress("[email protected]");InternetAddress from = new

InternetAddress("[email protected]", "Bill Gates");

m.setContent("yeah this is the body", "text/plain");m.setFrom(from);m.setRecipient(Message.RecipientType.TO, to);m.setSubject("de subject");

Transport.send(m);

Page 22: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Checking MailsProperties props = new Properties();Session session =

Session.getDefaultInstance(props, null);

Store store = session.getStore("pop3");store.connect(host, username, password);

Folder folder = store.getFolder("INBOX");folder.open(Folder.READ_ONLY);

Page 23: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Checking Mails(cont)Message message[] = folder.getMessages();

for (int i=0, n=message.length; i<n; i++) { System.out.println(i + ": " +

message[i].getFrom()[0] + "\t\t" + message[i].getSubject());}

folder.close(false);store.close();

Page 24: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Output0: Syed Basit <[email protected]> test 11: [email protected] de subject 2: [email protected] de subject3: [email protected] de subject 4: [email protected] de subject 5: Bill Gates <[email protected]> de subject

Page 25: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Let’s Try it out!!!• Send email using Gmail SMTP

server• Set Mail content type (text/plain)

Page 26: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

HTML Email

• You can also send HTML email with JavaMail. HTML email can be used to– Use different size fonts– imbed images into your email– Use different colored text, bold, italic, etc.

Page 27: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

HTML Email

• With HTML email,– you set the mime message content

type to "text/html" – call the setContent() method to set

your html content• It helps to know a little HTML!

Page 28: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Mail Security Virtually all mail servers require a username and password to receive

email Some mail servers require a username and password to send an

email (by default, James does not). This prevents spammers from hijacking the mail server to send

unauthorized email JavaMail supports this username/password authorization and

authentication To implement this, you get a transport object from the mail

session and call the connect() method with the mail host, username, and password

See next slide for code example

Page 29: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

HTML Email Example Example of sending html message with an imbedded image using

username/password authorization

MimeMessage msg = new MimeMessage(mailSession);msg.setFrom(new InternetAddress("[email protected]"));msg.addRecipient(Message.RecipientType.TO, new InternetAddress(“[email protected]"));msg.setSubject(subject);String html = "<html><body><b>MY SPAM</b><br><img src='http://www.wrfportal.org/images/NOAA_logo.jpg'> </body></html>";msg.setContent(html, "text/html");Transport transport = mailSession.getTransport("smtp");transport.connect("localhost","user", "passwd");msg.saveChanges();transport.sendMessage(msg, msg.getAllRecipients());transport.close();

Page 30: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

MultiPart Representation

MimeMessage Headers Session

Message Content: Multipart MimeBodyPart MimeBodyPart

String text

DataHandler

FileDataSource

File

String fileName

Page 31: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.Activation.DataSource

• Interface that allows access to file type and to streams that can manipulate the file

• public String getContentType() returns the name of the MIME file type

• Implemented by javax.Activation.FileDataSource

• Used by JavaMail to create and retrieve e-mail attachments– Constructors

• FileDataSource(File file)• FileDataSource(String filename)

Page 32: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.Activation.DataHandler

• Wrapper for DataSource objects so that the user does not need to manipulate the bytes for each file

• Constructors– DataHandler(DataSource ds)– DataHandler(Object obj, String mimeType)

• Public Object getContent() Returns the data as the object that represents its content type (ie runing this method on a text message returns a String)

Page 33: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.mail.Part Revisited

• Allows manipulation of DataHandlers– public void setDataHandler(DataHandler

dh)– public DataHandler getDataHandler()

• Other methods abstract user away from need to directly manipulate DataHandler– public void setContent(Object object, String

contentType)– public Object getContent()

Page 34: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

javax.mail.MimeBodyPart• Implements the Part interface (indirectly through a

few abstract classes)• Contains the content for a single part of an e-mail

message• Uses several methods to manipulate content

directly or through DataHandler or streams• Key Methods

– public void setText(String text): for text/plain content, makes a String into the message content

– public void setDataHandler(DataHandler dh) sets the content using a DataHandler (which may be text or any other permitted content)

– public void setFileName(String filename) sets the filename associated with the content, if the content represents a file

Page 35: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Javax.mail.Multipart• Container that holds multiple parts• Each part is indexed, starting with 0• A Multipart object may be a part within

another Multipart object• Key Methods

– public void addBodyPart(BodyPart part)– public void addBodyPart(BodyPart part, int index)– public int getCount() returns the number of BodyPart

objects

Page 36: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Email attachments -1 To append an email attachment, you need to send a "multipart"

message Create your MimeMessage object as usual, setting the from

address, to address, subject, etc... Create a MimeBodyPart object for your main message and set its

text (or content) to be your message Create a MimeBodyPart object for your attachment and call its

setContent() method to attach your file Create a Multipart object and add both body parts to it. Call your MimeMessage's setContent() method, passing in your

Multipart object Call Transport.send() to send the message

Whew!!!

Page 37: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Email attachment Example-1

MimeMessage msg = new MimeMessage(getMailSession());msg.setFrom(new

InternetAddress("[email protected]"));msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));msg.setSubject("RE: Oracle vs SQL Server");

//Create the main message (body) part for textMimeBodyPart mainBodyPart = new MimeBodyPart();mainBodyPart.setText("Here is my message");

Page 38: Java Mail API Sending and Receiving Emails. Mail API Java Mail API is an easy and standard way of sending and receiving emails Java Mail API supports.

Email attachment Example-2

//Create attachment body partMimeBodyPart attachBodyPart = new

MimeBodyPart();DataSource source = new FileDataSource("1.jpg");attachBodyPart.setDataHandler(new

DataHandler(source));attachBodyPart.setFileName("1.jpg");

//Now create the multipart and add the partsMultipart multipart = new MimeMultipart();multipart.addBodyPart(mainBodyPart);multipart.addBodyPart(attachBodyPart);

//add the multipart to the original Mime messagemsg.setContent(multipart);

Transport.send(msg);


Recommended