Lecture19

1,024 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,024
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture19

  1. 1. Server-side Web Programming Lecture 19: Java Mail
  2. 2. How email works Sending Client Mail Client Software Sending Server Mail Server Software Receiving Client Mail Client Software Receiving Server Mail Server Software SMTP SMTP POP/ IMAP
  3. 3. 3 protocols <ul><li>SMTP: Simple Mail Transfer Protocol </li></ul><ul><ul><li>is used to send a message from the mail client software to your mail server / or from one mail server to another </li></ul></ul><ul><li>POP: Post Office Protocol </li></ul><ul><ul><li>transfers all messages from the mail server to the mail client. POP in version 3: POP3 </li></ul></ul><ul><li>IMAP: Internet Message Access Protocol </li></ul><ul><ul><li>Is used by web-based mail services such as Hotmail, Yahoo, and Gmail. </li></ul></ul><ul><ul><li>Allows a web browser to read messages that are stored on the mail server. IMAP in version 4: IMAP4 </li></ul></ul>
  4. 4. Normal Email Process <ul><li>Mail client software converts message to SMTP (or other) format before sending to server </li></ul><ul><li>Common form for usual components of message: </li></ul><ul><ul><li>Sender </li></ul></ul><ul><ul><li>Recipient(s) </li></ul></ul><ul><ul><li>Subject </li></ul></ul><ul><ul><li>Text </li></ul></ul><ul><ul><li>Etc. </li></ul></ul><ul><li>Mail server sends message in MIME protocol to receiving server </li></ul><ul><ul><li>Multipurpose Internet Message Extension </li></ul></ul><ul><li>Mail client software on receiving client accesses using mail client software (possibly different) </li></ul><ul><ul><li>POP </li></ul></ul><ul><ul><li>IMAP </li></ul></ul>
  5. 5. Java Mail <ul><li>Java Mail API is a programming interface that makes it easy for Java developers to write code that automatically sends an email. </li></ul><ul><li>Key idea: Java Mail (or other mail software) must emulate what mail client software does </li></ul><ul><ul><li>Create SMTP or other protocol message </li></ul></ul><ul><ul><li>Insert required mail components </li></ul></ul><ul><ul><li>Connect with sending server and transmit message </li></ul></ul><ul><li>Requires mail classes </li></ul><ul><ul><li>Download from www.java.sun.com </li></ul></ul><ul><ul><li>Insert mail.jar and activation.jar file into java libraries </li></ul></ul><ul><ul><ul><li>mail. jar: contains the Java classes for the Java Mail API </li></ul></ul></ul><ul><ul><ul><li>activation. jar: contains the Java classes for the JavaBean Activation Framework. These classes are necessary for the JavaMail API to run </li></ul></ul></ul>
  6. 6. How to install the Java Mail API and JavaBeans Activation Framework API <ul><li>Locate the download page for the JavaMail API/JavaBeans Activation Framework API on the Java website: www.java.sun.com </li></ul><ul><li>Click on the Download button and follow the instructions </li></ul><ul><li>Save the zip file: javamail-1_4.zip / jaf-1_1.zip to your hard disk </li></ul><ul><li>Extract the files from the zip file </li></ul><ul><li>Copy the mail.jar / activation.jar file to the JDK’s jrelibext directory </li></ul>
  7. 7. Steps in Java Mail <ul><li>Create a mail session </li></ul><ul><ul><li>Creates email message object </li></ul></ul><ul><li>Set message components </li></ul><ul><ul><li>Properties of message object </li></ul></ul><ul><li>Set addresses </li></ul><ul><ul><li>Can be list for group of recipients </li></ul></ul><ul><li>Send message </li></ul><ul><ul><li>May need to identify self to server </li></ul></ul><ul><li>Will need mail libraries </li></ul><ul><li><%@ page import=&quot;javax.mail.*, javax.mail.internet.*, java.util.*&quot;%> </li></ul>
  8. 8. Creating an Email Session <ul><li>Create a Properties object to store information: </li></ul><ul><ul><li>Generic Java class to store attribute/value pairs </li></ul></ul><ul><ul><li>Property props = new Properties(); </li></ul></ul><ul><li>Specify the sending server: </li></ul><ul><ul><li>props.put(“mail.smtp.host”, URL of mail server ); </li></ul></ul><ul><li>Create a new session object from those properties: </li></ul><ul><ul><li>Session s = Session.getDefaultInstance(props); </li></ul></ul><ul><li>Create new MimeMessage object from session </li></ul><ul><ul><li>MimeMessage message = new MimeMessage(s); </li></ul></ul>
  9. 9. Common properties <ul><li>mail.transport.protocol : specifies the protocol that’s used for the session </li></ul><ul><li>mail.smtp.host : specifies the host computer for SMTP server </li></ul><ul><li>mail.smtp.port : specifies the port that the SMTP server is using </li></ul><ul><li>mail.smtp.auth : specifies whether authentication is required to log in to the SMTP server </li></ul><ul><li>mail.smtp.quitwait : this property can be set to false to prevent an SSLException from occurring when you attempt to connect to a Gmail SMTP server </li></ul>
  10. 10. <ul><li>To set the properties of Properties object, you can use the put method to specify of a property name and value </li></ul><ul><li>The static getDefaultInstance method of the Session class returns the default Session object for the application </li></ul><ul><li>The setDebug method of the Session object can be used to print debugging information about the session to a log file </li></ul><ul><li>If you change the properties for a Session object, you must restart Tomcat before the changes take effect </li></ul><ul><li>If the Java application is running on the same computer as the SMTP server, you can use the localhost keyword to specify the SMTP host. </li></ul>
  11. 11. Setting Message Components <ul><li>Set the subject: </li></ul><ul><ul><li>message. setSubject (“ subject ”); </li></ul></ul><ul><li>Set the message text: </li></ul><ul><ul><li>message. setText (“Thanks for your order!”); //”text/plain” </li></ul></ul><ul><li>Could also be html or other types </li></ul><ul><ul><li>Will need to specify MIME type </li></ul></ul><ul><li>String response = “<html><head><title>Reciept</title</head><body><h3>Thank you for your order!</h3>”; message. setContent (response, “text/html”); </li></ul>
  12. 12. Setting Addresses <ul><li>Use setRecipient method </li></ul><ul><ul><li>Specify internet address of recipient </li></ul></ul><ul><ul><ul><li>Must use InternetAddress class </li></ul></ul></ul><ul><ul><ul><li>InternetAddress constructor requires email address, can also optionally give name </li></ul></ul></ul><ul><ul><li>Specify TO, CC, or BCC </li></ul></ul><ul><ul><li>Can add multiple recipients </li></ul></ul><ul><li>Message. setRecipient (Message.RecipientType. TO , new InternetAddress(“ [email_address] ”)); </li></ul><ul><li>Message. setRecipient (Message.RecipientType .CC , new InternetAddress( [email_address] , “Barney Rubble”)); </li></ul>
  13. 13. Setting Addresses <ul><li>Address can also be a list </li></ul><ul><ul><li>Good for mailing lists </li></ul></ul><ul><ul><li>Array of type Address </li></ul></ul><ul><li>Use setRecipients method </li></ul><ul><li>Address[] list = new Address[ size of mailing list ]; </li></ul><ul><li>for (int i = 0; i < size of mailing list ; i++) { </li></ul><ul><li>list[i] = get next address for file ; </li></ul><ul><li>} </li></ul><ul><li>Message. setRecipients (Message.RecipientType.TO, list); </li></ul><ul><li>Use addRecipient method </li></ul><ul><li>Message. addRecipient ( Message.RecipientType.TO, </li></ul><ul><li>new InternetAddress(“ [email_address] ”) ) </li></ul><ul><li>Using addRecipient/addRecipients to add email addresses to any existing addresses for a message </li></ul>
  14. 14. Sending Messages <ul><li>Simple method: Transport.send(message); </li></ul><ul><li>More complex method if server requires authentication: </li></ul><ul><ul><li>Create Transport object from sessions Transport t = session.getTransport(); </li></ul></ul><ul><ul><li>Connect to server with name and password t.connect(“ your name ”, “ your password ”); </li></ul></ul><ul><ul><li>Send the message t.sendMessage(message, message.getAllRecipients()); </li></ul></ul>

×