用Java发送图文并茂的HTML邮件

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