Hi,
I am using java mail api to send mail from a standalone java application. The mail has one html file attachment. I am using the below java program.
// Create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
// Create the content multipart (for text and HTML)
MimeMultipart mpContent = new MimeMultipart("alternative");
// Add text
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("Hello World");
mpContent.addBodyPart(mbp1);
// Add html
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setContent("<P>Hello World</P>", "text/html");
mpContent.addBodyPart(mbp2);
contentPartRoot.setContent(mpContent);
// Add the root body part to the root multipart
mpRoot.addBodyPart(contentPartRoot);
// Add an attachment
MimeBodyPart mbp3 = new MimeBodyPart();
DataSource source = new FileDataSource("javamail.html");
mbp3.setDisposition(Part.ATTACHMENT);
mbp3.setDataHandler(new DataHandler(source));
mbp3.setFileName("javamail.html");
mbp3.setHeader("Content-Type", "text/html");
mpRoot.addBodyPart(mbp3);
message.setContent(mpRoot);
message.saveChanges();
session.setDebug(true);
// Send the message
Transport.send(message);
}
}
In my web mail I am not able to find the attachment in inbox/sent item. However I can see the mail is present there.
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
And welcome to JavaRanch!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: JavaMail - Problem while sending file attachment