Topic : JavaMail API (1) Sending and Receiving Emails Kaster Nurmukan.

Slides:



Advertisements
Similar presentations
Ivan Maurits. Untuk mengirim menggunakan java anda harus memiliki JavaMail API dan Java Activation Framework (JAF) yang telah terinstall dalam komputer.
Advertisements

By: E. Susheel Chandar M. Guna Sekaran Intranet Mail Server.
J2EE JavaMail API Presented by Bartosz Sakowicz. Instalation Java Mail API: Java Activation Framework.
TCP/IP Protocol Suite 1 Chapter 20 Upon completion you will be able to: Electronic Mail: SMTP, POP, and IMAP Understand four configurations of architecture.
JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail.
CPSC 441: FTP & SMTP1 Application Layer: FTP & Instructor: Carey Williamson Office: ICT Class.
Chapter 2: Application layer  2.1 Web and HTTP  2.2 FTP 2-1 Lecture 5 Application Layer.
Electronic Mail and SMTP
Java Training Introduction to Java Mail Written by Jeff Smith.
Implementing Application Protocols. Overview An application protocol facilitates communication between applications. For example, an client uses.
Technion – Israel Institute of Technology Department of Electrical Engineering Software Lab Remote Mailbox based on.NET technology Michael and Eugene Shamis.
Esimerkki: Sähköposti. Lappeenranta University of Technology / JP, PH, AH Electronic Mail Three major components: user agents mail servers simple mail.
CSCI 6962: Server-side Design and Programming Server Created .
POP Configuration Microsoft Outlook Express 6.x.
Introduction 1 Lecture 7 Application Layer (FTP, ) slides are modified from J. Kurose & K. Ross University of Nevada – Reno Computer Science & Engineering.
1 JavaMail (1) Presented by: Ke Duan Instructor: Dr. V. “Juggy” Jagannathan.
JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail.
Sending and Receiving s
Mail Server Fitri Setyorini. Content SMTP POP3 How mail server works IMAP.
POP Configuration Microsoft Outlook What is POP? Short for Post Office Protocol, a protocol used to retrieve from a mail server. Most.
» Explain the way that electronic mail ( ) works » Configure an client » Identify message components » Create and send messages.
Introduction 1-1 Chapter 2 FTP & Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012 IC322 Fall.
 ENGR 1110 Introduction to Engineering – Cyber Security Allison Holt, Adam Brown Auburn University.
E – Mail History of s What is an ? What happens when we sent an ? messages Folders Address Attachments Types of.
2: Application Layer1 Chapter 2 Application Layer These slides derived from Computer Networking: A Top Down Approach, 6 th edition. Jim Kurose, Keith Ross.
Electronic Mail (SMTP, POP, IMAP, MIME)
SMTP, POP3, IMAP.
1 Application Layer Lecture 5 Imran Ahmed University of Management & Technology.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 9
Mail Services.
CSE401N: Computer Networks Lecture-5 Electronic Mail S. M. Hasibul Haque Lecturer Dept. of CSE, BUET.
IT 424 Networks2 IT 424 Networks2 Ack.: Slides are adapted from the slides of the book: “Computer Networking” – J. Kurose, K. Ross Chapter 2: Application.
Intro to Computer Networks Bob Bradley The University of Tennessee at Martin.
Review: –How do we address “a network end-point”? –What services are provided by the Internet? –What is the network logical topology observed by a network.
Application Layer Protocols Simple Mail Transfer Protocol.
1 Computer Communication & Networks Lecture 27 Application Layer: Electronic mail and FTP Waleed.
Lecturer: Maxim Podlesny Sep CSE 473 File Transfer and Electronic in Internet.
Fall 2005 By: H. Veisi Computer networks course Olum-fonoon Babol Chapter 7 The Application Layer.
Computer Networking From LANs to WANs: Hardware, Software, and Security Chapter 12 Electronic Mail.
Electronic Mail Originally –Memo sent from one user to another Now –Memo sent to one or more mailboxes Mailbox –Destination point for messages.
 TCP/IP is the communication protocol for the Internet  TCP/IP defines how electronic devices should be connected to the Internet, and how data should.
CPT 499 Internet Skills for Educators Electronic Mail Session Five.
Simple Mail Transfer Protocol (SMTP)
Sending Topic 4, Chapters 9, 10 Network Programming Kansas State University at Salina.
1 Using Messages sent from machine to machine and stored for later reading. You will use a client to read –Type mail or pine in UNIX to read.
(or ?) Short for Electronic Mail The transmission of messages over networks.
Electronic Mail. Client Software and Mail Hosts –Client PC has client software that communicates with user’s mail host –Mail hosts deliver.
What is and How Does it Work?  Electronic mail ( ) is the most popular use of the Internet. It is a fast and inexpensive way of sending messages.
File Transfer Protocol (FTP)
Topic : JavaMail API Kaster Nurmukan. Overview of JavaMail API Javax.mail.Session Javax.mail.Authenticator and PasswordAuthentication Javax.mail.Message.
Concepts  messages are passed through the internet by using a protocol called simple mail transfer protocol.  The incoming messages are.
CS 3830 Day 9 Introduction 1-1. Announcements r Quiz #2 this Friday r Demo prog1 and prog2 together starting this Wednesday 2: Application Layer 2.
A Quick Look At How Works Understanding the basics of how works can make life a lot easier for any user. Especially those who are interested.
Slides based on Carey Williamson’s: FTP & SMTP1 File Transfer Protocol (FTP) r FTP client contacts FTP server at port 21, specifying TCP as transport protocol.
World Wide Web r Most Web pages consist of: m base HTML page, and m several referenced objects addressed by a URL r URL has two components: host name and.
COMP 431 Internet Services & Protocols
1 Kyung Hee University Chapter 22 Simple Mail Transfer Protocol (SMTP)
@Yuan Xue A special acknowledge goes to J.F Kurose and K.W. Ross Some of the slides used in this lecture are adapted from their.
© 2010 Computer Science Faculty, Kabul University FTP AND ELECTRONICE MAIL 5 TH LECTURE 4, May, 2010 Baseer Ahmad Baheer.
Spring 2006 CPE : Application Layer_ 1 Special Topics in Computer Engineering Application layer: Some of these Slides are Based on Slides.
درس مهندسی اینترنت – مهدی عمادی مهندسی اینترنت برنامه‌نویسی در اینترنت 1 SMTP, FTP.
Network Programming 10- SMTP-POP3
Networking Applications
JavaMail Electronic Mail Concepts JavaMail Classes
Instructor Materials Chapter 5 Providing Network Services
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 9
Chapter 2: Application layer
The Application Layer: SMTP, FTP
Chapter 2 Application Layer
Simplifying the Java Mail API
Presentation transcript:

Topic : JavaMail API (1) Sending and Receiving s Kaster Nurmukan

Overview of JavaMail API SMTP POP MIME javaMail API HTML With attachment

Java Mail API is an easy and standard way of sending and receiving s Java Mail API supports following Protocols: –SMTP –POP –IMAP –MIME

Simple Mail Transfer Protocol –Usually used for sending s from clients –Also used for relaying s from one server to another

Post office protocol Currently Version 3 in use (Pop3 RFC1939) Used at client side to check the s that are received in the mailbox for a user

Stands for Internet Message Access Protocol Currently IMAP4 (RFC2060) in use More advanced protocol for access that allows multiple folder management on server, periodic backups and several other advanced features.

Multi-purpose Internet Mail Extention Defines the contents that are to be transferred in an

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

As the name says, is used for sending and receiving s. Mail API requires JavaBeans Activation Framework

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

Both APIs can be downloaded from Also placed on \\shares\teachers\basit\shared docs\aip\APIs

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

You must know following classes before you start –Session –Message –Address –Transport –Store –Folder

Defines a basic mail session Everything in mail api works due to this session

Represents an 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 handling

Represents an addres Address is an abstract class javax.mail.Internet.InternetAddress is the sub-class of Address

This class speaks the protocol specific language to send messages. (Usually SMTP)

When receiving –We create a session –Connect to a store with our username and password –Get specific folders (usually inbox) –And start receiving Message objects

//Create properties object Properties p = System.getProperties(); p.put("mail.smtp.host", " "); // Get session Session s = Session.getDefaultInstance(p,null);

Message m = new MimeMessage(s); InternetAddress to = new InternetAddress from = new "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);

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

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

0: Syed Basit test 1 1: subject 2: subject 3: subject 4: subject 5: Bill Gates de subject

Send using Gmail SMTP server Set Mail content type (text/plain) Determine the Mail Server for Any Domain: –Click Start. –CMD –Type nslookup –Type set type = mx –type the name of the domain. Example: google.com

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

HTML With HTML , –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!

Mail Security n Virtually all mail servers require a username and password to receive n Some mail servers require a username and password to send an (by default, James does not). n This prevents spammers from hijacking the mail server to send unauthorized n JavaMail supports this username/password authorization and authentication n To implement this, you get a transport object from the mail session and call the connect() method with the mail host, username, and password n See next slide for code example

HTML Example n Example of sending html message with an imbedded image using username/password authorization MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new msg.addRecipient(Message.RecipientType.TO, new msg.setSubject(subject); String html = " MY SPAM <img src=' "; msg.setContent(html, "text/html"); Transport transport = mailSession.getTransport("smtp"); transport.connect("localhost","user", "passwd"); msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();

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 attachments –Constructors FileDataSource(File file) FileDataSource(String filename)

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)

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

Implements the Part interface (indirectly through a few abstract classes) Contains the content for a single part of an 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

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

attachments -1 n To append an attachment, you need to send a "multipart" message n Create your MimeMessage object as usual, setting the from address, to address, subject, etc... n Create a MimeBodyPart object for your main message and set its text (or content) to be your message n Create a MimeBodyPart object for your attachment and call its setContent() method to attach your file n Create a Multipart object and add both body parts to it. n Call your MimeMessage's setContent() method, passing in your Multipart object n Call Transport.send() to send the message n Whew!!!

attachment Example-1 MimeMessage msg = new MimeMessage(getMailSession()); msg.setFrom(new msg.addRecipient(Message.RecipientType.TO, new msg.setSubject("RE: Oracle vs SQL Server"); //Create the main message (body) part for text MimeBodyPart mainBodyPart = new MimeBodyPart(); mainBodyPart.setText("Here is my message");

attachment Example-2 //Create attachment body part MimeBodyPart 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 parts Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mainBodyPart); multipart.addBodyPart(attachBodyPart); //add the multipart to the original Mime message msg.setContent(multipart); Transport.send(msg);