电子邮件系统 6----JavaMail发送带附件的电子邮件示例(2)

  1. /** 
  2.  * CrazyItTest 
  3.  * JavaMail 创建带附件的电子邮件示例 
  4.  */  
  5. package com.labci.javamail.test;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.UnsupportedEncodingException;  
  10. import java.util.Date;  
  11. import java.util.Properties;  
  12. import javax.activation.DataHandler;  
  13. import javax.activation.FileDataSource;  
  14. import javax.mail.Message.RecipientType;  
  15. import javax.mail.MessagingException;  
  16. import javax.mail.Session;  
  17. import javax.mail.internet.AddressException;  
  18. import javax.mail.internet.InternetAddress;  
  19. import javax.mail.internet.MimeBodyPart;  
  20. import javax.mail.internet.MimeMessage;  
  21. import javax.mail.internet.MimeMultipart;  
  22. import javax.mail.internet.MimeUtility;  
  23. /** 
  24.  * @author Bill Tu 
  25.  * @since May 26, 2011(21:03:36 PM) 
  26.  * 
  27.  */  
  28. public class ComplexMailTest {  
  29.  private static MimeMessage getTextMessage(Session session) throws AddressException,   
  30.         MessagingException, UnsupportedEncodingException{  
  31.       MimeMessage message=new MimeMessage(session);  
  32.       String from="[email protected]";//发送方邮件地址   
  33.       String to="[email protected]";//接收方邮件地址   
  34.         
  35.       String subject="带附件的邮件";//邮件主题,注意是中文的   
  36.         
  37.       String content="<h1>欢迎啊</h1><img src="cid:my1.jpg" mce_src="cid:my1.jpg"/>";//cid为my1.jpg,下文会设置此cid   
  38.       message.setFrom(new InternetAddress(from));  
  39.       message.setRecipient(RecipientType.TO, new InternetAddress(to));  
  40.       message.setSubject(subject);  
  41.       message.setSentDate(new Date());//发送时间   
  42.         
  43.       MimeBodyPart picBodyPart=getPicBodyPart(content,"F://My头像.jpg");  
  44.       MimeBodyPart attached1BodyPart=getAttachedBodyPart("F://nginx中文.txt");//注意附件名是中文的  
  45.       MimeBodyPart attached2BodyPart=getAttachedBodyPart("F://nginx英文.doc");  
  46.         
  47.       MimeMultipart mmp=new MimeMultipart("mixed");//MIME消息头组合类型是mixed(html+附件)   
  48.       mmp.addBodyPart(picBodyPart);  
  49.       mmp.addBodyPart(attached1BodyPart);  
  50.       mmp.addBodyPart(attached2BodyPart);  
  51.         
  52.       message.setContent(mmp);  
  53.       message.saveChanges();  
  54.         
  55.       return message;  
  56.     
  57.  }  
  58.    
  59.  /** 
  60.   * 处理文件名 
  61.   * 此处是针对Window下的。 
  62.   * @param filePath 
  63.   * @return 
  64.   */  
  65.  private static String doHandlerFileName(String filePath){  
  66.       String fileName=filePath;  
  67.       if(null !=filePath && !"".equals(filePath)){  
  68.        fileName=filePath.substring(filePath.lastIndexOf("//")+1);  
  69.       }  
  70.       return fileName;  
  71.  }  
  72.    
  73.    
  74.  private static MimeBodyPart getAttachedBodyPart(String filePath) throws MessagingException,  
  75.         UnsupportedEncodingException{  
  76.       MimeBodyPart attached=new MimeBodyPart();  
  77.       FileDataSource fds=new FileDataSource(filePath);  
  78.       attached.setDataHandler(new DataHandler(fds));  
  79.       String fileName=doHandlerFileName(filePath);  
  80.       attached.setFileName(MimeUtility.encodeWord(fileName));//处理附件文件的中文名问题   
  81.       return attached;  
  82.  }  
  83.    
  84.  /** 
  85.   * 处理html加图片的类型(related) 
  86.   * @param content 
  87.   * @param picName 
  88.   * @return 
  89.   * @throws MessagingException 
  90.   */  
  91.  private static MimeBodyPart getPicBodyPart(String content,String picName) throws MessagingException{  
  92.       MimeBodyPart contentPart=new MimeBodyPart();  
  93.         
  94.       MimeMultipart mmp=new MimeMultipart("related");//此处MIME消息头组合类型为related   
  95.       MimeBodyPart contented=new MimeBodyPart();  
  96.       contented.setContent(content,"text/html;charset=gb2312");//因正文内容中有中文   
  97.         
  98.       mmp.addBodyPart(contented);  
  99.         
  100.       MimeBodyPart picBodyPart=new MimeBodyPart();  
  101.       FileDataSource fds=new FileDataSource(picName);  
  102.       picBodyPart.setDataHandler(new DataHandler(fds));  
  103.       picBodyPart.setContentID("my1.jpg");//设置contentId   
  104.         
  105.       mmp.addBodyPart(picBodyPart);  
  106.         
  107.       contentPart.setContent(mmp);  
  108.         
  109.       return contentPart;  
  110.  }  
  111.    
  112.    
  113.  public static void main(String[] args) throws AddressException,   
  114.         MessagingException, FileNotFoundException, IOException {  
  115.       Session session=Session.getDefaultInstance(new Properties());  
  116.       MimeMessage message=getTextMessage(session);  
  117.       message.writeTo(new FileOutputStream("F://mailtext.eml"));  
  118.     
  119.  }  
  120. }  

 

用outlook打开保存在F盘的mailtest.eml文件,查看一下邮件内容:

猜你喜欢

转载自2277259257.iteye.com/blog/2159474