Java Mail attachment not shown in outlook - java

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.

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 download link for attachment

i'm using java mail api to download java mail message attachment.
i am using this link tutorial
download mail attachment
but my requirement is that when user read mail it only display download link for attachment and when click on the link file downloaded that time.
Thanks
Manish
I think you want to do the following:
Check, if it is a multipart message AND consists of several parts
1.1 If so, it has attachments. You should then retrieve the first text/plain or text/html part, which should be the text itself. You leave the other parts alone. But along the text you store some id or reference to the message on the server, that the text is taken from. Consider the following code.
Folder folderInbox = store.getFolder("INBOX");
Message[] arrayMessages = folderInbox.getMessages();
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
}
Now the class javax.mail.Message has a method getMessageNumber()::int.
If you store this message number and the folder name, in this case "INBOX",
you have an identifier, that will allow you to find the message on the server
again at a later time and do something with it, like downloading parts that have
not been retrieved yet.
So (pseudo code here) you basically have to do this.
var message_text = message.getFirstTextBodyPart();
var message_ident = (message.getFolderName(), message.getMessageNumber());
var message = (message_ident, message_text);
saveMessage(message);
showMessage(message);
1.2 In other cases, you download the entire message right now, as there are no parts for later retrieval.

Reading a POP3 message via JavaMail returns only the trailing period, not the content

i have setup a POP3 mail server using MailEnable. I am able to send and receive emails via this server using Mozilla Thunderbird but i encounter a strange problem when reading mails with multipart content (in this case a mail with html content) via the JavaMail API. The data returned from the input stream is always only two CR/LF's with a trailing period!
Below is the relevant part of my message processing code:
for (Message m : messages) {
if (m.isMimeType("multipart/*")) {
System.out.println("Process multipart/* Nachricht");
Multipart mp = (Multipart) m.getContent();
Part part = mp.getBodyPart(0);
System.out.println(part.getContent());
}
}
There is only one Multipart, so i directly access the first element. Also no nested parts are present in the Multipart. I have no idea which causes the problem and it's getting me mad for a week, so i would be very happy if someone could help me on this issue.
Thanks,
fredddmadison
Instead of this
Part part = mp.getBodyPart(0); // What if there's more parts? Or empty parts?
System.out.println(part.getContent()); // No check for empty String?
I would suggest trying this
mp.writeTo(System.out); // Use the optimized write.
System.out.flush(); // Flush the outputstream.
Also, are you sure you're not receiving empty messages?
I have found the problem. It was because i had two different implementations of the JavaMail API on my classpath (The apache geronimo 1.4 which was shipped with EclipseLink as well as the JavaMail API reference implementation, 1.5.1). I have now removed the geronimo implementation from the classpath and it works as expected.

This message uses a character set that is not supported by the InternetService?

I am using Spring JavaMailSender for sending mail from our web-application. Until now it works great, except for this case: one of our customers reported that their email client can't read the characters in email, and show:
This message uses a character set that is not supported by the
Internet Service. To view the original message content, open the
attached message. If the text doesn't display correctly, save the
attachment to disk, and then open it using a viewer that can display
the original character set.
The strange thing is that only one person see this error. Others see the emails just fine. The troubled user is using Hotmail.
I searched through several forums, most of them are about frustrated customers about their email clients, but there's not many solutions/work arounds for server side. Some say that it's because of an in-the-way SMTP server doesn't support Unicode, but I don't think it's the case...
Here's my (simplified) code:
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(new InternetAddress("abc#abc.abc","Hoang Long"));
helper.setReplyTo("abc#abc.abc");
helper.setTo(student.getEmail());
helper.setSubject(emailSubject);
helper.setCc(studioParameterService.getAllCCEmailAddress(studio.getId()));
helper.setSentDate(new Date());
helper.setText(emailContent, true);
for (String filePath : attachFileList) {
FileSystemResource attachFile = new FileSystemResource(filePath);
helper.addAttachment(attachFile.getFilename(), attachFile);
}
mailSender.send(helper.getMimeMessage());

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

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? :-)

Categories