C# 使用 SmtpClient 发送带有内联图像的邮件
SmtpClient() 允许您向邮件添加附件,但是如果您想在邮件打开时显示图像而不是附加附件,该怎么办?
我记得,大约需要 4 行代码就可以完成,但我不记得如何完成,并且在 MSDN 网站上找不到它。
编辑:我没有使用网站或任何东西,甚至没有IP地址。 图像位于硬盘上。 发送时,它们应该是邮件的一部分。 所以,我想我可能想使用标签......但我不太确定,因为我的计算机没有广播。
SmtpClient() allows you to add attachments to your mails, but what if you wanna make an image appear when the mail opens, instead of attaching it?
As I remember, it can be done with about 4 lines of code, but I don't remember how and I can't find it on the MSDN site.
EDIT: I'm not using a website or anything, not even an IP address. The image(s) are located on a harddrive. When sent, they should be part of the mail. So, I guess I might wanna use an tag... but I'm not too sure, since my computer isn't broadcasting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
经常提到的一种解决方案是将图像作为
附件
添加到邮件中,然后使用cid:
引用在 HTML 邮件正文中引用它。但是,如果您改用
LinkedResources
集合,内联图像仍然会显示得很好,但不会显示为邮件的附加附件。 这就是我们想要发生的,所以这就是我在这里所做的:注意:此解决方案将
AlternateView
添加到您的MailMessage 类型为
text/html
。 为了完整起见,您还应该添加text/plain
类型的AlternateView
,其中包含用于非 HTML 邮件客户端的电子邮件的纯文本版本。One solution that is often mentioned is to add the image as an
Attachment
to the mail, and then reference it in the HTML mailbody using acid:
reference.However if you use the
LinkedResources
collection instead, the inline images will still appear just fine, but don't show as additional attachments to the mail. That's what we want to happen, so that's what I do here:NOTE: This solution adds an
AlternateView
to yourMailMessage
of typetext/html
. For completeness, you should also add anAlternateView
of typetext/plain
, containing a plain text version of the email for non-HTML mail clients.HTML 电子邮件和图像是附件,因此这只是通过内容 ID 引用图像的情况,即
这里似乎有全面的示例: 发送带有内嵌图像的电子邮件
The HTML Email and the images are attachments so it's just a case of referring to the image(s) by their content ids, i.e.
There seems to be comprehensive examples here: Send Email with inline images
当你说4行代码时,你指的是这个吗?
When you say 4 lines of code, are you referring to this?
将图像转换为 Base64 字符串怎么样? 据我所知,这可以很容易地嵌入邮件正文中。
请查看此处 。
What about converting images in Base64 strings? AFAIK this can be easily embedded within mail body.
Take a look here.
已经发布的解决方案是我发现的最好的解决方案,例如,如果您有多个图像,我将完成它。
正如您所看到的,您有一个图像名称数组,图像位于同一文件夹中非常重要,因为它指向同一输出文件夹。
最后,电子邮件以异步方式发送,因此用户不必等待电子邮件发送。
The solution already posted is the best I have found, I'm just going to complete it with for example if you have multiple images.
As you can see you have an array of image names, it is important that the images are in the same folder because it is pointing to the same output folder.
Finally the email is sent as async so the user doesn't have to wait for it to be sent.
当打开邮件时使图像出现在客户端上的过程是客户端功能。 只要客户知道如何渲染图像& 没有阻止任何图像内容,它会立即打开它。 只要您正确指定了图像 MIME 附件类型,您在发送电子邮件时无需执行任何特殊操作即可在客户端上打开该电子邮件。
The process of making an image appear on the client when the mail is opened is a client function. As long as the client knows how to render the image & has not blocked any image content it will open it right away. You do not have to do anything special while sending the email to make it open on the client as long as you have correctly specified the image mime attachment type.