频道澳门葡京手机版网址
登录注册
澳门葡京手机版网址 > 澳门葡京手机版网址 > App开发 > Java > 正文
java 本地生成带附件的eml文件
2011-10-31 15:58:28           
收藏   我要投稿

 

有时候,大家需要把从数据库中查出来的资料,动态生成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内容是否可以解析。 

 

摘自 eagleking012的专栏

点击复制链接 与好友分享!回澳门葡京手机版网址澳门葡京手机版网址
相关TAG标签 附件 文件
上一篇:Java多线程之线程返回值
下一篇:Java 删除数组中重复的元素
相关文章
图文推荐
点击排行

关于大家 | 联系大家 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 澳门葡京手机版网址_澳门新莆京娱乐_www.88807.com - 点此进入--致力于做实用的IT技术学习网站

XML 地图 | Sitemap 地图