Java Web JavaMail 邮件发送

来源:互联网 发布:网络彩票概念龙头股 编辑:程序博客网 时间:2024/06/02 18:50

  JavaMail是java技术当中用来发送邮件和接收邮件的API,JavaMail在java开发中是应用比较广泛的,很多时候,我们都需要邮件发送和接收的功能。

  如我们开发的一个网络书店,当用户下了订单之后,我们都会把订单信息发送到用户的邮箱当中


这是就可以使用JavaMail技术了。


  下载JavaMail:


    http://java.sun.com/products/javamail/index.html


    得到:javamail1_4_4.zip          解压;mail.jar


测试代码一:


SendMail.java


代码:


package webbook.chapter16;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;


public class SendMail {
 public static void main(String[] args) {
  try {
   Properties props = new Properties();
   Session session = Session.getInstance(props, null);
   // 在属性中设置发送邮件服务器地址与协议
   props.put("mail.host", "127.0.0.1");
   props.put("mail.transport.protocol", "smtp");
   Message message = new MimeMessage(session);
   // 设置发件人
   message.setFrom(new InternetAddress("hongtenzone@foxmail.com"));
   // 设置收件人, Message.RecipientType.CC是设置抄送者
   message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@163.com"));
   message.setRecipient(Message.RecipientType.CC, new InternetAddress("web@foxmail.com"));
   message.setSubject("问候"); // 邮件主题
   message.setSentDate(new Date()); // 发送时间
   message.setText("这是一个测试!\n哈哈,你好!\n呵呵!"); //内容   
   Transport.send(message);
  } catch (MessagingException m) {
   m.printStackTrace();
  }
 }
}


说明;


这个程序有很大的限制:


  首先,在本机要装一个邮件服务器,也可以使用其他人的邮件服务器,比如你公司专有的邮件服务器。


还可以使用免费的邮件服务器,如:sina,搜狐,网易等,他们都有免费邮件服务,但是需要一些身份验证,


有一些麻烦……


  那有没有其他方法呢?


测试代码二:


下载Apache Commons Email组件


   下载地址:http://commons.apache.org/mail/


   下载:commons-email-1.2-bin.zip        得到:commons-email-1.2.jar


新建web project项目:


  Build Path: commons-email-1.2.jar           javaMail1.2.jar


sendMail.jsp


代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head><title>Send E-Mail Form</title></head>
<style>* {font-family:"宋体" ; font-size: 14px }</style>
<body>
<p><font color="red">发送邮件的程序</font></p>
  <form action="<%=request.getContextPath()%>/servlet/sendMail" method="post">
    <table cellspacing="2" cellpadding="2" border="0">      
      <tr><td>收件人</td>
        <td><input type="text" name="to" size="30"></td>
      </tr>
      <tr><td>发件人</td>
        <td><input type="text" name="from" size="30"></td>
      </tr>
      <tr><td>主题</td>
        <td><input type="text" name="subject" size="30"></td>
      </tr>
      <tr><td valign="top">正文</td>
        <td><textarea cols="50" rows="15" name="body"></textarea></td>
      </tr>
      <tr><td></td><td>
    <input type="submit" value="提交"><input type="reset" value="重置"></td>
      </tr>
    </table>
  </form>
</body>
</html>


SendMailServlet.java


代码:


package webbook.chapter16;
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import org.apache.commons.mail.*;


public class SendMailServlet extends HttpServlet {
 private static final long serialVersionUID = -3302031686208755627L;


 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  SimpleEmail email = new SimpleEmail();
  email.setHostName("smtp.sina.com");
  email.setAuthentication("web08", "web2008");
  try {   
   email.setCharset("UTF-8");
   email.addTo(request.getParameter("to"));
   email.setFrom(request.getParameter("from"));
   email.setSubject(request.getParameter("subject"));   
   email.setMsg(request.getParameter("body"));
   email.send();
   request.setAttribute("sendmail.message", "邮件发送成功!");   
  } catch (EmailException e) {    
   e.printStackTrace();
   request.setAttribute("sendmail.message", "邮件发送不成功!");   
  }
  request.getRequestDispatcher("/sendResult.jsp").forward(request, response);
 }
}


sendResult.jsp


代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head><title>Send E-Mail result page</title></head>
<style>
* { font-family:"宋体" ; font-size: 14px }
</style>
<body>
<p><font color="red">${requestScope['sendmail.message']}</font></p>
<p><a href="<%=request.getContextPath()%>/sendMail.jsp">返回</a></p>
</body>

</html>

From:http://www.cnblogs.com/hongten/archive/2011/07/26/2117431.html

0 0
原创粉丝点击