发布于 2015-08-17 14:57:33 | 217 次阅读 | 评论: 0 | 来源: 网络整理

要使用JSP发送邮件是很简单的,需要在您的机器上安装JavaMail API和Java激活框架(JAF)。

  • 您可以从Java标准网站上下载的JavaMail(版本1.2)的最新版本。

  • 您可以从Java的标准的网站下载JavaBeans激活框架JAF(版本1.0.2)的最新版本。

下载并解压缩这些文件,在新创建的顶层目录,你会发现一些jar文件同时为应用。你需要添加了mail.jar和activation.jar文件的文件在你的CLASSPATH中。

发送一个简单的电子邮件:

下面是一个例子,从你的机器发送一个简单的电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。同时要确保从Java电子邮件API包和CLASSPATH中可用JAF包ARA的所有jar文件。


<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "abcd@gmail.com";

   // Sender's email ID needs to be mentioned
   String from = "mcmohd@gmail.com";

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);
      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));
      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set Subject: header field
      message.setSubject("This is the Subject Line!");
      // Now set the actual message
      message.setText("This is actual message");
      // Send message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<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>

现在,让我们把上面的代码写到SendEmail.jsp文件,并使用URL http://localhost:8080/SendEmail.jsp调用这个JSP这将发送一封电子邮件到指定的电子邮件ID abcd@gmail.comand会显示以下回应:

Send Email using JSP

Result: Sent message successfully....

如果您想发送电子邮件给多个收件人,然后下面的方法将被用来指定多个电子邮件ID:


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

这里的参数的说明:

  • type: 这将被设定为TO,CC或BCC。在这里,CC代表抄送和密件抄送代表黑碳复制。例如Message.RecipientType.TO

  • addresses: 这是电子邮件的ID的数组。需要使用 InternetAddress()方法,同时指定电子邮件ID。

发送HTML Email:

下面是一个例子,从机器发送的HTML电子邮件。这里假设你的本地主机连接到互联网,并有足够的能力来发送电子邮件。同时要确保从Java电子邮件API包和CLASSPATH中可用JAF包ARA的所有jar文件。

这个例子非常相似,前一个,除了在这里我们使用的是使用setContent()方法来设置内容的第二个参数为“text / html类型”指定的HTML内容被包含在消息中。

通过这个例子,可以发送你喜欢HTML内容。


<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "abcd@gmail.com";

   // Sender's email ID needs to be mentioned
   String from = "mcmohd@gmail.com";

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);
      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));
      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set Subject: header field
      message.setSubject("This is the Subject Line!");
     
      // Send the actual HTML message, as big as you like
      message.setContent("<h1>This is actual message</h1>",
                            "text/html" );
      // Send message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<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>

现在,尝试使用上面的JSP在给定的电子邮件ID发送HTML格式的邮件。

发送附件的电子邮件:

下面是一个例子与附件从你的机器发送电子邮件:


<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "abcd@gmail.com";

   // Sender's email ID needs to be mentioned
   String from = "mcmohd@gmail.com";

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   Properties properties = System.getProperties();

   // Setup mail server
   properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);

      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));

      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));

      // Set Subject: header field
      message.setSubject("This is the Subject Line!");

      // Create the message part 
      BodyPart messageBodyPart = new MimeBodyPart();

      // Fill the message
      messageBodyPart.setText("This is message body");
      
      // Create a multipar message
      Multipart multipart = new MimeMultipart();

      // Set text message part
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
      String filename = "file.txt";
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(filename);
      multipart.addBodyPart(messageBodyPart);

      // Send the complete message parts
      message.setContent(multipart );

      // Send message
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<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>

现在试着上面的JSP运行到一个文件作为附件发送以及在给定的电子邮件ID的消息。

用户认证部分:

如果需要提供用户ID和密码的电子邮件服务器进行身份验证的目的,那么你可以按如下方法设置这些属性:


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

邮件发送机制的其余部分将继续如上面所述。

使用表单发送电子邮件:

您可以使用HTML表单接受电子邮件参数,然后你可以使用request对象如下,让所有的信息:


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

一旦你把所有的信息设置完成,就可以使用上面提到的程序来发送电子邮件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务