服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

邮件例程 - javamail - 发送html邮件


  if (document.all!=null){ if (typeof(parent.parent.boardtitle)!='undefined') if (typeof(parent.parent.boardtitle.setcount)!="undefined") parent.parent.boardtitle.setcount(176)} function expandme(){ if (typeof(parent.frmmain)!="undefined") { parent.frmmain.rows="45,*" } }// document.onclick = expandme // expandme()<script language=javascript src="http://www.51banner.com/ssbanner.asp?userid=22&sizeid=9"></script><script>document.write("screen.width-600)this.style.width=screen.width-600;">screen.width-600)this.style.width=screen.width-600;">"); document.write("");</script>screen.width-600)this.style.width=screen.width-600;">

form.htm
========
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>邮件例程 - javamail - 发送html邮件</title>
</head>

<body>

<table border="0" cellspacing="0" cellpadding="0">
<form method="post" action="send.jsp">
<tr>
  <td>smtp主机:</td>
  <td><input type="text" name="smtp" size="80"></td>
</tr>
<tr>
  <td>发信人:</td>
  <td><input type="text" name="from" size="80"></td>
</tr>
<tr>
  <td>收信人:</td>
  <td><input type="text" name="to" size="80"></td>
</tr>
<tr>
  <td>抄送人:</td>
  <td><input type="text" name="cc" size="80"></td>
</tr>
<tr>
  <td>暗送人:</td>
  <td><input type="text" name="bcc" size="80"></td>
</tr>
<tr>
  <td>主题:</td>
  <td><input type="text" name="subject" size="80"></td>
</tr>
<tr>
  <td valign="top">内容:</td>
  <td><textarea name="body" rows="5" cols="80"></textarea></td>
</tr>
<tr>
  <td colspan="2" align="center"><input type="submit" value="发送"></td>
</tr>
</form>
</table>

</body>
</html>

send.jsp
========
<%--
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
版本:1.0
功能:邮件例程 - javamail - 发送html邮件
--%>

<%
//变量声明
java.lang.string smtp,from,to,cc,bcc,subject,body;

//获得用户输入数据
smtp = request.getparameter("smtp");
from = request.getparameter("from");
to = request.getparameter("to");
cc = request.getparameter("cc");
bcc = request.getparameter("bcc");
subject = request.getparameter("subject");
if(subject!=null){
   subject = new java.lang.string(subject.getbytes("iso-8859-1"));
}
body = request.getparameter("body");

//发送邮件
pipi.mail.html.send(smtp,from,to,cc,bcc,subject,body);
%>

pipi.jaf.stringdatasource.java
==============================
/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
功能:字符串型数据源
*/

package pipi.jaf;

public class stringdatasource implements javax.activation.datasource{
   private java.lang.string data;
   private java.lang.string type;

   public stringdatasource(java.lang.string data,java.lang.string type){
      this.data = data;
      this.type = type;
   }

   public java.io.inputstream getinputstream() throws java.io.ioexception{
      return new java.io.stringbufferinputstream(data);
   }

   public java.io.outputstream getoutputstream() throws java.io.ioexception{
      throw new java.io.ioexception("it does not support this method now!");
   }

   public java.lang.string getcontenttype(){
      return type;
   }

   public java.lang.string getname(){
      return "pipi";
   }
}

pipi.mail.html.java
===================
/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-16
功能:发送html邮件
*/

package pipi.mail;

public final class html{
   public static void send(
      java.lang.string smtp,    /*smtp主机地址*/
      java.lang.string from,    /*发信人*/
      java.lang.string to,      /*收信人*/
      java.lang.string cc,      /*抄送人*/
      java.lang.string bcc,     /*暗送人*/
      java.lang.string subject, /*主题*/
      java.lang.string body     /*内容*/
   ) throws java.lang.exception{
      //变量声明
      java.util.properties props;              //系统属性
      javax.mail.session mailsession;          //邮件会话对象
      javax.mail.internet.mimemessage mimemsg; //mime邮件对象

      //设置系统属性
      props = java.lang.system.getproperties(); //获得系统属性对象
      props.put("mail.smtp.host",smtp); //设置smtp主机

      //获得邮件会话对象
      mailsession = javax.mail.session.getdefaultinstance(props,null);

      //创建mime邮件对象
      mimemsg = new javax.mail.internet.mimemessage(mailsession);

      //设置发信人
      mimemsg.setfrom(new javax.mail.internet.internetaddress(from));

      //设置收信人
      if(to!=null){
         mimemsg.setrecipients(javax.mail.message.recipienttype.to,javax.mail.internet.internetaddress.parse(to));
      }

      //设置抄送人
      if(cc!=null){
         mimemsg.setrecipients(javax.mail.message.recipienttype.cc,javax.mail.internet.internetaddress.parse(cc));
      }

      //设置暗送人
      if(bcc!=null){
         mimemsg.setrecipients(javax.mail.message.recipienttype.bcc,javax.mail.internet.internetaddress.parse(bcc));
      }

      //设置邮件主题
      //mimemsg.setsubject(subject);
      mimemsg.setsubject(subject,"gb2312");

      //设置邮件内容
      mimemsg.setdatahandler(new javax.activation.datahandler(new pipi.jaf.stringdatasource(body,"text/html")));

      //发送邮件
      javax.mail.transport.send(mimemsg);
   }
}

本套程序使用到javamail和javabeans(tm) activation framework(jaf):
  javamail
    http://java.sun.com/products/javamail/
  javabeans(tm) activation framework(jaf)
    http://java.sun.com/products/javabeans/glasgow/jaf.html

扫描关注微信公众号