Loading...
  所在位置:论坛首页 -> ┈┋电脑网络技术区┋┈ -> Asp/Asp.Net技术 -> 用Java发送图文并茂的HTML邮件
标题:用Java发送图文并茂的HTML邮件收藏 编辑 删除 楼主 | 上一篇 下一篇
点点头
等级:社区游民
权限:普通用户
积分:11
金钱:3259
声望:17
经验:17
发帖数:740
注册:2007年1月16日
资料 短消息2008-6-18 10:39:49
  1. package com.syj;   
  2.   
  3. import java.io.ByteArrayOutputStream;   
  4. import java.io.FileInputStream;   
  5. import java.io.IOException;   
  6. import java.util.Arrays;   
  7. import java.util.Date;   
  8. import java.util.Properties;   
  9.   
  10. import javax.activation.DataHandler;   
  11. import javax.activation.FileDataSource;   
  12. import javax.mail.Authenticator;   
  13. import javax.mail.Message;   
  14. import javax.mail.PasswordAuthentication;   
  15. import javax.mail.Session;   
  16. import javax.mail.Transport;   
  17. import javax.mail.internet.InternetAddress;   
  18. import javax.mail.internet.MimeMessage;   
  19.   
  20. import javax.mail.BodyPart;   
  21. import javax.mail.Multipart;   
  22. import javax.mail.internet.MimeBodyPart;   
  23. import javax.mail.internet.MimeMultipart;   
  24.   
  25. import com.sun.istack.internal.ByteArrayDataSource;   
  26.   
  27. /**  
  28.  * <P>  
  29.  * Title:用java发送邮件的例子  
  30.  * </P>  
  31.  *   
  32.  * <P>  
  33.  * Description:发送图片附件并在html中使用该图片  
  34.  * </P>  
  35.  *   
  36.  * <P>  
  37.  * Copyright: Copyright (c) 2007  
  38.  * </P>  
  39.  *   
  40.  * @author 孙钰佳  
  41.  * @main sunyujia@yahoo.cn  
  42.  * @date Jun 10, 2008 12:35:26 AM  
  43.  */  
  44. public class SendMail {   
  45.     private static String username = "xxxx";   
  46.     private static String password = "xxxx";   
  47.     private static String smtpServer = "smtp.163.com";   
  48.     private static String fromMailAddress = "xxxx@163.com";   
  49.     private static String toMailAddress = "sunyujia@yahoo.cn";   
  50.   
  51.     public static void main(String[] args) throws Exception {   
  52.         Properties props = new Properties();   
  53.         props.put("mail.smtp.auth""true");   
  54.         props.put("mail.smtp.host", smtpServer);   
  55.         // 获得邮件会话对象   
  56.         Session session = Session.getDefaultInstance(props,   
  57.                 new SmtpAuthenticator(username, password));   
  58.         /** *************************************************** */  
  59.         // 创建MIME邮件对象   
  60.         MimeMessage mimeMessage = new MimeMessage(session);   
  61.         mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人   
  62.         mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(   
  63.                 toMailAddress));// 收件人   
  64.         mimeMessage.setSubject("主题");   
  65.         mimeMessage.setSentDate(new Date());// 发送日期   
  66.         Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件   
  67.         /** *************************************************** */  
  68.         BodyPart bodyPart = new MimeBodyPart();// 正文   
  69.         bodyPart.setDataHandler(new DataHandler("测<img src="cid:IMG1" />试",   
  70.                 "text/html;charset=GBK"));// 网页格式   
  71.         /** *************************************************** */  
  72.         BodyPart attachBodyPart = new MimeBodyPart();// 普通附件   
  73.         FileDataSource fds = new FileDataSource("c:/boot.ini");   
  74.         attachBodyPart.setDataHandler(new DataHandler(fds));   
  75.         attachBodyPart.setFileName("=?GBK?B?"  
  76.                 + new sun.misc.BASE64Encoder().encode(fds.getName().getBytes())   
  77.                 + "?=");// 解决附件名中文乱码   
  78.         mp.addBodyPart(attachBodyPart);   
  79.         /** *************************************************** */  
  80.         MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标   
  81.         byte[] bytes = readFile("C:/button.gif");   
  82.         ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,   
  83.                 "application/octet-stream");   
  84.         imgBodyPart.setDataHandler(new DataHandler(fileds));   
  85.         imgBodyPart.setFileName("button.gif");   
  86.         imgBodyPart.setHeader("Content-ID""<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"   
  87.         mp.addBodyPart(imgBodyPart);   
  88.         /** *************************************************** */  
  89.         mp.addBodyPart(bodyPart);   
  90.         mimeMessage.setContent(mp);// 设置邮件内容对象   
  91.         Transport.send(mimeMessage);// 发送邮件   
  92.   
  93.     }   
  94.   
  95.     /**  
  96.      * 读取文件  
  97.      *   
  98.      * @param file  
  99.      *            文件路径  
  100.      * @return 返回二进制数组  
  101.      */  
  102.     public static byte[] readFile(String file) {   
  103.         FileInputStream fis = null;   
  104.         ByteArrayOutputStream bos = null;   
  105.         try {   
  106.             fis = new FileInputStream(file);   
  107.             bos = new ByteArrayOutputStream();   
  108.             int bytesRead;   
  109.             byte buffer[] = new byte[1024 * 1024];   
  110.             while ((bytesRead = fis.read(buffer)) != -1) {   
  111.                 bos.write(buffer, 0, bytesRead);   
  112.                 Arrays.fill(buffer, (byte0);   
  113.             }   
  114.         } catch (IOException e1) {   
  115.             e1.printStackTrace();   
  116.         } finally {   
  117.             try {   
  118.                 if (bos != null)   
  119.                     bos.close();   
  120.             } catch (IOException e) {   
  121.                 e.printStackTrace();   
  122.             }   
  123.         }   
  124.         return bos.toByteArray();   
  125.     }   
  126. }   
  127.   
  128. /**  
  129.  * Smtp认证  
  130.  */  
  131. class SmtpAuthenticator extends Authenticator {   
  132.     String username = null;   
  133.     String password = null;   
  134.   
  135.     // SMTP身份验证   
  136.     public SmtpAuthenticator(String username, String password) {   
  137.         this.username = username;   
  138.         this.password = password;   
  139.     }   
  140.   
  141.     public PasswordAuthentication getPasswordAuthentication() {   
  142.         return new PasswordAuthentication(this.username, this.password);   
  143.     }   
  144.   
  145. }  

2008-6-18 10:39:49 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 商业版本 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com