Whenever I had problem with Java I always found an answer on stackoverflow without the need to ask a question on my own, but this time even Google cant help me. I just cant figure out the exact problem and try to KISS my question.
When I receive and open a mail created & sent with JavaMail API, everything is fine in Thunderbird. The HTML-part shows up with it's inline-image and the plain text is at the bottom, but Gmail wont show the HTML-part. Instead it just shows the plaintext and only the attachment (so my inline-image isnt visible at all). Gmail says the eMail ist not encoded and I am not sure if this may cause the problem.
The structure of the mail is as followed:
- MimeMultipart mixed
-- BodyPart (attachment)
-- BodyPart (as a container)
--- MimeMultipart alternative
---- BodyPart (plaintext)
---- BodyPart (as a second container)
----- MimeMultipart related
------ BodyPart (html)
------ BodyPart (inline)
Part where I create the message:
MimeBodyPart BPhtml = new MimeBodyPart();
BPhtml.setContent("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head><body>Take a look at this image:<br><br><IMG src=\"cid:fileNamePlusUniqueHash\"></body></html>", "text/html; charset=utf-8");
MimeBodyPart BPinline = new MimeBodyPart();
BPinline.setDataHandler(new DataHandler(new FileDataSource(pathToImage)));
BPinline.setFileName(fileName);
// Note: my CID is a combination of the image's fileName and a salt/hash - unique:
BPinline.setHeader("Content-ID", "<fileNamePlusUniqueHash>");
BPinline.setDisposition(MimeBodyPart.INLINE);
MimeMultipart MPrel = new MimeMultipart("related");
MPrel.addBodyPart(BPhtml);
MPrel.addBodyPart(BPinline);
MimeBodyPart BPcon1 = new MimeBodyPart();
BPcon1.setContent(MPrel);
MimeBodyPart BPplain = new MimeBodyPart();
BPplain.setText("Plain text", "text/plain; charset=utf-8");
MimeMultipart MPalt = new MimeMultipart("alternative");
MPalt.addBodyPart(BPcon1);
MPalt.addBodyPart(BPplain);
MimeBodyPart BPcon2 = new MimeBodyPart();
BPcon2.setContent(MPalt);
MimeBodyPart BPattach = new MimeBodyPart();
BPattach.attachFile(new File(pathToAttachment));
MimeMultipart MPmx = new MimeMultipart();
MPmx.addBodyPart(BPcon2);
MPmx.addBodyPart(BPattach);
MimeMessage message = new MimeMessage(Session.getInstance(properties, null));
message.setContent(MPmx);
// ... set recipients, sender, subject, e.g. ...
Transport.send(message);
Source of eMail:
[...] (delivered, received, subject, e.g., ask for more if important)
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_1248658274.1455785789602"
------=_Part_0_1248658274.1455785789602
Content-Type: multipart/alternative;
boundary="----=_Part_1_860340994.1455785789629"
------=_Part_1_860340994.1455785789629
Content-Type: multipart/related;
boundary="----=_Part_2_743690907.1455785789629"
------=_Part_2_743690907.1455785789629
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>Take a look at this image:<br><br></html><IMG src="cid:blabla.pngPlusUniqueHash"></body>
------=_Part_2_743690907.1455785789629
Content-Type: application/octet-stream; name=blabla.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=blabla.png
Content-ID: <blabla.pngPlusUniqueHash>
iVBORw0KGgoAAAANSUhEUgAAAPEAAAEdCAIAAABSWBYTAAAYpklEQVR4nO2dXawkR3mGR4 [...] (some hundred lines more)
------=_Part_2_743690907.1455785789629--
------=_Part_1_860340994.1455785789629
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
plain text
------=_Part_1_860340994.1455785789629--
------=_Part_0_1248658274.1455785789602
Content-Type: application/octet-stream; name=blabla.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=blabla.pdf
UEsDBBQACAgIAERuSEgAAAAAAAAAAAAAAAALAAAARW50aXR5U3RvcmXsfQl8G8W5+NqxfM [...] (some hundred lines more)
------=_Part_0_1248658274.1455785789602--
I tried a lot of different methods to add the attachment/inline (InputStreamReader eg) but nothing changed. Could you help me to find the mistake? I speculate on an incorrect construction of the message.
Best regards, Fenrir
EDIT I sent the mail to another provider (GMX) and have no problems with the presentation of the HTML-part, so I guess it's a problem with Gmail itself. Is there a best practice to avoid this problem?
UPDATE
So I tried some different mail-constructions and finally got a working Gmail-mail! But the solution is strange and I still dont understand it.
The only thing I changed is the Multipart-Alternative MimeType. Instead of using
MPalt = new MimeMultipart("alternative");
I changed it to
MPalt = new MimeMultipart();
and then added the HTML-BodyPart and Inline-BodyPart to it. But in my opinion this makes no sense - can somebody explain this weird behavior?
Note: There is no difference between the not-working mail-source-'code' and the working mail-source-code. I am confused.
Best regards, Fenrir
EDIT:
Click for screenshot: AT = Attachment, HT = HTML, PL = Plain, IN = Inline
I made a screenshot of my Gmail-inbox and as you can see Google has only problems with PLINHT (a mail created with plain, html and an inline-image) and PLINHTAT.
case PLINHTAT:
mimeMultipart = new MimeMultipart("mixed");
bpHtml = new MimeBodyPart();
bpHtml.setContent(this.htmlText, "text/html; charset=utf-8");
mpRel = new MimeMultipart("related");
mpRel.addBodyPart(bpHtml);
bpInlines = this.getInlineAttachmentBodyParts();
for (int i = 0; i < bpInlines.length; i++)
mpRel.addBodyPart(bpInlines[i]);
bpCon1 = new MimeBodyPart();
bpCon1.setContent(mpRel);
bpPlain = new MimeBodyPart();
bpPlain.setText(this.plainText);
mpAlt = new MimeMultipart("alternative");
mpAlt.addBodyPart(bpPlain);
mpAlt.addBodyPart(bpCon1);
bpCon2 = new MimeBodyPart();
bpCon2.setContent(mpAlt);
mimeMultipart.addBodyPart(bpCon2);
bpAttaches = this.getAttachmentBodyParts();
for (int i = 0; i < bpAttaches.length; i++)
mimeMultipart.addBodyPart(bpAttaches[i]);
break;
case PLINHT:
bpHtml = new MimeBodyPart();
bpHtml.setContent(this.htmlText, "text/html; charset=utf-8");
mpRel = new MimeMultipart("related");
mpRel.addBodyPart(bpHtml);
bpInlines = this.getInlineAttachmentBodyParts();
for (int i = 0; i < bpInlines.length; i++)
mpRel.addBodyPart(bpInlines[i]);
bpCon1 = new MimeBodyPart();
bpCon1.setContent(mpRel);
bpPlain = new MimeBodyPart();
bpPlain.setText(this.plainText);
mimeMultipart = new MimeMultipart("alternative");
mimeMultipart.addBodyPart(bpCon1);
mimeMultipart.addBodyPart(bpPlain);
break;
Two working examples (ATHTIN and PLHT):
case ATHTIN:
bpHtml = new MimeBodyPart();
bpHtml.setContent(this.htmlText, "text/html; charset=utf-8");
mpRel = new MimeMultipart("related");
mpRel.addBodyPart(bpHtml);
bpInlines = this.getInlineAttachmentBodyParts();
for (int i = 0; i < bpInlines.length; i++)
mpRel.addBodyPart(bpInlines[i]);
bpCon1 = new MimeBodyPart();
bpCon1.setContent(mpRel);
mimeMultipart = new MimeMultipart("mixed");
mimeMultipart.addBodyPart(bpCon1);
bpAttaches = this.getAttachmentBodyParts();
for (int i = 0; i < bpAttaches.length; i++)
mimeMultipart.addBodyPart(bpAttaches[i]);
break;
case PLHT:
bpHtml = new MimeBodyPart();
bpHtml.setContent(this.htmlText, "text/html; charset=utf-8");
bpPlain = new MimeBodyPart();
bpPlain.setText(this.plainText);
mimeMultipart = new MimeMultipart("alternative");
mimeMultipart.addBodyPart(bpPlain);
mimeMultipart.addBodyPart(bpHtml);
break;
Still no satisfactory solution.
Okay it seems I solved the problem. I have no idea whats the clue, but I'll post the working construction of the mail (which doesnt differ from the code of my previous post):
case PLINHTAT:
mimeMultipart = new MimeMultipart("mixed");
bpHtml = new MimeBodyPart();
bpHtml.setContent(this.htmlText, "text/html; charset=utf-8");
mpRel = new MimeMultipart("related");
mpRel.addBodyPart(bpHtml);
bpInlines = this.getInlineAttachmentBodyParts();
for (int i = 0; i < bpInlines.length; i++)
mpRel.addBodyPart(bpInlines[i]);
bpCon1 = new MimeBodyPart();
bpCon1.setContent(mpRel);
bpPlain = new MimeBodyPart();
bpPlain.setText(this.plainText);
mpAlt = new MimeMultipart("alternative");
mpAlt.addBodyPart(bpPlain);
mpAlt.addBodyPart(bpCon1);
bpCon2 = new MimeBodyPart();
bpCon2.setContent(mpAlt);
mimeMultipart.addBodyPart(bpCon2);
bpAttaches = this.getAttachmentBodyParts();
for (int i = 0; i < bpAttaches.length; i++)
mimeMultipart.addBodyPart(bpAttaches[i]);
break;
I am still open for explanations. Click to see eMail
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.