开发者

How to attach an object in an email with Java?

开发者 https://www.devze.com 2022-12-28 20:59 出处:网络
I have the following method to send an email, but I couldn\'t get the attached object, why ? void Send_Email(String From,String To,String Subject,Text Message_Text,Contact_Info_Entry An_Info_Entry)

I have the following method to send an email, but I couldn't get the attached object, why ?

  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);
//    开发者_如何学Python  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)
    {
      // ...
    }
  }


Probably because:

  1. You're setting your content type as: text/html and you're really sending raw binary.

    MimeBodyPart attachment=new MimeBodyPart();
    attachment.setFileName(An_Info_Entry.Contact_Id);
    attachment.setContent(attachmentData,"text/html"); // <-- Here
    multipart.addBodyPart(attachment);
    
  2. Even if you use the right content-type, you should encode the content as using base64 that way the attachment could make it trough the net.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号