Message missing in e-mail depending on order of bodypart adds - java

The problem I'm experiencing is when I add the message Body part of the e-mail to the message object before I add the attachments the body of the e-mail doesn't show up, but when I add the message body part after all the attachments it shows up fine.
This is weird, but I have an e-mail I'm trying to send using JavaMail. It has all the regular stuff you need for an e-mail (addresses, etc). The "email" object you'll see below is a Javabean that holds mimeBodyParts for attachments as well as a mimeBodyPart for the message body, subject, etc...
Here's the code that does not work (as described above)
Multipart multipart = new MimeMultipart("alternative");
message.setSubject(email.getSubject());
multipart.addBodyPart(email.getContentBodyPart()); //This is the only line that moves
for (MimeBodyPart mimeBodyPart : email.getBodyParts()) {
multipart.addBodyPart(mimeBodyPart);
}
message.setContent(multipart);
Here's the code that does work:
Multipart multipart = new MimeMultipart("alternative");
message.setSubject(email.getSubject());
for (MimeBodyPart mimeBodyPart : email.getBodyParts()) {
multipart.addBodyPart(mimeBodyPart);
}
multipart.addBodyPart(email.getContentBodyPart()); //This is the only line that moved
message.setContent(multipart);
If you need more info about the email javabean I'll give it to you (or you can find the entire object code here), but I'm guessing I'm just missing something simple. Thanks in advance.

Just to clarify for anyone else reading this: If you use an "alternative" MimeMultipart, all its parts should be alternative versions of the same content. Also according to the relevant RFCs the preferred version of the content should be added last. You do this a lot when creating an HTML email with a plain text fallback. This is why they warn you in the JavaMail docs to read the RFCs.

If you're creating a message with attachments, why are you using a multipart/alternative? You should be using the (default) multipart/mixed.
Did you cut and paste that code without understanding it? :-)

Related

Body content is not being sent while I am trying to send an email with attachment with main body content in Java methods of MimeMessage

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setFileName("abc.xls");
message.setText("Fill the content:");
Above is the main part of code which I am using. While I use the above code, i don't see main body content "Fill the content" in the sent mail. There are other posts and comments which have piece of code which works that's this problem cab be resolved by using MimeMultipart & MimeBodyPart class. But no where it's explained the reason of why the above code is not working.
I am also aware that using setFileName is not enough to add the content present inside the file, it's just used to add the attachment without content.
Note: I am using javax.mail-1.5.0.jar
Can you please explain the reason of the above code not working?
Thanks in advance.
A mail that contains a text message and one or more attachments must be a MultiPart message, because that's the way such a mail is constructed so the receiving mail client understands it.
In your simple example, you are not constructing a mail that has an excel file abc.xls as attachment; instead, you create a text mail and tell the client that the body of this mail should be named abc.xls. Most likely, the receiving mail client will offer a text file with the content Fill the content:, inappropriately named abc.xls, as an attachment of an otherwise empty mail; opening the supposed Excel file will probably cause Excel to import this text file.
TL;DR: Use MimeMultiPart to create mails with attachments.

Java Mail attachment not shown in outlook

we use Java Mail to send E-Mails with PDF attachments via SMTP over Lotus Notes to our customers.
Some time ago we got notified that serveral customers don't received an attachment.
One of these customers uses Microsoft Outlook and got an attachment flag in his inbox. But when he opens the
E-Mail, he doesn't see an attachment. We don't have the possibility to check the version of the E-Mail client's
and to do customer side test's, because our customers are worldwide located.
If our customer responds or (internal) forward the E-Mail, the attachment shown in receiver's E-Mail client.
The following part is the affected Java source code:
private static Multipart createMultipartMailWithAttachment(String messageText)
throws MessagingException {
// Message with attachments
Multipart mp = new MimeMultipart();
// Attach Text
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(messageText, UTF8, HTML);
mp.addBodyPart(mbp1);
for (File f : attachments) {
MimeBodyPart fileAttachment = new MimeBodyPart();
try {
fileAttachment.setDisposition(MimeBodyPart.ATTACHMENT);
fileAttachment.attachFile(f);
if(f.getName().toLowerCase().endsWith(PDF_EXTENSION)) {
fileAttachment.setHeader(CONTENT_TYPE, APPLICATION_PDF);
}
} catch (IOException e) {
returnMessage = e.getMessage();
}
mp.addBodyPart(fileAttachment);
}
return mp;
}
We already tested different webmail services like gmail.com, yahoo.com and outlook.com. In every case the
attachment was shown. Also in an local installation of Mozilla Thunderbird, Microsoft Outlook or Lotus Notes
was the attachment shown.
After many inquiries we got many different solution processes. See setDisposition(MimeBodyPart.ATTACHMENT) and
setHeader(CONTENT_TYPE, APPLICATION_PDF). None of these solutions lead us to success. Does anyone know
a solution or a new solution process to solve that problem?
We had a similar problem where we sent file attachments from a J2EE application to various mail accounts. We utilized SMTP gmail server (smtp.gmail.com) with port 465 and HTTPS connection type for our outgoing messages.
Attachments to the messages sent via Java were not shown in Outlook but we could observe them in web interface for gmail accounts.
In our case, it turned out to be that MimeMultipart construction was not the correct one. We had
Multipart multipart = new MimeMultipart("alternative");
When we modified it to
Multipart multipart = new MimeMultipart();
the attachments became visible.
Please also refer to the following resource for a full explanation.
If messages sent from other mailers work properly and only messages sent from JavaMail fail, you'll need to examine the raw MIME content of the working and non-working messages to see what's different. You should be able to reproduce whatever content works using JavaMail.
There's lots of ways to access the raw MIME content of messages; let me know if you need help with that.
Obviously you'll need working and non-working examples messages with similar content to compare. If you have a repeatable test case - a message you can send to the same recipient multiple times and it fails every time - that would be most helpful.
I wanted to add a comment (question), but don't have the required points.
It could be a filter on the customer side that blocks attachments. See if the customer is getting a mail with attachment (same pdf) when sending it via normal outlook.
Second possible cause is the size of the attachment is a problem for the customer.
Third possible cause is that I have noticed that when I set up a rule to automatically put messages into a different folder, i see a message saying:
Links and other functionality have been disabled in this message. To turn on that functionality, move this message to the Inbox. Outlook blocked access to the potentially unsafe attachments: xxxx.pdf
Maybe this customer has setup a rule similarly.

java mail - processing csv attachment

I am receiving e-mails set from a normal GUI client such as thunderbird or outlook, which conntain message body as String and one csv file attachment.
Using Java mail I would like to process the attachment i.e. read in the contet of the csv file into a String. Can I simply check whether the contet is a Multipart and then process the one at index 1 ( I guess e-mail body is at index 0)? Or should I check for dispositions which flags which part is an attachment? My only cocern is that if oe seds an e-mail using a regular GUI client, these flags are not set...
Any comments on what approach to take?
This JavaMail FAQ entry will help.
According to the Javamail tutorial
The content of your message is a Multipart object when it has
attachments. You then need to process each Part, to get the main
content and the attachment(s). Parts marked with a disposition of
Part.ATTACHMENT from part.getDisposition() are clearly attachments...

How to add a message as an attachment on another message with javax.mail

I've got a mailer daemon that check a box and fetch the mails inside every X mins.
When an error occurs, I need to send a alert mail, with the faulty message as an attachment.
I dont want to create a file, write the content from the original message and then add the file to the error message, I want to directly set the MimeBodyPart content from the original stream.
I already did this some years ago, so I know it's possible, I just can't remember the exact syntax to copy the message stream to the MimeBodyPart and set the content type ( which is RFC2822 )
Anyone can help ?
Here's the solution that I found:
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(forwardedMsg, "message/rfc822");
mp.addPart(mbp);

How do I send a binary attachment in an email with Java using the JavaMail API?

Using JDK1.5 how does one send a binary attachemnt (such as a PDF file) easily using the JavaMail API?
Have you looked at the JavaMail FAQ? It seems to have little snippets to demonstrate the process (and how to fix a common problem -- running out of memory).
Assuming that you don't want to read some links and don't want any external dependencies, you need to use MimeMultipart and BodyPart:
MimeMultipart messageContent = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(yourFile);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName("MyFile.ext");
bodyPart.setDisposition(Part.ATTACHMENT);
// Then add to your message:
messageContent.addBodyPart(bodyPart);
Attaching a body to the messages is just attaching a BodyPart with disposition Part.INLINE
If you want to do it easily I'd suggest using Commons-Email! It's built on the JavaMail API, but it makes it much simpler.
There is a sample in the User Guide on how to send email with attachments... it's much easier than using the standard JavaMail API!

Categories