我正在尝试发送带有嵌入式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中查看图像.
我还应该注意,这不是针对垃圾邮件,而是将电子邮件发送给每天都在期待的特定客户.内容是数据驱动的,而不是广告.
SmtpClient()允许您在邮件中添加附件,但是如果您想在邮件打开时显示图像而不是附加它,该怎么办?
我记得,它可以使用大约4行代码完成,但我不记得我在MSDN网站上找不到它.
编辑:我没有使用网站或任何东西,甚至没有IP地址.图像位于硬盘上.发送时,它们应该是邮件的一部分.所以,我想我可能想要使用标签......但我不太确定,因为我的电脑没有广播.
我找到了这个解决方案,用于在电子邮件正文中显示图像: 将图像添加到电子邮件正文中
它工作正常,但它也添加图像作为电子邮件的附件.
Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";
Run Code Online (Sandbox Code Playgroud)
有一行代码,评论显示为内联而非附件,但此行无法正常工作,因为图片仍会附加到电子邮件中:
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
Run Code Online (Sandbox Code Playgroud)
如何阻止图像附加到电子邮件?
如何在身体内容中附加图像.我写了下面的代码
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "xyz@someorg.com";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com"));
message.From = new System.Net.Mail.MailAddress("fromaddress@fromaddress.com");
message.Subject = "test subject";
message.Body = "<img src=@'C:\\Sunset.jpg'/>";
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = "hostname";
smtpClient.Port = 25;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.Send(message);
Run Code Online (Sandbox Code Playgroud)
代码很好,因为我也收到了消息,但图像在身体内部[X]而不是图像.怎么解决这个?路径是正确的?
目前,我有发送电子邮件与MailMessage
和SmtpClient
,但我需要发送图片当前在base64
string
该范围内MailAddress
的身体.
我已经认识到有必要把它的Attachment
,但我不知道如何把base64
在MailMessage
类,然后,以可视化的电子邮件的正文中的图像阅读.我没有网址图片路径.
我正在尝试从包含自定义样式和附件的C#SharePoint应用程序发送电子邮件.它使用模板结构,我已成功配置电子邮件以使用内联图像的新样式和附件,它可以很好地显示Outlook客户端.但是,当我尝试在iOS设备上查看电子邮件时,我会看到两次图像; 一旦内联,再次在电子邮件的末尾.
我能找到最接近解决方案的是为Django编写的,我没有太多成功将该解决方案移植到C#.我在这里找到答案:在iPhone,iPad上显示内嵌图像
我以这种方式配置附件:
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(new MemoryStream(imageBytes.Key), MediaTypeNames.Image.Jpeg);
attachment.Name = imageBytes.Value;
attachment.ContentDisposition.Inline = true;
attachment.ContentId = imageBytes.Value;
attachments.Add(attachment);
Run Code Online (Sandbox Code Playgroud)
如何才能仅显示一次这些图像?我需要能够以只显示内联的方式显示它们.我不确定这是否意味着我应该使用替代视图,如果是,那么如何使用它们.
编辑:
以下是我用于生成电子邮件的其余代码:
public override System.Net.Mail.MailMessage GenerateMessage()
{
var keys = new Dictionary<string, string>();
var fileBytes = new Dictionary<byte[], string>();
var attachments = new List<System.Net.Mail.Attachment>();
var message = new MailMessage();
//get the attachment images as html in a dictionary object, byte[] and string
fileBytes = GetEmailAttachments();
foreach (var imageBytes in fileBytes)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(new MemoryStream(imageBytes.Key), …
Run Code Online (Sandbox Code Playgroud) 我想使用 C# 将图片框中的图像添加到我的电子邮件正文中。这就是我所做的。任何人都可以帮助我吗?
var fromAddress = new MailAddress("aaa@gmail.com", "aaa");
var toAddress = new MailAddress("bbb@gmail.com", "bbb");
const string fromPassword = "mypassword";
const string subject = "Tes Program";
const string body = "Bersama ini kami kirimkan QR Code sebagai sarana validasi pengiriman rekening koran Anda. Harap simpan dan tunjukkan QR Code ini saat kurir kami datang untuk mengantar rekening koran. Atas perhatiannya kami sampaikan terima kasih. Salam";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true, …
Run Code Online (Sandbox Code Playgroud) c# ×6
email ×4
asp.net ×2
image ×2
asp.net-mvc ×1
html ×1
html-email ×1
inline ×1
ios ×1
mime ×1
oracle ×1
smtp ×1
smtpclient ×1