Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30480083
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-05-07 13:23:36

------------------------------------------------------------------------sendMail.jsp----------------------------------------------------------------

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
   <head>
     <html:base />
     <title>sendMail.jsp</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

   </head>
  
   <body>
     <html:form action="/sendMail">
      <table>
       <tr>
        <td>Mail to:
         <html:text property="mailTo" size="50"/>
        </td>
       </tr>
       <tr>
        <td>Subject:
         <html:text property="subject" size="50"/>&nbsp;
         <html:submit>Send</html:submit>
        </td>
       </tr>
       <tr>
        <td>Information:<br>
         <html:textarea property="information" rows="10" cols="55"/>
        </td>
       </tr>
      </table>
     </html:form>
   </body>
</html:html>

------------------------------------------------------------------SendMailForm.java--------------------------------------------------------------

package com.sh.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class SendMailForm extends ActionForm {
     private static final long serialVersionUID = -3461586021385247680L;

     private String information;

     private String mailTo;

     private String subject;

     public ActionErrors validate(ActionMapping mapping,
      HttpServletRequest request) {
              return null;
     }

     public void reset(ActionMapping mapping, HttpServletRequest request) {
              this.mailTo = null;
              this.subject = null;
              this.information = null;
     }

    public String getInformation() {
              return information;
     }

    public void setInformation(String information) {
              this.information = information;
     }

    public String getMailTo() {
              return mailTo;
     }

    public void setMailTo(String mailTo) {
              this.mailTo = mailTo;
     }

    public String getSubject() {
             return subject;
     }

    public void setSubject(String subject) {
             this.subject = subject;
     }
}

-----------------------------------------------------------------------SendMailAction.java----------------------------------------------------------

package com.sh.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.sh.struts.form.SendMailForm;

public class SendMailAction extends Action {
         public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response) {
                   SendMailForm smf = (SendMailForm) form;
                   SendMail sml = new SendMail();
                   sml.send(smf.getMailTo(), smf.getSubject(), smf.getInformation());
                   return mapping.findForward("success");
         }
}

----------------------------------------------------------------------struts-config.xml----------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "">

<struts-config>
   <data-sources/>

   <form-beans >
     <form-bean name="sendMailForm" type="com.sh.struts.form.SendMailForm" />
   </form-beans>

   <global-exceptions />
   <global-forwards />

   <action-mappings >
      <action
       attribute="sendMailForm"
       input="/sendMail.jsp"
       name="sendMailForm"
       path="/sendMail"
       scope="request"
       type="com.sh.struts.action.SendMailAction">
       <forward name="success" path="/sendMail.jsp" />
     </action>
   </action-mappings>
   <message-resources parameter="com.sh.struts.ApplicationResources" />
</struts-config>

--------------------------------------------------------------------------SendMail.java-----------------------------------------------------------------

package com.sh.struts.action;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {
     private String sender = "test@126.com";

     private String smtpHost = "smtp.126.com";

     private String user = "test";

     private String password = "test";

     private void sendBody(String mailTo, String subject, String information)
     throws Exception, MessagingException {
              Properties prop = new Properties();
              prop.put("mail.smtp.host", smtpHost);// 设置smtp主机

              prop.put("mail.smtp.auth", "true");// 使用smtp身份验证

              prop.put("mail.smtp.user", user);
              prop.put("mail.smtp.password", password);
              Session session = Session.getInstance(prop, new Authenticator() {
                          public PasswordAuthentication getPasswordAuthentication() {
                                     return new PasswordAuthentication(sender, password);
                          }
              });
              // 获得邮件会话对象

              MimeMessage message = new MimeMessage(session);
              message.setFrom(new InternetAddress(sender));
              // 设置收件人地址

              message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
              // 设置邮件主题

              message.setSubject(subject);
              // mail内容部分

              MimeBodyPart part = new MimeBodyPart();
              part.setText(information == null ? "" : information, "UTF-8");
              // 设置邮件格式为html

              part.setContent(information.toString(), "text/html;charset=UTF-8");
              Multipart multipart = new MimeMultipart();
              multipart.addBodyPart(part);// 在 Multipart 中增加mail内容部分

              message.setContent(multipart);// 增加 Multipart 到信息体

              message.setSentDate(new Date());// 设置发送日期

              Transport.send(message);// 发送邮件

     }

     public void send(String mailTo, String subject, String information) {
              try {
                       this.sendBody(mailTo, subject, information);
              } catch (Exception e) {
                       e.printStackTrace();
              }
     }
}
 

阅读(2042) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~