Sending and Receiving s

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

Topic : JavaMail API (1) Sending and Receiving s Kaster Nurmukan.
By: E. Susheel Chandar M. Guna Sekaran Intranet Mail Server.
INTRANET MAIL SERVER (DESIGN OF SMTP and POP3)
1 Electronic Mail u Three major components: u user agents u mail servers u simple mail transfer protocol: SMTP u User Agent u a.k.a. “mail reader” u composing,
JavaMail Electronic Mail Concepts JavaMail Classes Examples of Sending JavaMail JavaBeans Activation Framework Multi-part Messages Example of Sending Attachments.
J2EE JavaMail API Presented by Bartosz Sakowicz. Instalation Java Mail API: Java Activation Framework.
JavaMail JavaMail Classes Sending a Message Using JavaMail (MessageSend.java) Sending a Message to Multiple Recipients (SendToMany.java) Installing JavaMail.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 20 Sending Apr 13, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
Remote mailbox access gateway Software lab project.
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.
Chapter 30 Electronic Mail Representation & Transfer
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.
Simple Mail Transfer Protocol
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.
Mail Server Fitri Setyorini. Content SMTP POP3 How mail server works IMAP.
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.
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.
Lecturer: Maxim Podlesny Sep CSE 473 File Transfer and Electronic in Internet.
Computer Networking From LANs to WANs: Hardware, Software, and Security Chapter 12 Electronic Mail.
 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.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 8 Omar Meqdadi Department of Computer Science and Software Engineering University of.
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.
Application Layer instructors at St. Clair College in Windsor, Ontario for their slides. Special thanks to instructors at St. Clair College in Windsor,
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
JavaMail Electronic Mail Concepts JavaMail Classes
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:

Sending and Receiving Emails 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 following Protocols: SMTP POP IMAP MIME

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

SMTP

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

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.

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

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

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

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

Where to get? Both APIs can be downloaded from http://java.sun.com Also placed on \\shares\teachers\basit\shared docs\aip\APIs

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

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

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

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

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

Message Representation

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

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

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

Sending an Email (cont) Message m = new MimeMessage(s); InternetAddress to = new InternetAddress("basit@ucp.edu.pk"); InternetAddress from = new InternetAddress("bill_gates@microsoft.com", "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);

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

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

Output 0: Syed Basit <basit@ucp.edu.pk> test 1 1: basit@cnn.com de subject 2: basit@microsoft.com de subject 3: basit@dell.com de subject 4: basit@hell.com de subject 5: Bill Gates <bill_gates@microsoft.com> de subject

Let’s Try it out!!! Send email using Gmail SMTP server Set Mail content type (text/plain)

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.

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!

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

HTML Email Example MimeMessage msg = new MimeMessage(mailSession); Example of sending html message with an imbedded image using username/password authorization MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress("bill@msn.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(“tom@msn.com")); 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();

MultiPart Representation

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)

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)

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

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

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

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!!!

Email attachment Example-1 MimeMessage msg = new MimeMessage(getMailSession()); msg.setFrom(new InternetAddress("bill.gates@msn.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("larry.ellison@oracle.com")); 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");

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