Electronic Mail outgoing message queue user mailbox Three
Electronic Mail outgoing message queue user mailbox Three major components: • user agents • mail servers user agent mail server SMTP • simple mail transfer protocol: SMTP User Agent SMTP • a. k. a. “mail reader” • composing, editing, reading mail messages SMTP mail server • e. g. , Eudora, Outlook, elm, Netscape Messenger • outgoing, incoming messages stored on server user agent mail server user agent
Electronic Mail: mail servers user agent Mail Servers • mailbox contains incoming messages for user mail server SMTP • message queue of outgoing (to be sent) mail messages • SMTP protocol between mail servers SMTP to send email messages • client: sending mail server • “server”: receiving mail server SMTP mail server user agent mail server user agent
Electronic Mail: SMTP [RFC 2821] • uses TCP to reliably transfer email message from client to server, port 25 • direct transfer: sending server to receiving server • three phases of transfer • handshaking (greeting) • transfer of messages • closure • command/response interaction • commands: ASCII text • response: status code and phrase • messages must be in 7 -bit ASCII
Scenario: Alice sends message to Bob 1) Alice uses UA to compose message and “to” bob@someschool. edu 4) SMTP client sends Alice’s message 2) Alice’s UA sends message to her 5) Bob’s mail server places the mail server; message placed in message in Bob’s mailbox over the TCP connection message queue 6) Bob invokes his user agent to read message 3) Client side of SMTP opens TCP connection with Bob’s mail server 1 user agent 2 mail server 3 mail server 4 5 6 user agent
Sample SMTP interaction S: 220 hamburger. edu C: HELO crepes. fr S: 250 Hello crepes. fr, pleased to meet you C: MAIL FROM: <alice@crepes. fr> S: 250 alice@crepes. fr. . . Sender ok C: RCPT TO: <bob@hamburger. edu> S: 250 bob@hamburger. edu. . . Recipient ok C: DATA S: 354 Enter mail, end with ". " on a line by itself C: Do you like ketchup? C: How about pickles? C: . S: 250 Message accepted for delivery C: QUIT S: 221 hamburger. edu closing connection
Try SMTP interaction for yourself: • telnet servername 25 • see 220 reply from server • enter HELO, MAIL FROM, RCPT TO, DATA, QUIT commands above lets you send email without using email client (reader)
SMTP: final words • SMTP uses persistent connections • SMTP requires message (header & body) to be in 7 -bit ASCII • SMTP server uses CRLF to determine end of message Comparison with HTTP: • HTTP: pull • SMTP: push • both have ASCII command/response interaction, status codes • HTTP: each object encapsulated in its own response msg • SMTP: multiple objects sent in multipart msg
Mail message format SMTP: protocol for exchanging email header msgs RFC 822: standard for text message format: • header lines, e. g. , body • To: • From: • Subject: different from SMTP commands! • body • the “message”, ASCII characters only 2 blank line
Message format: multimedia extensions • MIME: multimedia mail extension, RFC 2045, 2056 • additional lines in msg header declare MIME content type MIME version method used to encode data multimedia data type, subtype, parameter declaration encoded data From: alice@crepes. fr To: bob@hamburger. edu Subject: Picture of yummy crepe. MIME-Version: 1. 0 Content-Transfer-Encoding: base 64 Content-Type: image/jpeg base 64 encoded data. . . . . base 64 encoded data
MIME types Content-Type: type/subtype; parameters Text Video • example subtypes: plain, html • example subtypes: mpeg, quicktime Image • example subtypes: jpeg, gif Application • other data that must be processed by reader before “viewable” Audio • exampe subtypes: basic (8 -bit mulaw encoded), 32 kadpcm (32 kbps coding) • example subtypes: msword, octet -stream
Multipart Type From: alice@crepes. fr To: bob@hamburger. edu Subject: Picture of yummy crepe. MIME-Version: 1. 0 Content-Type: multipart/mixed; boundary=Start. Of. Next. Part --Start. Of. Next. Part Dear Bob, Please find a picture of a crepe. --Start. Of. Next. Part Content-Transfer-Encoding: base 64 Content-Type: image/jpeg base 64 encoded data. . . . . base 64 encoded data --Start. Of. Next. Part Do you want the reciple?
Mail access protocols SMTP user agent sender’s mail server access protocol receiver’s mail server • SMTP: delivery/storage to receiver’s server • Mail access protocol: retrieval from server • POP: Post Office Protocol [RFC 1939] • authorization (agent <-->server) and download • IMAP: Internet Mail Access Protocol [RFC 1730] • more features (more complex) • manipulation of stored msgs on server • HTTP: Hotmail , Yahoo! Mail, etc. user agent
POP 3 protocol authorization phase • client commands: • user: declare username • pass: password • server responses • +OK • -ERR transaction phase, client: • list: list message numbers • retr: retrieve message by number • dele: delete • quit S: C: S: +OK POP 3 server ready user bob +OK pass hungry +OK user successfully logged C: list S: 1 498 S: 2 912 S: . C: retr 1 S: <message 1 contents> S: . C: dele 1 C: retr 2 S: <message 1 contents> S: . C: dele 2 C: quit S: +OK POP 3 server signing off on
POP 3 (more) and IMAP More about POP 3 IMAP • Previous example uses “download • Keep all messages in one place: the and delete” mode. • Bob cannot re-read e-mail if he changes client • “Download-and-keep”: copies of messages on different clients • POP 3 is stateless across sessions server • Allows user to organize messages in folders • IMAP keeps user state across sessions: • names of folders and mappings between message IDs and folder name
Without Java. Mail • Read RFC 821 for SMTP • http: //www. cis. ohio-state. edu/htbin/rfc 821. html • Other RFC describe things like attachments • RFC 822: Text Messages, RFC 2045 -7: MIME, RFC 1939: POP 3, RFC 2060: IMAP, . . . • Open socket connection to port 25 HELO sending host MAIL FROM: sender email RCPT TO: recipient email DATA. . . the email message. . . any number of lines. . QUIT © 2001 JZ Ventures, Inc. 18
JAVAMAIL https: //javaee. github. io/javamail/#API_Documentation https: //github. com/javaee/javamail/releases/download/JAVAMAIL 1_6_2/javamail-samples. zip
Steps to send email using Java. Mail API There are following three steps to send email using Java. Mail. They are as follows: • Get the session object that stores all the information of host like host name, username, password etc. • compose the message • send the message © 2001 JZ Ventures, Inc. 20
Get the session object The javax. mail. Session class provides two methods to get the object of session, Session. get. Default. Instance() method and Session. get. Instance() method. You can use any method to get the session object. 1 public static Session get. Default. Instance(Properties props) default session. returns the 2 public static Session get. Default. Instance(Properties props, Authenticator auth) returns the default session. 3 public static Session get. Instance(Properties props) returns the new session. 4 public static Session get. Instance(Properties props, Authenticator auth) returns the new session. © 2001 JZ Ventures, Inc. 21
Get the session object Example of get. Default. Instance() method Properties properties=new Properties(); //fill all the information like host name etc. Session session=Session. get. Default. Instance(properties, null); Example of get. Instance() method Properties properties=new Properties(); //fill all the information like host name etc. Session session=Session. get. Instance(properties, null); © 2001 JZ Ventures, Inc. 22
Compose the message The javax. mail. Message class provides methods to compose the message. But it is an abstract class so its subclass javax. mail. internet. Mime. Message class is mostly used. To create the message, you need to pass session object in Mime. Message class constructor. For example: Mime. Message message=new Mime. Message(session); © 2001 JZ Ventures, Inc. 23
Com po se the message-- Commonly used met hods of Mime. Message clas s 1 public void set. From(Address address) is used to set the from header field. 2 public void add. Recipient(Message. Recipient. Type type, Address address)is used to add the given address to the recipient type. 3 public void add. Recipients(Message. Recipient. Type type, Address[] addresses) is used to add the given addresses to the recipient type. 4 public void set. Subject(String subject) field. is used to set the subject header 5 public void set. Text(String textmessage) is used to set the text as the message content using text/plain MIME type. 6 public void set. Content(Object msg, String content. Type) the content as the message content using given MIME type. © 2001 JZ Ventures, Inc. is used to set 24
Compose the message Example: Mime. Message message=new Mime. Message(session); message. set. From(new nternet. Address("sonoojaiswal@sssit. org")); message. add. Recipient(Message. Recipient. Type. To, new Internet. Address("sonoojaiswal@javatpoint. com")); message. set. Header("Hi, everyone"); message. set. Text("Hi, This mail is to inform you. . . "); © 2001 JZ Ventures, Inc. 25
Send the message The javax. mail. Transport class provides method to send the message. Commonly used methods of Transport class: 1 public static void send(Message message) is used send the message. 2 public static void send(Message message, Address[] address) is used send the message to the given addresses. Example to send the message: Transport. send(message); © 2001 JZ Ventures, Inc. 26
Simple example of sending email in Java In this example, we are going to learn how to send email by SMTP server installed on the machine e. g. Postcast server, Apache James server, Cmail server etc. If you want to send email by using your SMTP server provided by the host provider, see the example after this one. For sending the email using Java. Mail API, you need to load the two jar files: mail. jar activation. jar download these jar files or go to the Oracle site to download the latest version. © 2001 JZ Ventures, Inc. 27
Simple example of sending email in Java-1 import java. util. *; import javax. mail. internet. *; import javax. activation. *; public class Send. Email { public static void main(String [] args){ String to = "sonoojaiswal 1988@gmail. com"; //change accordingly String from = "sonoojaiswal 1987@gmail. com"; change accordingly String host = "localhost"; //or IP address //Get the session object Properties properties = System. get. Properties(); properties. set. Property("mail. smtp. host", host); Session session = Session. get. Default. Instance(properties); © 2001 JZ Ventures, Inc. 28
Simple example of sending email in Java-2 //compose the message try{ Mime. Message message = new Mime. Message(session); message. set. From(new Internet. Address(from)); message. add. Recipient(Message. Recipient. Type. TO, new Internet. Address(to)); message. set. Subject("Ping"); message. set. Text("Hello, this is example of sending email "); // Send message Transport. send(message); System. out. println("message sent successfully. . "); }catch (Messaging. Exception mex) {mex. print. Stack. Trace(); } } } © 2001 JZ Ventures, Inc. 29
wireshark 分析 客户端发送登陆请求 © 2001 JZ Ventures, Inc. 30
Thanks reference 1. some codes and pictures are from Internet
- Slides: 32