Chapter 15 How to use JavaMail to send email Objectives Applied Knowledge
How email works Sending client Mail client software Receiving client Mail client software SMTP Sending server Mail server software POP Receiving server Mail server software Three protocols for sending and retrieving email messages Protocol Description
Another protocol that s used with email Protocol Description The JAR files for the JavaMail API File Description How to install the JavaMail API
How to install the JavaBeans Activation Framework API Three packages for sending email Package Description
Code that uses the JavaMail API to send an email try // 1 - get a mail session Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); Session session = Session.getDefaultInstance(props); // 2 - create a message Message message = new MimeMessage(session); message.setsubject(subject); message.settext(body); // 3 - address the message Address fromaddress = new InternetAddress(from); Address toaddress = new InternetAddress(to); message.setfrom(fromaddress); message.setrecipient(message.recipienttype.to, toaddress); Code that uses the JavaMail API (cont.) // 4 - send the message Transport.send(message); catch (MessagingException e) log(e.tostring());
Common properties that can be set for a Session object Property Description How to get a mail session for a local SMTP server Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); Session session = Session.getDefaultInstance(props); Another way to get a mail session for a local SMTP server Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "localhost"); props.put("mail.smtp.port", 25); Session session = Session.getDefaultInstance(props); session.setdebug(true);
How to get a mail session for a remote SMTP server Properties props = new Properties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtps.host", "smtp.gmail.com"); props.put("mail.smtps.port", 465); props.put("mail.smtps.auth", "true"); props.put("mail.smtps.quitwait", "false"); Session session = Session.getDefaultInstance(props); session.setdebug(true); How to create a mail session
How to create a message Message message = new MimeMessage(session); How to set the subject line of a message message.setsubject("order Confirmation"); How to set the body of a plain text message message.settext("thanks for your order!"); How to set the body of an HTML message message.setcontent( "<H1>Thanks for your order!</h1>", "text/html"); How to create a message
How to set the From address Address fromaddress = new InternetAddress("cds@murach.com"); message.setfrom(fromaddress); How to set the To address Address toaddress = new InternetAddress("andi@yahoo.com"); message.setrecipient( Message.RecipientType.TO, toaddress); How to set the CC address Address ccaddress = new InternetAddress("ted@yahoo.com"); message.setrecipient( Message.RecipientType.CC, ccaddress); How to set the BCC address Address bccaddress = new InternetAddress("jsmith@gmail.com"); message.setrecipient( Message.RecipientType.BCC, bccaddress); How to include an email address and a name Address toaddress = new InternetAddress( "andi@yahoo.com", "Andrea Steelman"); How to send a message to multiple recipients Address[] maillist = new InternetAddress("andi@hotmail.com"), new InternetAddress("joelmurach@yahoo.com"), new InternetAddress("jsmith@gmail.com") ; message.setrecipients( Message.RecipientType.TO, maillist); How to add recipients to a message Address toaddress = new InternetAddress("joelmurach@yahoo.com"); message.addrecipient( Message.RecipientType.TO, toaddress);
How to address a message How to send a message when no authentication is required Transport.send(message); How to send a message when authentication is required Transport transport = session.gettransport(); transport.connect("johnsmith@gmail.com", "sesame"); transport.sendmessage( message, message.getallrecipients()); transport.close();
How to send a message A helper class for sending an email with a local SMTP server package util; import java.util.properties; import javax.mail.*; import javax.mail.internet.*; public class MailUtilLocal public static void sendmail(string to, String from, String subject, String body, boolean bodyishtml) throws MessagingException // 1 - get a mail session Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "localhost"); props.put("mail.smtp.port", 25); Session session = Session.getDefaultInstance(props); session.setdebug(true);
A helper class for sending an email with a local SMTP server (cont.) // 2 - create a message Message message = new MimeMessage(session); message.setsubject(subject); if (bodyishtml) message.setcontent(body, "text/html"); else message.settext(body); // 3 - address the message Address fromaddress = new InternetAddress(from); Address toaddress = new InternetAddress(to); message.setfrom(fromaddress); message.setrecipient( Message.RecipientType.TO, toaddress); // 4 - send the message Transport.send(message); A servlet that sends an email package email; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.messagingexception; import business.user; import data.userio; import util.*; public class AddToEmailListServlet extends HttpServlet protected void dopost(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException
A servlet that sends an email (cont.) // get parameters from the request String firstname = request.getparameter("firstname"); String lastname = request.getparameter("lastname"); String emailaddress = request.getparameter("emailaddress"); // create the User object and write it to a file User user = new User( firstname, lastname, emailaddress); ServletContext sc = getservletcontext(); String path = sc.getrealpath( "/WEB-INF/EmailList.txt"); UserIO.addRecord(user, path); // store the User object in the session HttpSession session = request.getsession(); session.setattribute("user", user); A servlet that sends an email (cont.) // send email to user String to = emailaddress; String from = "email_list@murach.com"; String subject = "Welcome to our email list"; String body = "Dear " + firstname + ",\n\n" + "Thanks for joining our email list. " + "We'll make sure to send you announcements " + "about new products and promotions.\n" + "Have a great day and thanks again!\n\n" + "Kelly Slivkoff\n" + "Mike Murach & Associates"; boolean isbodyhtml = false; try MailUtilLocal.sendMail( to, from, subject, body, isbodyhtml);
A servlet that sends an email (cont.) catch (MessagingException e) String errormessage = "ERROR: Unable to send email. " + "Check Tomcat logs for details.<br>" + "NOTE: You may need to configure your " + "system as described in chapter 15.<br>" + "ERROR MESSAGE: " + e.getmessage(); request.setattribute( "errormessage", errormessage); this.log( "Unable to send email. \n" + "Here is the email you tried to send: \n" + "=====================================\n" + "TO: " + emailaddress + "\n" + "FROM: " + from + "\n" + "SUBJECT: " + subject + "\n" + "\n" + body + "\n\n"); A servlet that sends an email (cont.) // forward request and response to JSP page String url = "/display_email_entry.jsp"; RequestDispatcher dispatcher = getservletcontext().getrequestdispatcher(url); dispatcher.forward(request, response);
A helper class for sending an email with a remote SMTP server package util; import java.util.properties; import javax.mail.*; import javax.mail.internet.*; public class MailUtilYahoo public static void sendmail(string to, String from, String subject, String body, boolean bodyishtml) throws MessagingException // 1 - get a mail session Properties props = new Properties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtps.host", "smtp.mail.yahoo.com"); props.put("mail.smtps.port", 465); props.put("mail.smtps.auth", "true"); Session session = Session.getDefaultInstance(props); session.setdebug(true); A helper class for sending an email with a remote SMTP server (cont.) // 2 - create a message Message message = new MimeMessage(session); message.setsubject(subject); if (bodyishtml) message.setcontent(body, "text/html"); else message.settext(body); // 3 - address the message Address fromaddress = new InternetAddress(from); Address toaddress = new InternetAddress(to); message.setfrom(fromaddress); message.setrecipient( Message.RecipientType.TO, toaddress);
A helper class for sending an email with a remote SMTP server (cont.) // 4 - send the message Transport transport = session.gettransport(); transport.connect("johnsmith", "sesame"); transport.sendmessage( message, message.getallrecipients()); transport.close();