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)
{
// ...
}
}