JavaMail邮件发送-发送带附件的邮件 转的

之前已经看到,如果消息包指定了他们之间的关系,增加一个文件进去,邮箱就会显示成普通附件

但是那毕竟不是附件,附件就是附件不能含糊

 

其实增加附件很简单

指定消息包内元素为混合关系

增加附件并指定附件的名称

 

代码如下:

Java代码   收藏代码
  1. package com.mail;  
  2. import java.util.Date;  
  3. import java.util.Properties;  
  4. import javax.activation.DataHandler;  
  5. import javax.activation.FileDataSource;  
  6. import javax.mail.Message;  
  7. import javax.mail.Session;  
  8. import javax.mail.Transport;  
  9. import javax.mail.internet.InternetAddress;  
  10. import javax.mail.internet.MimeBodyPart;  
  11. import javax.mail.internet.MimeMessage;  
  12. import javax.mail.internet.MimeMultipart;  
  13. import javax.mail.internet.MimeUtility;  
  14. public class Html_File_InnerFile {  
  15.     public static void main(String[] args) throws Exception {  
  16.         Properties props = new Properties();  
  17.         props.put( "mail.smtp.host ""smtp.163.com ");  
  18.         props.put("mail.smtp.auth""true");  
  19.         Session session = Session.getInstance(props);  
  20.         Message message = new MimeMessage(session);  
  21.         InternetAddress from = new InternetAddress("test20120711120200@163.com");  
  22.         from.setPersonal(MimeUtility.encodeText("风中落叶<test20120711120200@163.com>"));  
  23.         message.setFrom(from);  
  24.         InternetAddress to = new InternetAddress("test20120711120200@163.com");  
  25.         message.setRecipient(Message.RecipientType.TO, to);  
  26.         message.setSubject(MimeUtility.encodeText("强哥邀请,谁敢不从!"));  
  27.         message.setSentDate(new Date());  
  28.         MimeMultipart msgMultipart = new MimeMultipart("mixed");// 指定为混合关系  
  29.         message.setContent(msgMultipart);  
  30.         // 邮件内容  
  31.         MimeBodyPart htmlPart = new MimeBodyPart();  
  32.         htmlPart.setContent(  
  33.                         "<body background='http://dl.iteye.com/upload/picture/pic/110267/e244bda9-9034-36e3-87fd-807629b84222.jpg'>"  
  34.                                 + "<div style='position: absolute; left: 390px; top: 150px;height: "  
  35.                                 + "100px;width: 200px;' align='center'>"  
  36.                                 + "<font color='red'>这是测试邮件,请勿回复</font>" + "</div></body>",  
  37.                         "text/html;charset=UTF-8");  
  38.         // TODO 组装的顺序非常重要,一定要先组装文本域,再组装文件  
  39.         msgMultipart.addBodyPart(htmlPart);  
  40.         // 组装附件  
  41.         MimeBodyPart file = new MimeBodyPart();  
  42.         FileDataSource file_datasource = new FileDataSource("D:\\img201008031058340.zip");  
  43.         DataHandler dh = new DataHandler(file_datasource);  
  44.         file.setDataHandler(dh);  
  45.         // 附件区别内嵌内容的一个特点是有文件名,为防止中文乱码要编码  
  46.         file.setFileName(MimeUtility.encodeText(dh.getName()));  
  47.         msgMultipart.addBodyPart(file);       
  48.         message.saveChanges();  
  49.         Transport transport = session.getTransport("smtp");  
  50.         transport.connect("smtp.163.com"25"test20120711120200""test123456");  
  51.         transport.sendMessage(message, message.getAllRecipients());  
  52.         transport.close();  
  53.         System.out.println("发送完毕");  
  54.     }  
  55. }  

  

主要组装元素的时候一定要先组装邮件内容再组装附件

 无论在 客户端工具还是邮箱,都是提示附件,邮件还是带背景图片的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值