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

Lecture19

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