【问题标题】:How to attach an object in an email with Java?如何使用 Java 在电子邮件中附加对象?
【发布时间】:2011-02-13 23:44:48
【问题描述】:

我有如下方法发送邮件,但是我无法获取附件,为什么?

  void Send_Email(String From,String To,String Subject,Text Message_Text,Contact_Info_Entry An_Info_Entry)
  {
    Properties props=new Properties();
    Session session=Session.getDefaultInstance(props,null);
    String htmlBody="<Html>Hi</Html>";
    byte[] attachmentData;
    Multipart multipart=new MimeMultipart();
    BASE64Encoder BASE64_Encoder=new BASE64Encoder();
    try
    {
      Message message=new MimeMessage(session);
      message.setFrom(new InternetAddress(From,"NM-Java Admin"));
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(To,"Ni , Min"));
      // You may need to encode the subject with this special method !
      // message.setSubject(javax.mail.internet.MimeUtility.encodeText("Testing javamail with french : ��� "));
      message.setSentDate(new Date());
      message.setSubject(Subject);

      MimeBodyPart messageBodyPart=new MimeBodyPart();                         // Create the message part
      messageBodyPart.setText(Message_Text.getValue());                        // Fill message
      multipart.addBodyPart(messageBodyPart);

      MimeBodyPart htmlPart=new MimeBodyPart();
      htmlPart.setContent(htmlBody,"text/html");
      multipart.addBodyPart(htmlPart);
//setHTMLContent(message);

      ByteArrayOutputStream bStream=new ByteArrayOutputStream();
      ObjectOutputStream oStream=new ObjectOutputStream(bStream);
      oStream.writeObject(An_Info_Entry);
//      attachmentData=bStream.toByteArray();
//      attachmentData=BASE64_Encoder.encode(bStream.toByteArray());

      MimeBodyPart attachment=new MimeBodyPart();
      attachment.setFileName(An_Info_Entry.Contact_Id);
      attachment.setContent(BASE64_Encoder.encode(bStream.toByteArray()),"application/x-java-serialized-object");
      multipart.addBodyPart(attachment);

      setBinaryContent(message,BASE64_Encoder.encode(bStream.toByteArray()).getBytes());

      message.setContent(multipart);                                             // Put parts in message
      message.setText(Message_Text.getValue()+" [ An_Info_Entry.Contact_Id = "+An_Info_Entry.Contact_Id+" ]");

      Transport.send(message);
    }
    catch (Exception ex)
    {
      // ...
    }
  }

【问题讨论】:

    标签: java email attachment


    【解决方案1】:

    可能是因为:

    1. 您将内容类型设置为:text/html,并且您实际上是在发送原始二进制文件。

      MimeBodyPart attachment=new MimeBodyPart();
      attachment.setFileName(An_Info_Entry.Contact_Id);
      attachment.setContent(attachmentData,"text/html"); // <-- Here
      multipart.addBodyPart(attachment);
      
    2. 即使您使用了正确的内容类型,您也应该使用 base64 对内容进行编码,这样附件才能使其通过网络。

      // This
      //attachmentData=bStream.toByteArray(); 
      // should be  something like:
      attachmentData=Base64Coder.encode( bStream.toByteArray());
      

    【讨论】:

    • 好的,我应该在 attachment.setContent(attachmentData,"??") 中添加什么?
    • 指定附加数据的 MIME 类型。例如,对于 PNG 图像,请使用“image/png”。
    • 可能是application/x-java-serialized-object 更好地了解:en.wikipedia.org/wiki/Internet_media_type
    • 您希望看到什么?你有什么?你有例外吗?截图?
    • 我刚刚用我的最新代码编辑了原始问题,我可以接收电子邮件,但它们没有附件,也没有错误消息。我希望看到附有我的对象的电子邮件,因此我可以将附件中的对象从电子邮件保存到我的 PC。如何添加屏幕截图?
    猜你喜欢
    • 2014-07-26
    • 2011-08-04
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多