tbo*_*one 104 html email oracle mime image
我正在尝试发送带有嵌入式gif图像的多部分/相关html电子邮件.此电子邮件是使用Oracle PL/SQL生成的.我的尝试失败了,图像显示为红色X(在Outlook 2007和雅虎邮件中)
我已经发送了一段时间的HTML电子邮件,但我现在要求在电子邮件中使用几个gif图像.我可以将它们存储在我们的一个Web服务器上并链接到它们,但许多用户的电子邮件客户端不会自动显示它们,并且需要更改设置或手动为每个电子邮件下载它们.
所以,我的想法是嵌入图像.我的问题是:
片段:
MIME-Version: 1.0
To: me@gmail.com
BCC: me@yahoo.com
From: email@yahoo.com
Subject: Test
Reply-To: email@yahoo.com
Content-Type: multipart/related; boundary="a1b2c3d4e3f2g1"
--a1b2c3d4e3f2g1
content-type: text/html;
<html>
<head><title>My title</title></head>
<body>
<div style="font-size:11pt;font-family:Calibri;">
<p><IMG SRC="cid:my_logo" alt="Logo"></p>
... more html here ...
</div></body></html>
--a1b2c3d4e3f2g1
Content-Type: image/gif;
Content-ID:<my_logo>
Content-Transfer-Encoding: base64
Content-Disposition: inline
[base64 image data here]
--a1b2c3d4e3f2g1--
Run Code Online (Sandbox Code Playgroud)
非常感谢.
顺便说一句:是的,我已经验证了base64数据是正确的,因为我可以将图像嵌入到html本身(使用相同的算法用于创建标题数据)并在Firefox/IE中查看图像.
我还应该注意,这不是针对垃圾邮件,而是将电子邮件发送给每天都在期待的特定客户.内容是数据驱动的,而不是广告.
小智 127
尝试直接插入,这样您就可以在电子邮件的不同位置插入多个图像.
<img src="data:image/jpg;base64,{{base64-data-string here}}" />
Run Code Online (Sandbox Code Playgroud)
并且要使这篇文章对其他人有用:如果你没有base64数据字符串,可以从图像文件中轻松地创建一个:http: //www.motobit.com/util/base64-decoder-encoder.asp.
电子邮件源代码看起来像这样,但我真的不能告诉你这个边界是什么:
To: email@email.de
Subject: ...
Content-Type: multipart/related;
boundary="------------090303020209010600070908"
This is a multi-part message in MIME format.
--------------090303020209010600070908
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="cid:part1.06090408.01060107" alt="">
</body>
</html>
--------------090303020209010600070908
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline;
filename="moz-screenshot.png"
[base64 image data here]
--------------090303020209010600070908--
Run Code Online (Sandbox Code Playgroud)
//编辑:哦,我只是意识到如果你从我的帖子中插入第一个代码片段来写一个带有thunderbird的电子邮件,thunderbird会自动更改html代码,看起来和我帖子中的第二个代码差不多.
小智 17
另一个解决方案是将图像作为附件附加,然后使用cid引用它的html代码.
HTML代码:
<html>
<head>
</head>
<body>
<img width=100 height=100 id="1" src="cid:Logo.jpg">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
C#代码:
EmailMessage email = new EmailMessage(service);
email.Subject = "Email with Image";
email.Body = new MessageBody(BodyType.HTML, html);
email.ToRecipients.Add("abc@xyz.com");
string file = @"C:\Users\acv\Pictures\Logo.jpg";
email.Attachments.AddFileAttachment("Logo.jpg", file);
email.Attachments[0].IsInline = true;
email.Attachments[0].ContentId = "Logo.jpg";
email.SendAndSaveCopy();
Run Code Online (Sandbox Code Playgroud)
小智 10
我没有发现任何有用的答案,所以我提供了我的解决方案.
问题是您使用multipart/related的内容类型在这种情况下并不好.我在multipart/mixed里面使用它multipart/alternative(它适用于大多数客户端).
消息结构应如下所示:
[Headers]
Content-type:multipart/mixed; boundary="boundary1"
--boundary1
Content-type:multipart/alternative; boundary="boundary2"
--boundary2
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
[HTML code with a href="cid:..."]
--boundary2
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename="moz-screenshot.png"
[base64 image data here]
--boundary2--
--boundary1--
Run Code Online (Sandbox Code Playgroud)然后它会工作
如果它不起作用,您可以尝试使用这些工具之一将图像转换为HTML表格(请注意图像的大小):
小智 7
我知道这是一篇旧帖子,但当前的答案并未解决 Outlook 和许多其他电子邮件提供商不支持内联图像或 CID 图像的事实。在电子邮件中放置图像的最有效方法是将其在线托管并在电子邮件中放置指向它的链接。对于小型电子邮件列表,公共保管箱效果很好。这也可以减小电子邮件的大小。
使用Base64将图像嵌入html非常棒。但是,请注意,base64字符串可能会使您的电子邮件很大。
因此,
1)如果您有很多图像,将图像上传到服务器并从服务器加载这些图像可以减小电子邮件的大小。(您可以通过Google获得很多免费服务)
2)如果您的邮件中只有少量图像,使用base64字符串绝对是一个很棒的选择。
除了现有答案提供的选择之外,您还可以在Linux上使用命令生成base64字符串:
base64 test.jpg
Run Code Online (Sandbox Code Playgroud)