网页教程的分类: .Net编程  Asp编程  Mssql数据库  Mysql数据库  Access数据库  Xml编程  DreamWeaver教程  FireWorks教程  Flash动画教程
网页教程的分类: FrontPage教程  HTML基础教程  PhotoShop教程  动态网站制作  网页设计心得
你现在的位置:首页 >> 网页教程 >> .Net编程 >> 正文
文章标题:  用java发送图文并茂的html邮件
浏览: 来源:网络 作者:admin 发表时间:2008-6-23
  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. }  
网友评论:  用java发送图文并茂的html邮件

网友留言

暂无留言!

本栏热门
本站推荐
热评文章
广告信息