How to use JavaMail to send email



Similar documents
An introduction to web programming with Java

SERVLETS - SENDING

2. Follow the installation directions and install the server on ccc

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Java Server Pages combined with servlets in action. Generals. Java Servlets

Ch-03 Web Applications

Java 2 Web Developer Certification Study Guide Natalie Levi

1. Seam Mail Introduction 2. Configuration 3. Core Usage 4. Templating 5. Advanced Features

2.8. Session management

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Web Container Components Servlet JSP Tag Libraries

Java Servlet and JSP Programming. Structure and Deployment China Jiliang University

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

INTRODUCTION TO WEB TECHNOLOGY

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

DTS Web Developers Guide

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, Java Servlets

Web Application Development on a Linux System With a DB2 Database By Alan Andrea

Managing Data on the World Wide-Web

BAPI. Business Application Programming Interface. Compiled by Y R Nagesh 1

JAVAMAIL API - SMTP SERVERS

Web Applications. For live Java training, please see training courses at

IBM WebSphere Application Server

Manual. Programmer's Guide for Java API

Principles and Techniques of DBMS 5 Servlet

Introduction to J2EE Web Technologies

Mass ing Techniques

Intellicus Single Sign-on

Java Web Programming. Student Workbook

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

Java and Web. WebWork

James: The Java Apache Mail Enterprise Server

Web Application Programmer's Guide

Application Security

CONTROLLING WEB APPLICATION BEHAVIOR WITH

SSC - Web applications and development Introduction and Java Servlet (II)

Piotr Nowicki's Homepage. Java EE 6 SCWCD Mock Exam. "Simplicity is the ultimate sophistication." Important!

UTL_SMTP and Sending Mail

Chapter 22 How to send and access other web sites

CHAPTER 9: SERVLET AND JSP FILTERS

Multiple vulnerabilities in Apache Foundation Struts 2 framework. Csaba Barta and László Tóth

J2EE Web Development. Agenda. Application servers. What is J2EE? Main component types Application Scenarios J2EE APIs and Services.

JSP. Common patterns

CS108, Stanford Handout #33 Young. HW5 Web

Penetration from application down to OS

Web Programming with Java Servlets

Java Servlet Tutorial. Java Servlet Tutorial

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

Mail User Agent Project

To configure Outlook Express for your InfoMetrics address:

Exam Prep. Sun Certified Web Component Developer (SCWCD) for J2EE Platform

Building Web Applications with Servlets and JavaServer Pages

Controlling Web Application Behavior

Pure server-side Web Applications with Java, JSP. Application Servers: the Essential Tool of Server-Side Programming. Install and Check Tomcat

Web Applications. Originals of Slides and Source Code for Examples:

No no-argument constructor. No default constructor found

Device Log Export ENGLISH

Overview of Web Services API

Outline. Lecture 9: Java Servlet and JSP. Servlet API. HTTP Servlet Basics

Development of a Real-time Customer Service System. Abstract

PA165 - Lab session - Web Presentation Layer

INSIDE SERVLETS. Server-Side Programming for the Java Platform. An Imprint of Addison Wesley Longman, Inc.

Integrating Servlets and JSP: The Model View Controller (MVC) Architecture

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

Java Servlet 3.0. Rajiv Mordani Spec Lead

Arjun V. Bala Page 20

Step-by-Step Configuration Instructions

Systems Integration in the Cloud Era with Apache Camel. Kai Wähner, Principal Consultant

Session Tracking Customized Java EE Training:

chapter 3Chapter 3 Advanced Servlet Techniques Servlets and Web Sessions Store Information in a Session In This Chapter

Geronimo Quartz Plugins

CHAPTER 5. Java Servlets

Building Web Applications, Servlets, JSP and JDBC

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

An Overview of Java. overview-1

Java EE 6 New features in practice Part 3

Result: Adds an X-header named "X-Company" with the value of "Your Company Name"

Creating Java EE Applications and Servlets with IntelliJ IDEA

Web Application Architecture (based J2EE 1.4 Tutorial)

MPDS Configuration Sheet MS Outlook Express Mail Client

Web Applications and Struts 2

WIRIS quizzes web services Getting started with PHP and Java

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

SSC - Web development Model-View-Controller for Java web application development

White Paper March 1, Integrating AR System with Single Sign-On (SSO) authentication systems

Database System Concepts

Web Application Development

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

Transcription:

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