i am sending the inline image with email with mime message. Here is the brief code for the same. This is working fine. My
question is i am not setting the MimeMessage content-type as multipart/related (Also not setting
multipart subtype as related)still my code is working fine and iam able to get the inline image at expected postion.
Should i really care about setting the Content-Type as multipart/related when i am referring the image part with cid
or server takes care of that?
MimeMessage msg = new MimeMessage(mailSession);
MimeMultipart mpart = new MimeMultipart();
MimeBodyPart bp = new MimeBodyPart();
bp.setText("plain text and here is html image refering image part <img src="cid:Unique-ContentId" />", CHARSET_UTF_8, MESSAGE_HTML_CONTENT_TYPE);
// add message body
mpart.addBodyPart(bp);
// adding inline image part
MimeBodyPart bodyPart1 = new MimeBodyPart();
bodyPart1.setFileName("inline image");
file1 = new File("image1");
DataSource source1 = new FileDataSource(file);
bodyPart1.setDataHandler(new DataHandler(source));
bodyPart1.setDisposition(MimeBodyPart.INLINE);
bodyPart1.setHeader("Content-ID", "Unique-ContentId");
bodyPart1.setHeader("Content-Type", "image/jpeg");
mpart.addBodyPart(bodyPart1);
// At last setting multipart In MimeMessage
msg.setContent(mpart);
Just for information my email client can be outlook,lotusnotes,yahoo,gmail,thunderbird
That's what we call "luck". :-)
Apparently the email clients you're using are being very generous in the way they interpret the message you're sending. There's nothing in the email specs that suggest they should interpret such messages in this way.
Related
I am trying to send meeting invite using the Javamail.
In multipart I am creating three mimebodypart,
1.HTML mail body part
2.Calendar Inivte Part
3.Signature Image part (this part is the problem )
but when I send the mail, the signature image part gets converted to AT00001.bin and shown instead of signature image.
The image is shown if I remove the calendar invite part from mail.
After some research I came to know that the the attachment part must be the last in the mail so to avoid this file creation, but after doing so the problem continues.(as you can see in the mimebody addpart sequence)
The code part is as below:
Multipart multipart = new MimeMultipart("mixed");
BodyPart messageBodyPart = buildHtmlTextPart(); //html is read and added to the mail body part
BodyPart calendarPart = buildCalendarPart();
BodyPart signatureImagePart = buildSignatureImagePart(); //image is read and added as a content part of html.
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(calendarPart);
multipart.addBodyPart(signatureImagePart);
buildSignatureImagePart() is as below,
MimeBodyPart signatureimagepart = new MimeBodyPart();
DataSource fds = new FileDataSource(filePath); //filepath is image file location
signatureimagepart.setDataHandler(new DataHandler(fds));
signatureimagepart.setHeader("Content-ID", "<my-image-id>");
buildHtmlTextPart() is as below,
MimeBodyPart descriptionPart = new MimeBodyPart();
descriptionPart.setContent("<html><body><b>Test</b> email <img src='cid:my-image-id'></body></html>", "text/html; charset=utf-8");
Please tell me if i am doing something wrong in this to get the image part.
Is there any other way to do the same?
The structure of your message is wrong.
What you want is an outer maultipart/mixed, the first body part of which is a multipart/related, the second body part of which is the calendar attachment. The multipart/related has two parts - the html text and the signature image that it references.
I'm trying to embed a link in email for a file attached in email message using JavaMail APi like here.
This is my code:
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String attachment = "/path/test.pdf";
File fAtachh = new File(attachment);
String htmlText = "<a href='cid:file'>test.pdf</a>";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPartAttach = new MimeBodyPart();
try {
messageBodyPartAttach.attachFile(fAtachh);
} catch (IOException e) {
logger.info("Exception" + e.getMessage());
}
messageBodyPartAttach.setContentID("<file>");
multipart.addBodyPart(messageBodyPartAttach);
message.setContent(multipart);
The problem is that link doesn't work:
ErrorLink
If change the last code by:
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart messageBodyPart = new MimeBodyPart();
String attachment = "/path/test.pdf";
String htmlText = "<a href='cid:file'>test.pdf</a>";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPartAttach = new MimeBodyPart();
DataSource fds = new FileDataSource
(attachment);
messageBodyPartAttach.setDataHandler(new DataHandler(fds));
messageBodyPartAttach.setHeader("Content-ID","<file>");
multipart.addBodyPart(messageBodyPartAttach);
message.setContent(multipart);
The link Works but the file name and extension are wrong:
WrongFileName
I've tried to change the file name by method:
messageBodyPartAttach.setFileName("test.pdf");
but if I set the file name, the link doesn´t work like in the first code.
Any suggestion?
Thanks!
The attachFile method sets the filename and sets the Content-Disposition to ATTACHMENT. In a multipart/related message you probably don't want those set, although it likely depends on what mail reader you're using to display the message.
There should be no functional different between using the setContentID method and using the setHeader method with "Content-ID".
Normally multipart/related messages are used to allow the html part to reference an image part also included in the message, and displayed along with the html part. Probably no mail readers are going to display a pdf file inline with the html.
Maybe the question to ask is, what exactly are you trying to accomplish?
If you just want the pdf file to appear as an attachment with a correct filename that the user can then click on to save or view, you should use the (default) multipart/mixed instead of multipart/related. I'm not sure there's a good way to embed the link to the attached file in the html text. If you're able to create such a message with some other mailer, you can examine the structure of that message and replicate it using JavaMail.
I'm having an issue with sending attachments via email in java using the java mail (1.4.6). It seems that when I attach a document of any sort and send it, the recipient gets the file in plain text format in the body. Rather than, you guessed it, sending the whole file like you would expect and the body not interfered with.
Code
try
{
// Create a message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(Compose.to));
message.setSubject(Compose.subject);
//message.setText(Compose.body);
//If there are no CC's then skip it. This if seemed to decrease send time.
if(Compose.cc != null)
{
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(Compose.cc));
message.saveChanges();
}
else
message.saveChanges();
/*
* For adding the attached file to the email. This time the if
* statement is used to stop the email attachment process if there
* is none. Other wise due to the way I've set it up it'll try to
* send file path and file name as null, and we fail an otherwise valid email.
*/
if(Compose.filename != null)
{
String file = Compose.filepath;
String fileName = Compose.filename;
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(Compose.body);
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
}
else
{
message.setText(Compose.body);
message.saveChanges();
}
//Send the message by javax.mail.Transport .
Transport tr = session.getTransport("smtp"); // Get Transport object from session
tr.connect(smtphost, username, password); // We need to connect
tr.sendMessage(message, message.getAllRecipients()); // Send message
//Notify the user everything functioned fine.
JOptionPane.showMessageDialog(null, "Your mail has been sent.");
}
Thinking over this I remembered how FileDataSource() is an overloaded statement taking a string or a file type as a parameter, trying both I got the same result but I will experiment more with the the file type now.
EDIT: After more testing I noticed that sometimes the file will not appear along with whatever was in the body at the time of sending.
For each part you have to set disposition to Part.INLINE for the body and Part.ATTACHMENT for the attachment. The attachFile methods will do that for you. Avoid JavaMail 1.4.6 in favor of the latest release or at least use JavaMail 1.4.7 which contains fixes for known issues with JavaMail 1.4.6.
You're setting the attachment as the first body part. It needs to be the second body part.
Also, consider upgrading to JavaMail 1.5.4 and using the MimeBodyPart.attachFile method to attach the file.
I have been trying to get a solution but I am not able to. Here is the whole thing. I wrote the following code
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setContent(messageContent, "text/html");
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(messagePart);
MimeBodyPart attachmentPart = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(attachment.getBytes(), "text/plain");
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName(attachmentFileName);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
try {
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
The code is pretty much self explanatory. messagePart is the mail and attachmentPart is the attachment. But messagePart here is not plain text. It is HTML. So the problem is if I run this code mail is sent successfully but the attachment does not come attached to the mail. So my question boils down to this. Is it possible to send html content and attach something to a mail at the same time. I am stuck here. Could anyone help please.
I don't see anything obviously wrong with your code.
How are you determining that the message has no attachment?
You can see exactly what JavaMail would send by adding "message.writeTo(System.out);" just before you call Transport.send.
What version of JavaMail are you using? What mail server are you using? Some mail servers (I'm looking at you, Exchange) will reformat the message to what they think it should be, even if it's different than what you intended.
If you are running on Google App Engine and you add a dummy text part it works great. I have no idea why the dummy text part makes it work, but I was having the exact same problem and adding a dummy text part worked for me too.
You want to add
attachmentPart.setDisposition(Part.ATTACHMENT);
Part.ATTACHMENT means the part should be showed as an attachment.
Part.INLINE means you want to show the attachment as part of the message.
When I send an e-mail currently from within GAE I receive the email with a Content-Transfer-Encoding of quoted-printable. I am looking to set this to base64. The quoted-printable would be find except the image is not displayed when I receive the email. As it is right now my html which looks like this:
String base64StringImg = Base64.encode(my byte array);
StringBuilder htmlBody = new StringBuilder();
htmlBody.append("<html>");
htmlBody.append("<body>");
htmlBody.append("<img src='data:image/png;base64,");
htmlBody.append(base64StringImg);
htmlBody.append("'/>");
htmlBody.append("<br/><br/>");
htmlBody.append("Hello " + name);
htmlBody.append("</body>");
htmlBody.append("</html>");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromUser));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject(subject);
Multipart mp = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlBody.toString(), "text/html; charset=UTF-8");
mp.addBodyPart(htmlPart);
message.setContent(mp);
Transport.send(message);
How can I send an html e-mail with an image in GAE? I have read the following two bug/feature requests which make it clear there are limitations.
http://code.google.com/p/googleappengine/issues/detail?id=198
http://code.google.com/p/googleappengine/issues/detail?id=965
HTML embedded images seem to be poorly supported in email clients: http://www.campaignmonitor.com/blog/post/1761/embedding-images-in-email/
What is supported are images HTML images attached to email: http://www.campaignmonitor.com/blog/post/1759/embedding-images-revisited/
However, as you noted with the link to the issue, the second option is poorly supported in GAE. What you could try is create by hand the mail content shown in second link.