Send email via SMTP with attachment, plain/text, and text/hml
Asked Answered
J

1

12

My goal: Send transactional emails via SMTP with plain/text, text/html, and attachments.

My code: Implemented with JavaMail

My issue: It looks fine on hotmail, or outlook. But on gmail, it does not show the message body properly if it is an email with a .txt attachment (it works alright if attachments are images)

Any help would be highly appreciated.

Here is my raw SMTP output:

Subject: ALTERNATIVE | TXT | HTML |ATT.ATTACHMENT | Thu Jun 13 17:48:04 EDT
 2013
MIME-Version: 1.0
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_21791733.1371160084561"

------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in text format!
------=_Part_0_21791733.1371160084561
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Body message in <b>html</b> format! Sent on Thu Jun 13 17:48:04 EDT 2013<br> to: [email protected]<br> to: [email protected]<br> cc: [email protected]<br> cc: [email protected]
------=_Part_0_21791733.1371160084561
Content-Type: text/plain; charset=us-ascii; name=email_attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=email_attachment.txt

This is a text attachment file!
------=_Part_0_21791733.1371160084561--
.
250 Delivery in progress
QUIT

Some screenshots

Sent with only one .txt attachment. The message body does not display and attachment are duplicated.

enter image description here

Same message but with different attachment (.gif). Everything looks fine. enter image description here

=== SOLUTION FOR JAVA DEVELOPERS ====

The overall idea is described here: http://www.coderanch.com/t/503380/java/java/Java-Mail-text-html-attachment

So, now my code looks like:

// contentPart is the content to be sent. It is divided in bodyContent and attachmentContent
            MimeMultipart contentPart = new MimeMultipart("mixed");

            // Message body in txt and html format
            MimeMultipart bodyPart = new MimeMultipart("alternative");
            // Creates plain text message
            BodyPart bodyTxt = new MimeBodyPart();
            bodyTxt.setText(getMessageBodyText());
            // Creates html message
            BodyPart bodyHtml = new MimeBodyPart();
            bodyHtml.setContent(getMessageBodyHtml(), "text/html");
            bodyPart.addBodyPart(bodyTxt);
            bodyPart.addBodyPart(bodyHtml);

            // Wrapper for bodyTxt and bodyHtml
            MimeBodyPart bodyContent = new MimeBodyPart();
            bodyContent.setContent(bodyPart);

            // At this point, contentPart contains bodyTxt and bodyHtml wrapped in a multipart/alternative
            contentPart.addBodyPart(bodyContent);

            // Adds attachments to contentPart
            if (getAttachments() != null) {
                for(File f : getAttachments()) {
                    try {
                        MimeBodyPart attachmentPart = new MimeBodyPart();
                        attachmentPart.attachFile(f);
                        contentPart.addBodyPart(attachmentPart);
                    } catch (IOException e) {
                        logger.severe("Could not attach file to email!" +
                                " TO: "+ getTo().toString() +
                                "; CC: "+ getCc().toString() +
                                "; ExceptionMessage: " + e.getMessage());
                        throw new SmtpRequestException(e.getMessage());
                    }
                }
            }
Jemappes answered 13/6, 2013 at 21:53 Comment(0)
A
13

The structure of your message is wrong. You need nested multiparts to get the right structure, something like this:

  multipart/mixed
    multipart/alternative (holding the two forms of the body part)
      text/plain
      text/html
    text/plain or image/gif (the attachment)
Amara answered 14/6, 2013 at 18:49 Comment(6)
That's right. Actually, I implemented the solution for this issue a little before seeing your answer. Thank you very much.Jemappes
What if some of the attachments are actually pictures, embedded in the text/html part? What does the right structure looks like, then? Can I simply replaced "mixed" with "related"? Or do I have to use different subparts for the "related" attachments with INLINE disposition, and "mixed" for the rest? I am VERY confused... :(Dismount
Keep with mixed. The answer presented here worked greatly with text and binary files.Jemappes
If the text/html part is referencing the images using "cid:" references, they all should be in a multipart/related. Don't replace the multipart/mixed in the example above. Instead, replace the text/html with a multipart/related that includes the text/html and the image/jpg that it references. Let me know if that's not clear.Amara
Your hierarchy looks as if you're attaching a Multipart to a Multipart. Specifically, how is a multipart/alternative Object attached to a multipart/mixed Object?Moluccas
Multipart mp = new MimeMultipart(); Multiaprt mpa = new MimeMultipart("alternative"); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent(mpa); mp.addBodyPart(mbp); Amara

© 2022 - 2024 — McMap. All rights reserved.