Javamail Api

  • Uploaded by: hhaddad123
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Javamail Api as PDF for free.

More details

  • Words: 752
  • Pages: 25
By Husam Haddad

JAVAMAIL API

Outlines  Introduction

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

Introduction to JavaMail API

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

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.

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).

Installing the JavaMail API

 JavaMail API

1.4.2  JavaBeans Activation Framework (JAF) 1.1.1

Reviewing the Core Classes

 Session

 Message  Address

(Sender, Recipient)  Transport  Store  Folder

Reviewing the Core Classes

 Session

Properties props = new Properties(); Session session = Session.getDefaultInstance(props);

OR Properties props = new Properties(); Session session = Session.getInstance(props);

Reviewing the Core Classes  Message MimeMessage message = new MimeMessage(session); message.setContent("Hello", "text/plain"); message.setText("Hello"); message.setSubject("First");

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

Reviewing the Core Classes

 Sender

message.setFrom(address) Address address[] = ...; message.addFrom(address);



Recipient

message.addRecipient(type, address)  Message.RecipientType.TO  Message.RecipientType.CC  Message.RecipientType.BCC

Reviewing the Core Classes

Address toAddress = new InternetAddress("[email protected]"); Address ccAddress = newInternetAddress("[email protected]"); message.addRecipient(Message.RecipientType.TO, toAddress); message.addRecipient(Message.RecipientType.CC, ccAddress);

Reviewing the Core Classes

 Transport

Transport transport = session.getTransport("smtp"); transport.connect(username, password); transport.sendMessage(message, message.getAllRecipients()); transport.close();

Reviewing the Core Classes

 Store

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

 Folder Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message message[] = folder.getMessages();

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

Sending HTML Messages Including Images with Your Messages ○ Embedded ○ URL

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);

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();

Replying to Messages MimeMessage reply = (MimeMessage)message.reply(false); reply.setFrom(new InternetAddress("[email protected]")); reply.setText("Thanks"); Transport.send(reply);

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);

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);

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);

Getting Attachments Multipart 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

Processing HTML Messages 

Sending HTML Messages

message.setContent("HTML Message is sent

Hello World!

", "text/html");



Including Images with Your Messages (Embedded)

// Create your new message part BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "

Hello

" + ""; 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","");

Processing HTML Messages 

Including Images with Your Messages (URL)

String htmlText = "

Hello

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

References        

http://java.sun.com/developer/onlineTraining/JavaMail/contents.htm http://java.sun.com/products/javamail/downloads/index.html http://java.sun.com/developer/onlineTraining/JavaMail/exercises.htm http://www.javaworld.com/javatips/jw-javatip115.html?page=1 http://www.j2ee.me/products/javamail/javadocs/index.html http://www.j2ee.me/products/javamail/FAQ.html http:// java.sun.com/products/javamail/javadocs/javax/mail/package-summ http:// java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/packa

Questions ?

Related Documents

Api Javamail
June 2020 4
Javamail Api
May 2020 2
Api
April 2020 43
Api
July 2020 25
Api
November 2019 64

More Documents from ""

Javamail Api
May 2020 2