views:

167

answers:

1

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);
//      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)
    {
      // ...
    }
  }
+1  A: 

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());
    
OscarRyz
Ok, so what should I put into attachment.setContent(attachmentData,"??") ?
Frank
Specify the MIME type of the attached data. For a PNG image, for example, use "image/png".
Sean Owen
Probably `application/x-java-serialized-object` To undertand better see: http://en.wikipedia.org/wiki/Internet_media_type
OscarRyz
I've tried all the above, still can't attach object.
Frank
What are you expecting to see? What are you having? Do you have an exception? An screenshot?
OscarRyz
I've just edited the original question with my latest code, I can receive emails, but they have no attachments, and there were no error messages. I expect to see emails with my objects attached, so I can save the attached objects from the emails to my PC. How to add screen shots ?
Frank