当前位置:  技术问答>java相关

javamail问题高分相赠!

    来源: 互联网  发布时间:2015-01-05

    本文导语:  程序可执行 ======================= public static void main(String[] args) {   Properties props = System.getProperties();   String smtp = (String)props.get("mail.smtp.host");   if(smtp == null || smtp.equals(""))   {     props.put("mail.smtp.host", "local...

程序可执行
=======================
public static void main(String[] args)
{
  Properties props = System.getProperties();
  String smtp = (String)props.get("mail.smtp.host");
  if(smtp == null || smtp.equals(""))
  {
    props.put("mail.smtp.host", "localhost");
    System.setProperties(props);
  }
  Session session = Session.getDefaultInstance(System.getProperties(), null);
 
  MimeMessage  msg = new MimeMessage(session);
  try
  {
     msg.setFrom(new InternetAddress("test@test.com"));
     msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("test@test.com", false));
     msg.setSubject("subject");
     msg.addHeader("Mime-Version","1.0");

     MimeBodyPart mbp1 = new MimeBodyPart();
   mbp1.setText("Hello");
     mbp1.setHeader("Content-Transfer-Encoding","8bit");
     mbp1.setHeader("Content-Type","text/plain");

   // create the second message part
   MimeBodyPart mbp2 = new MimeBodyPart();

     // attach the file to the message
     FileDataSource fds=new FileDataSource("c:/help.jpg");
   mbp2.setDataHandler(new DataHandler(fds));
   mbp2.setFileName("help.jpg");
     mbp2.setHeader("Content-Transfer-Encoding","base64");
     mbp2.setHeader("Content-Type","image/jpeg");

   // create the Multipart and its parts to it
   Multipart mp = new MimeMultipart();
   mp.addBodyPart(mbp1);
   mp.addBodyPart(mbp2);

   // add the Multipart to the message
   msg.setContent(mp);

     msg.writeTo(new FileOutputStream("c:/windows/temp/mail.txt"));
     
   }catch(Exception e){e.printStackTrace();}
   try
   {
     msg = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null),new FileInputStream("c:/windows/temp/mail.txt"));
     Transport.send(msg);
   }
   catch(Exception e)
   {
      e.printStackTrace();
   }
   System.out.println("OK!");
 }

==========================================
没有寄出时保存的内容mail.txt
==========================================
From: test@test.com
To: test@test.com
Subject: subject
Mime-Version: 1.0

--62667.991642489220.JavaMail.share@window
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

Hello
--62667.991642489220.JavaMail.share@window
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=help.jpg

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQICAQECAQEBAgICAgICAgICAQICAgICAgICAgL/2wBDAQEBAQEBAQEBAQECAQEBAgICAgIC
AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/wAARCAAeADIDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+Z+ii
v3l/4I9ate/Br4Ef8FFv2pNa8e3HgLwj4B+B2i/Crwzf6rP4om8IW/xZ+Meuf2T4T8Wy6L4Y02/n
v9f0XUdM0NIZoNPuLq3i8WMEaKGWaRf9QM+zV5LllXH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K4YZBlFNSUMGoqca0Je/U96OIqOrWjN8
95qpVcqj5r+/KU1aUm3br1XvO+sXst4q0baaWWmnSyP2+/Zy/bx+GfhfWP23f2svHuofCLw9+0h8
Yvhrrfw7+D/wWh8F/GC48IXNl8QPHg8ZfGm88X+KdC0O6mXXdat/tQsNRm8QTX02oa1dw3Vzoelp
aiH85v2gP2rfGXx38IfCj4X/APCP+Hvh/wDCL4J6fr0HgH4feFn1W5t/7b8W38eqeL/GvirWtavp
7rxN4x1G8ht1kuHMNra21nFa6fZWcXnCb5aorHB8N5VgsbUx9OlKpiG6bp88uaNBU6H1eEaUUkkl
Tc/enz1L1ar57VJIueKrTpqk5Whre32m3zNy+fRWWi00CiiiveOc/9k=
--62667.991642489220.JavaMail.share@window--
===============================================
寄出后的邮件内容
===============================================
Message-ID: 
Date: Mon, 4 Jun 2001 07:37:15 +0000 (GMT)
From: test@test.com
To: test@test.com
Subject: subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="2521527.991640235900.JavaMail.share@window"

--2521527.991640235900.JavaMail.share@window
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

Hello
--2521527.991640235900.JavaMail.share@window
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=help.jpg
name: help.jpg

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQICAQECAQEBAgICAgICAgICAQICAgICAgICAgL/2wBDAQEBAQEBAQEBAQECAQEBAgICAgIC
AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgL/wAARCAAeADIDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTV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+GX7NPgX9p74d/FHVvHPgzxT8bfG3wNvtN8UeCIPBWrJrHhvQrXxXo3ifQI7XxPqY1T
w5e6JcXEcwuBZ3Nnd2SoY5RcFbbpy3irAYyOWUsSngswzOjh60aPLUmoxxUas6EZVvZRgpTjRqPl
lyyTi4tXceaJ4SpFVZw/eUqLcXLRaxsnpdu13urq2p8DUUUV9OcoV9n6P+2x4u0L9j/xN+xXp3wn
+D0Pw08Y+Nbf4j+JfFap8W0+JGr+ObHT9P0zTfEF5qkPxbTSpVtrXStN8nTjox0gTWS3D6e9w0kr
/GFFcuKwWFxqoLFUvarDVIVoayXLUpu8J+61dxeqvdJ62uVGcoc3K7cyafo90ff3jL/goj8UfE3g
n9lHwN4e+GPwY+F+n/sXeMT47+BGoeALP4qNqWjeIZvFGleNb+616Hxx8WNbsfEzXPirRrC9lkur
Jp1kEqW80ENxcRS4viL9t3UtZ/af0T9rGw+DPw20n4mWfx48O/tEa/51/wCO/EOma7420PXV8Sz6
LZx+IPFVw/hvwNca81zd/YbVvtMEs0aR3/2a0s7e3+G6K4YZBlFNSUMGoqca0Je/U96OIqOrWjN8
95qpVcqj5r+/KU1aUm3br1XvO+sXst4q0baaWWmnSyP2+/Zy/bx+GfhfWP23f2svHuofCLw9+0h8
Yvhrrfw7+D/wWh8F/GC48IXNl8QPHg8ZfGm88X+KdC0O6mXXdat/tQsNRm8QTX02oa1dw3Vzoelp
aiH85v2gP2rfGXx38IfCj4X/APCP+Hvh/wDCL4J6fr0HgH4feFn1W5t/7b8W38eqeL/GvirWtavp
7rxN4x1G8ht1kuHMNra21nFa6fZWcXnCb5aorHB8N5VgsbUx9OlKpiG6bp88uaNBU6H1eEaUUkkl
Tc/enz1L1ar57VJIueKrTpqk5Whre32m3zNy+fRWWi00CiiiveOc/9k=
--2521527.991640235900.JavaMail.share@window--


=======================================
问题:
没有寄出的信通过writeTo()方法写到文件后与寄出的信的内容主要差别是少了
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="2521527.991640235900.JavaMail.share@windows"
这两句。

如何补回这两句到writeTo()的文件里呢,程序里还要加什么吗。搞来搞去还是不能补上这两句到writeTo()后的文件里,望各位帮手!
因为没有这两句话,用
msg = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null),new FileInputStream("c:/windows/temp/mail1.txt"));
后不能正确的区分附件,收信时你就会知道了。

(我不想手工地插这两句话)

|
pls try to add msg.saveChanges() before you save it.

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 急! 500分求助! javamail VS esmtp server 的不兼容性问题, javamail高手请进
  • javamail附件名称的问题
  • jbuilder ,javamail 问题
  • 关于javamail的奇怪问题,请大家相助!
  • javamail发送问题,急!
  • 关于javamail的奇怪问题,请大家帮忙!
  • javamail的monitor例子问题
  • javamail的问题??谢谢你的关注!!
  • java相关 iis7站长之家
  • 100分求 JavaMail编程实验的环境搭建问题!
  • javamail问题,如果哪位老大帮忙解决了,一定酬谢!!
  • 我用javamail API 读出主题正确,但正文是乱吗?啥问题????
  • 请教javamail发信的身份验证问题!!!
  • javamail的问题
  • 关于javamail中message类的setExpunged方法的问题。
  • JavaMail的问题
  • JavaMail的接收html问题
  • 有关javamail的问题!
  • JavaMail效率问题
  • javamail的中文附件名问题。。。:(
  • 请给一个javamail的程序实例和javamail api 的下载地址100分!
  • 初学javamail
  • 请教熟悉JavaMail的大虾请帮帮忙,多谢了!
  • javamail如何安装?? (45分)
  • 请问使用JavaMail如何将一个邮件原样转发给另外一个人?
  • 请问使用JavaMail如何将一个邮件原样转发给另外一个人?
  • javamail-android
  • 怎样使用javamail发送带有html格式信息的文件
  • 请问JavaMail的API在什么地方(我用的是VisualAge for java 4.0),急!
  • 用javamail怎么可以成批发送
  • 请问各位大虾,不用javaMail如何才能发送附件。


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3