javamail api

25
JAVAMAIL API By Husam Haddad

Upload: hhaddad123

Post on 13-Nov-2014

336 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JavaMail API

JAVAMAIL APIBy Husam Haddad

Page 2: JavaMail API

Outlines

Introduction to JavaMail API Overview of Related Protocols Installing the JavaMail API Reviewing the Core Classes Using the JavaMail API

Page 3: JavaMail API

Introduction to JavaMail API

The JavaMail API is an optional package (standard extension) for reading, composing, and sending electronic messages (e-mail).

Page 4: JavaMail API

Introduction to JavaMail API You use the package to create Mail User

Agent (MUA) type programs, similar to Microsoft Outlook. Its main purpose is not for transporting, delivering, and forwarding messages like Mail Transfer Agent (MTA) type programs.

In other words, users interact with MUA-type programs to read and write emails.

MUAs rely on MTAs to handle the actual delivery.

Page 5: JavaMail API

Overview of Related Protocols

Simple Mail Transfer Protocol (SMTP). Post Office Protocol version 3 (POP3). Internet Message Access Protocol version 4

(IMAP4). Multipurpose Internet Mail Extensions

(MIME).

Page 6: JavaMail API

Installing the JavaMail API

JavaMail API 1.4.2 JavaBeans Activation Framework (JAF) 1.1.1

Page 7: JavaMail API

Reviewing the Core Classes Session Message Address (Sender, Recipient) Transport Store Folder

Page 8: JavaMail API

Reviewing the Core Classes Session

Properties props = new Properties();

Session session = Session.getDefaultInstance(props);

OR

Properties props = new Properties();

Session session = Session.getInstance(props);

Page 9: JavaMail API

Reviewing the Core Classes MessageMimeMessage message = new MimeMessage(session);

message.setContent("Hello", "text/plain");

message.setText("Hello");

message.setSubject("First");

AddressAddress address = new

InternetAddress("[email protected]");

Address address = new InternetAddress("[email protected]", “Barack Obama");

Page 10: JavaMail API

Reviewing the Core Classes Sendermessage.setFrom(address)

Address address[] = ...;

message.addFrom(address);

Recipientmessage.addRecipient(type, address)

Message.RecipientType.TO Message.RecipientType.CC Message.RecipientType.BCC

Page 11: JavaMail API

Reviewing the Core ClassesAddress toAddress = new

InternetAddress("[email protected]");

Address ccAddress = newInternetAddress("[email protected]");

message.addRecipient(Message.RecipientType.TO, toAddress);

message.addRecipient(Message.RecipientType.CC, ccAddress);

Page 12: JavaMail API

Reviewing the Core Classes Transport Transport transport = session.getTransport("smtp");

transport.connect(username, password);

transport.sendMessage(message, message.getAllRecipients());

transport.close();

Page 13: JavaMail API

Reviewing the Core Classes StoreStore store = session.getStore("imap");

//Store store = session.getStore("pop3");

store.connect(host, username, password);

FolderFolder folder = store.getFolder("INBOX");

folder.open(Folder.READ_ONLY);

Message message[] = folder.getMessages();

Page 14: JavaMail API

Using the JavaMail API Sending Messages Fetching Messages Replying to Messages Forwarding Messages Sending Attachments Getting Attachments Processing HTML Messages

Sending HTML MessagesIncluding Images with Your Messages

○ Embedded○ URL

Page 15: JavaMail API

Sending Messages// Get system properties

Properties props = System.getProperties();

// Setup mail server

props.put("mail.smtp.host", host);

// Get session

Session session = Session.getDefaultInstance(props);

// Define message

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject("Hello JavaMail");

message.setText("Welcome to JavaMail");

// Send message

Transport.send(message);

Page 16: JavaMail API

Fetching Messages// Create empty properties

Properties props = new Properties();

// Get session

Session session = Session.getDefaultInstance(props);

// Get the store

Store store = session.getStore("pop3");

store.connect(host, username, password);

// Get folder

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

folder.open(Folder.READ_ONLY);

// Get directory

Message message[] = folder.getMessages();

// Close connection

folder.close(false);

store.close();

Page 17: JavaMail API

Replying to MessagesMimeMessage reply = (MimeMessage)message.reply(false);

reply.setFrom(new InternetAddress("[email protected]"));

reply.setText("Thanks");

Transport.send(reply);

Page 18: JavaMail API

Forwarding Messages// Create the message to forward

Message forward = new MimeMessage(session);

// Fill in header

forward.setSubject("Fwd: " + message.getSubject());

forward.setFrom(new InternetAddress(from));

forward.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Create your new message part

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText( "Here you go with the original message:\n\n");

// Create a multi-part to combine the parts

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

Page 19: JavaMail API

Forwarding Messages// Create and fill part for the forwarded content

messageBodyPart = new MimeBodyPart();

messageBodyPart.setDataHandler(message.getDataHandler());

// Add part to multi part

multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message

forward.setContent(multipart);

// Send message

Transport.send(forward);

Page 20: JavaMail API

Sending Attachments// Define message

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject("Hello JavaMail Attachment");

// Create the message part

BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message

messageBodyPart.setText("Pardon Ideas");

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

// Part two is attachment

messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(filename);

multipart.addBodyPart(messageBodyPart);

// Put parts in message

message.setContent(multipart);

// Send the message

Transport.send(message);

Page 21: JavaMail API

Getting AttachmentsMultipart multipart = (Multipart)message.getContent();

for (int i=0, i < multipart.getCount(); i++) {

Part part = multipart.getBodyPart(i));

String disposition = part.getDisposition();

if (disposition != null && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))) {

saveFile(part.getFileName(), part.getInputStream());

}//else

} //end of for lop

Page 22: JavaMail API

Processing HTML Messages

Sending HTML Messagesmessage.setContent("HTML Message is sent<br><h1>Hello World!</h1>",

"text/html");

Including Images with Your Messages (Embedded)// Create your new message part

BodyPart messageBodyPart = new MimeBodyPart();

String htmlText = "<H1>Hello</H1>" + "<img src=\"cid:logo\">";

messageBodyPart.setContent(htmlText, "text/html");

// Fetch the image and associate to part

DataSource dataSource= new FileDataSource(fileName);

messageBodyPart.setDataHandler(new DataHandler(dataSource));

messageBodyPart.setHeader("Content-ID","<logo>");

Page 23: JavaMail API

Processing HTML Messages

Including Images with Your Messages (URL)String htmlText = "<H1>Hello</H1><img

src=\"http://www.migfx.com/fileadmin/images/header_random/header_1.jpg\">";

message.setContent(htmlText, "text/html");

Page 24: JavaMail API

References http://java.sun.com/developer/onlineTraining/JavaMail/c

ontents.html http

://java.sun.com/products/javamail/downloads/index.html http://java.sun.com/developer/onlineTraining/JavaMail/e

xercises.html http://www.javaworld.com/javatips/jw-javatip115.html?pa

ge=1 http://www.j2ee.me/products/javamail/javadocs/index.ht

ml http://www.j2ee.me/products/javamail/FAQ.html http://

java.sun.com/products/javamail/javadocs/javax/mail/package-summary.html

http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html

Page 25: JavaMail API

Questions ?