邮件例程 - JavaMail - 发送HTML邮件

Java 专栏收录该内容
40 篇文章 0 订阅
<script> 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>  
<script language=JavaScript src="http://www.51banner.com/SSbanner.asp?userid=22&sizeId=9"></script> <script> document.write(" "); document.write("/"飞华统计专家/""); document.write(""); </script> 飞华统计专家

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

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

package com.kjy.nbs.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /** * 发送邮件 * @author kuang_jyu */ public class MailSender { //日志输出 private static final PrintLog4j log = new PrintLog4j(ByteInOut.class); public static void main(String args[]){ /** * Session类 * 该类用于定义整个应用程序所需的环境信息及收集客户端 * 与邮件服务器建立网络连接的会话信息。 * Session对象根据这些信息构建用于邮件收发的Transport和Store对象, * 以及为客户端创建Message对象时提供信息支持。 */ Session session = Session.getDefaultInstance(new Properties()); /** * Message类 * 是创建和解析邮件的核心API,它的实例对象代表了一封电子邮件 */ MimeMessage msg = createMessage(session); try { msg.writeTo(new FileOutputStream("D:\\WorkSpace\\webBus\\ComplexMessage.eml")); log.printException("成功发送邮件!"); } catch (FileNotFoundException e) { log.printException("没有此地址,发送邮件失败!"); e.printStackTrace(); } catch (IOException e) { log.printException("发生异常,发送邮件失败!"); e.printStackTrace(); } catch (MessagingException e) { log.printException("邮件异常,发送邮件失败!"); e.printStackTrace(); } } public static MimeMessage createMessage(Session session){ String from = "benkuangjianyu@163.com"; String to = "benkuangjianyu@163.com"; String subject = "HTML邮件"; String body = "<h4>welcome to here</h4>"; MimeMessage msg = new MimeMessage(session); try { msg.setFrom(new InternetAddress(from)); /** * RecipientType是Message的静态内部类 To代表邮件的主要接受者 * setRecipient方法设置一个收件人的地址 */ msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); /** * 设置发送邮件的主题 */ msg.setSubject(subject); /** * 该方法用于设置MimeMessage对象的邮件体 */ MimeMultipart allMultipart = new MimeMultipart("mixed"); MimeBodyPart part = createContent(body,"D:\\WorkSpace\\webBus\\jMail\\mail.jpg"); MimeBodyPart attach = createAttach("D:\\WorkSpace\\webBus\\jMail\\cust.zip"); allMultipart.addBodyPart(part); allMultipart.addBodyPart(attach); msg.setContent(allMultipart); msg.saveChanges(); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } return msg; } public static MimeBodyPart createContent(String body, String fileName){ MimeBodyPart contentPart = new MimeBodyPart(); MimeMultipart contentMultipart = new MimeMultipart("related"); MimeBodyPart htmlBodyPart = new MimeBodyPart(); try { htmlBodyPart.setContent(body,"text/html;charSet=gb2312"); contentMultipart.addBodyPart(htmlBodyPart); MimeBodyPart gifBodyPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileName); gifBodyPart.setDataHandler(new DataHandler(fds)); gifBodyPart.setContentID("myMail_jpg"); contentMultipart.addBodyPart(gifBodyPart); contentPart.setContent(contentMultipart); } catch (MessagingException e) { e.printStackTrace(); } return contentPart; } public static MimeBodyPart createAttach(String fileName){ MimeBodyPart attachPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileName); try { attachPart.setDataHandler(new DataHandler(fds)); attachPart.setFileName(fds.getName()); } catch (MessagingException e) { e.printStackTrace(); } return attachPart; } }
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值