简体   繁体   English

GMail不显示使用JavaMail API发送的电子邮件的HTML- / Inline-部分

[英]GMail doesnt show HTML-/Inline-part of eMail sent with JavaMail API

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. 每当我遇到Java问题时,我总能找到关于stackoverflow的答案,而无需自己提出问题,但这一次Google都无法帮助我。 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. 当我接收并打开使用JavaMail API创建和发送的邮件时,在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. HTML部分显示为内嵌图像,纯文本显示在底部,但Gmail不会显示HTML部分。 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. Gmail表示电子邮件ist未编码,因此我不确定是否可能导致此问题。

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. 我尝试了很多不同的方法来添加附件/内联(例如InputStreamReader),但没有任何改变。 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. 编辑我已将邮件发送给另一个提供商(GMX),并且HTML部分的显示没有问题,所以我认为这是Gmail本身的问题。 Is there a best practice to avoid this problem? 是否有最佳实践来避免此问题?

UPDATE UPDATE

So I tried some different mail-constructions and finally got a working Gmail-mail! 因此,我尝试了一些不同的邮件构造,最后得到了可以使用的Gmail邮件! But the solution is strange and I still dont understand it. 但是解决方案很奇怪,我仍然不明白。

The only thing I changed is the Multipart-Alternative MimeType. 我唯一更改的是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. 然后向其中添加HTML-BodyPart和Inline-BodyPart。 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 单击此处以截取屏幕截图: AT =附件,HT = HTML,PL =普通,IN =内联

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. 我为我的Gmail收件箱制作了一个屏幕截图,您可以看到Google仅对PLINHT(使用纯文本,html和嵌入式图像创建的邮件)和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): 两个工作示例(ATHTIN和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 点击查看电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javamail api发送的html电子邮件正文在Outlook中显示为乱码 - html email body sent using javamail api appears garbled in outlook 使用javamail api从gmail服务器删除电子邮件 - deleting a email from gmail server with javamail api 无法在ExtentReport中查看使用JavaMail API发送到其他电子邮件ID的嵌入式图像? - Unable to view inline images in ExtentReport that was sent to a different email id using JavaMail API? 未在使用JavaMail API发送的电子邮件中设置发件人 - Sender is not set in Email sent using JavaMail api 使用JavaMail API的HTML电子邮件中的布局 - Layout in html email with JavaMail API 是否可以将内联 bitmap object 添加到通过 JavaMail 发送的 email 的正文中? - Is it possible to add inline bitmap object to the body of an email sent via JavaMail? 使用 Javamail API 确定将电子邮件置于哪个 Gmail 标签下 - Determining which Gmail Tab an email is placed under, using the Javamail API 使用JavaMail API通过Servlet通过附件发送的电子邮件未通过 - Email sent with attachment via servlet using JavaMail API not getting through 使用JavaMail API通过Servlet发送的电子邮件无法通过 - Email sent via servlet using JavaMail API not getting through JavaMail获取电子邮件的URL-Gmail - JavaMail get the URL of the Email - Gmail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM