使用内嵌图像生成电子邮件

时间:2013-03-14 10:29:26

标签: image email email-attachments

我正在尝试生成一个插入图像的电子邮件文本。我知道我应该使用语法<img src="cid:image-id" />并将图像添加为附件。我试着这样做,结果如下:

--===============6239034322813840804==
Content-Type: multipart/alternative;
 boundary="===============6051774682785554910=="
MIME-Version: 1.0

--===============6051774682785554910==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

my email in base64 with text only

--===============6051774682785554910==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

my email in base64 with html
...
<img src="cid:my-image.png" alt="My Image">
...

--===============6051774682785554910==--
--===============6239034322813840804==
MIME-Version: 1.0
Content-Type: image/png; name="badge-img.png"
Content-Disposition: inline; filename="badge-img.png"
Content-ID: <badge-img.png>
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI
...

发送时,图像处于附件中(可以打开,有效图像)。但在显示的电子邮件内容中,我只看到替代文字“我的图像”,而不是图像。

知道为什么吗?

2 个答案:

答案 0 :(得分:0)

您使用的语言是什么?在C#中你需要尝试类似的东西:

sending mail along with embedded image using asp.net

Attaching Image in the body of mail in C#

答案 1 :(得分:0)

好的我觉得我发现了问题。我必须使用multipart/related内容类型并更改结构。我目前有类似的东西

<email>
    <part1 content-type=multipart/alternative>
        <subpart1>plain text</subpart1>
        <subpart2>html text</subpart2>
    </part1>
    <part2>
        attachments
    </part2>
</email>

应该有像

这样的东西
<email>
    <part content-type=multipart/alternative>
        <subpart1>plain text</subpart1>
        <subpart2 content-type=multipart/related>
            <subsubpart1>html text</subsubpart1>
            <subsubpart2>attachments</subsubpart2>
        </subpart2>
    </part>
</email>

我仍然需要更改我的代码以适应,但似乎是问题。