java 本地生成带附件的eml文件

 
有时候,我们需要把从数据库中查出来的资料,动态生成eml文件进行保存和发送,有的还需要嵌入附件,用什么方法来生成eml文件 ?如何本地生成邮件eml文件。
参考了JAVAMAIL通过IMAP协议保存的eml文件格式后,我们自己也可以本地构造eml文件。
还是看例子吧。
 
 
String mailbody = "";
mailbody = "Message-ID:"+uid+"\r\n";
mailbody = mailbody+"X-FreePOPs-User: "+user+"\r\n";
mailbody = mailbody+"X-FreePOPs-Domain: "+domain+"\r\n";
mailbody = mailbody+"Subject: "+sub+"\r\n";
mailbody = mailbody+"Date: "+sentDate+" \r\n";
mailbody = mailbody+"From: "+from+"\r\n";
mailbody = mailbody+"To: "+to+"\r\n" ;
mailbody = mailbody+"MIME-Version: 1.0\r\n";
BASE64Encoder encoder = new BASE64Encoder();
byte[] data = "body".getBytes();
 try{
   MessageDigest digest = MessageDigest.getInstance("MD5");
   digest.update(((new Random()).nextInt() + "").getBytes());
   data = digest.digest();// Get a romdom integer and get it's digest
 }catch (Exception e) {
  e.printStackTrace();
 }
 String mainBoundary = "****MAIN_BOUNDARY****" + byte2hex(data);
 // Main boundary
 String subBoundary = "****SUB_BOUNDARY****" + byte2hex(data);
		
 mailbody +=  "Content-Type: multipart/mixed;\r\n" + "\tboundary=\"" + mainBoundary
	 + "\"\r\n" + "\r\n" + "This is a multi-part message in MIME format.\r\n" + "\r\n"
 // Above is mail's header
	 + "--" + mainBoundary + "\r\n"
 // The mail's text body
 + "Content-Type: multipart/alternative;\r\n" + "\tboundary=\"" + subBoundary + "\"\r\n" + "\r\n" + "--"
 + subBoundary + "\r\n";
 mailbody += "Content-Type: text/html;\r\n";
 mailbody += "\tcharset=\"utf-8\"\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n"
	 + massage + "\r\n" + "\r\n" + "--" + subBoundary + "--\r\n"// End of  sub boundary
	 + "\r\n";
//		
	 for (int i = 0; i < fjs.size(); i++) {
	  List<Object> attachment = fjs.get(i);
	  String attachName = (String)attachment.get(0);
	  Matcher m = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(attachName);
	 try{
	   if(m.find()){
		 attachName = MimeUtility.encodeText(attachName, "gb18030", "B");
	   }else{
		 attachName = MimeUtility.encodeText(attachName, "utf-8", "B");
	   }
	 }catch (Exception e) {
		 attachName = "attachName_"+i;
		 e.printStackTrace();
	 }
	 try{
	   mailbody += "--" + mainBoundary + "\r\n" + "Content-Type:" + (String)attachment.get(1)
	    + ";\r\n" + "\tname=\"" + attachName + "\"\r\n"
	    + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment;\r\n"
	    + "\tfilename=\"" + attachName + "\"\r\n" + "\r\n"
             + encoder.encode((byte[])attachment.get(2)) + "\r\n" + "\r\n";
	 }catch (Exception e) {
	    e.printStackTrace();
	 }
	 }
	 mailbody += "--" + mainBoundary + "--";
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullFileName), "UTF-8"));
bw.write(mailbody);
bw.flush();
bw.close();
 
其后可以用foxmail等工具打开eml来查看附件是否可以下载,eml内容是否可以解析。



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值