JSP Send Mail

Although the use of JSP for message sending function is simple, but requires JavaMail API, and need to install JavaBean Activation Framework.

  • You can download the latest version of Java the JavaMail , open the right side of the page there is a Downloads link, click on it to download.
  • You can download the latest version of Java (Version 1.1.1) JAF .

You can also use the site to provide download link:

  • JavaMail mail.jar 1.4.5
  • JAF (version 1.1.1) activation.jar

Download and unzip the files in the root directory, you will see a series of jar package. The mail.jar packages and package activation.jar added CLASSPATH variable.

Send a simple mail

This example shows how from your machine to send a simple message. It assumes that localhost is already connected to the network and the ability to send a message. In the meantime, once again confirm mail.jar packages and activation.jar package has been added to the CLASSPATH variable.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);
      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));
      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");
      // 现在设置的实际消息
      message.setText("This is actual message");
      // 发送消息
      Transport.send(message);
      result = "Sent message successfully/en/en";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message/en/en";
   }
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

Now visit http: // localhost: 8080 / SendEmail, it will send a message to abcd@gmail.com and displays the following results:

Send Email using JSP
Result: Sent message successfully/en/en

If you want to send a message to many people, the methods listed below can be used to specify more than one e-mail address:

void addRecipients(Message.RecipientType type, 
                   Address[] addresses)
throws MessagingException

Description of the parameters are as follows:

  • type: This value will be set to TO, CC, or BCC. CC representatives a copy, BCC for black copy the example used in the program is TO.
  • addresses: This email address is an array, when the specified e-mail address is required InternetAddress () method.

Send an HTML e-mail

This example sends a simple HTML messages. It assumes that you have already connected to the network of localhost and the ability to send messages. In the meantime, once again confirm mail.jar packages and activation.jar package has been added to the CLASSPATH variable.

This example is very similar to the previous example, but in this case we use setContent () method, the "text / html" as the second parameter passed to it to indicate that the message contains the HTML content.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);
      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));
      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");
     
      // 设置 HTML消息
      message.setContent("<h1>This is actual message</h1>",
                            "text/html" );
      // 发送消息
      Transport.send(message);
      result = "Sent message successfully/en/en";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message/en/en";
   }
%>
<html>
<head>
<title>Send HTML Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

Now you can try to use more than JSP file to send HTML e-mail message.


Accessories included in the message

This example demonstrates how to send a message that contains an attachment.

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);

      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));

      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));

      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");

      // 创建消息部分
      BodyPart messageBodyPart = new MimeBodyPart();

      // 填充消息
      messageBodyPart.setText("This is message body");
      
      // 创建多媒体消息
      Multipart multipart = new MimeMultipart();

      // 设置文本消息部分
      multipart.addBodyPart(messageBodyPart);

      // 附件部分
      messageBodyPart = new MimeBodyPart();
      String filename = "file.txt";
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(filename);
      multipart.addBodyPart(messageBodyPart);

      // 发送完整消息
      message.setContent(multipart );

      // 发送消息
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully/en/en";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message/en/en";
   }
%>
<html>
<head>
<title>Send Attachement Email using JSP</title>
</head>
<body>
<center>
<h1>Send Attachement Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

User authentication section

If the mail server requires a user name and password for user authentication, you can set such as the following:

 props.setProperty("mail.user", "myuser");
 props.setProperty("mail.password", "mypwd");

Use the form to send a message

Using an HTML form to receive an email and get all the information by e-mail request to:

String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");

After obtaining this information, you can use the examples mentioned above to send mail.