SmtpClient()允许您向邮件中添加附件,但如果您想在邮件打开时使图像出现,而不是将其作为附件添加,该怎么办呢?
据我记得,只需要大约4行代码就可以完成,但我不记得具体细节了,也找不到MSDN网站上的相关信息。
编辑:我没有使用任何网站或IP地址。这些图片位于硬盘上。发送时,它们应该成为邮件的一部分。因此,我想我可能需要使用一个 tag...但我不太确定,因为我的电脑没有广播。
SmtpClient()允许您向邮件中添加附件,但如果您想在邮件打开时使图像出现,而不是将其作为附件添加,该怎么办呢?
据我记得,只需要大约4行代码就可以完成,但我不记得具体细节了,也找不到MSDN网站上的相关信息。
编辑:我没有使用任何网站或IP地址。这些图片位于硬盘上。发送时,它们应该成为邮件的一部分。因此,我想我可能需要使用一个 tag...但我不太确定,因为我的电脑没有广播。
经常提到的解决方案是将图像作为附件添加到邮件中,然后在HTML邮件正文中使用cid:
引用来引用它。
但是,如果您改用LinkedResources
集合,则内联图像仍将显示得很好,但不会作为邮件的其他附件显示。 这正是我们想要发生的事情,所以我在这里这样做:
using (var client = new SmtpClient())
{
MailMessage newMail = new MailMessage();
newMail.To.Add(new MailAddress("you@your.address"));
newMail.Subject = "Test Subject";
newMail.IsBodyHtml = true;
var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"), "image/png");
inlineLogo.ContentId = Guid.NewGuid().ToString();
string body = string.Format(@"
<p>Lorum Ipsum Blah Blah</p>
<img src=""cid:{0}"" />
<p>Lorum Ipsum Blah Blah</p>
", inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);
client.Send(newMail);
}
注意: 这个解决方案向你的 MailMessage
添加了一个类型为 text/html
的 AlternateView
。为了完整起见,你还应该添加一个类型为 text/plain
的 AlternateView
,其中包含邮件的纯文本版本,以供非 HTML 邮件客户端使用。
你所说的4行代码是指这个吗?
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;
HTML邮件和图片都是附件,所以只需通过它们的内容ID来引用图像,即:
Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text)
Dim RGen As Random = New Random()
A.ContentId = RGen.Next(100000, 9999999).ToString()
EM.Body = "<img src='cid:" + A.ContentId +"'>"
这里似乎有全面的示例:使用内联图像发送电子邮件
将图像转换为Base64字符串怎么样?据我所知,这可以轻松地嵌入电子邮件正文中。
请看这里。
已经发布的解决方案是我找到的最好的,我只是要补充一下,例如如果您有多个图像。
string startupPath = AppDomain.CurrentDomain.RelativeSearchPath;
string path = Path.Combine(startupPath, "HtmlTemplates", "NotifyTemplate.html");
string body = File.ReadAllText(path);
//General tags replacement.
body = body.Replace("[NOMBRE_COMPLETO]", request.ToName);
body = body.Replace("[ASUNTO_MENSAJE]", request.Subject);
//Image List Used to replace into the template.
string[] imagesList = { "h1.gif", "left.gif", "right.gif", "tw.gif", "fb.gif" };
//Here we create link resources one for each image.
//Also the MIME type is obtained from the image name and not hardcoded.
List<LinkedResource> imgResourceList = new List<LinkedResource>();
foreach (var img in imagesList)
{
string imagePath = Path.Combine(startupPath, "Images", img);
var image = new LinkedResource(imagePath, "image/" + img.Split('.')[1]);
image.ContentId = Guid.NewGuid().ToString();
image.ContentType.Name = img;
imgResourceList.Add(image);
body = body.Replace("{" + Array.IndexOf(imagesList, img) + "}", image.ContentId);
}
//Altern view for managing images and html text is created.
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//You need to add one by one each link resource to the created view
foreach (var imgResorce in imgResourceList)
{
view.LinkedResources.Add(imgResorce);
}
ThreadPool.QueueUserWorkItem(o =>
{
using (SmtpClient smtpClient = new SmtpClient(servidor, Puerto))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 50000;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = UMail,
Password = password
};
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress(UMail);
mailMessage.To.Add(request.ToEmail);
mailMessage.Subject = "[NAPNYL] " + request.Subject;
mailMessage.AlternateViews.Add(view);
smtpClient.Send(mailMessage);
}
}
});
正如你所看到的,你有一个图像名称的数组,重要的是这些图像都在同一个文件夹中,因为它们指向相同的输出文件夹。
最后,发送电子邮件是异步的,因此用户无需等待它被发送。