如何使用 razor 引擎进行带有图像 src 的电子邮件模板 [英] How to use razor engine for email templating with image src

查看:19
本文介绍了如何使用 razor 引擎进行带有图像 src 的电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个链接 关于如何在 asp.net 中将 Razor Engine 用于电子邮件模板,并且效果很好.但我尝试在电子邮件模板中添加带有图像的徽标.

I've found this link on how to using Razor Engine for email templates in asp.net and it worked great. But I've tried to have a logo in the email template with an image.

像这样:

EmailTemplate.cshtml(顺便说一下,这是一个强类型视图)

EmailTemplate.cshtml (this by the way is a strongly-type view)

<html>
<body>
  <img src="logo.jpg" />
</body>
</html>

当我尝试通过电子邮件提交它时,似乎没有读取图像路径,它只在内容中呈现了一个 X.

and when I try to submit it on email, it seems that the image path was not read, it only rendered an X in the content.

我正在考虑将图像路径作为模型的一部分传递,但这种方式似乎很奇怪.有什么办法可以做到这一点吗?

I'm thinking to pass the image path as part of the Model but it seems odd that way. Is there any way to achieve this?

任何帮助将不胜感激.谢谢

Any help would be much appreciated. Thanks

推荐答案

要在任何地方查看图像,您可以使用以下选项:

To see image everywhere you can use these options:

绝对网址

您可以简单地使用图像的完整绝对路径,例如 "http://example.com/images/logo.png"

You can simply use full absolute path of image for example "http://example.com/images/logo.png"

IMO 这是最简单的选项,推荐用于解决您的问题.

IMO It is the most simple option and recommended for your problem.

附件

正如梅森在评论中提到的,您可以将图像附加到邮件中,然后放置图像标签并使用ContentId 附件:

As mentioned by Mason in comments You can attach image to mail and then put image tag and useContentId of attachment:

//(Thanks to Mason for comment and Thanks to  Bartosz Kosarzyck for sample code)
string subject = "Subject";
string body = @"<img src=""$CONTENTID$""/> <br/> Some Content";

MailMessage mail = new MailMessage();
mail.From = new MailAddress("from@example.com");
mail.To.Add(new MailAddress("to@example.com"));
mail.Subject = subject;
mail.Body = body;
mail.Priority = MailPriority.Normal;

string contentID = Guid.NewGuid().ToString().Replace("-", "");
body = body.Replace("$CONTENTID$", "cid:" + contentID);

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//path of image or stream
LinkedResource imagelink = new LinkedResource(@"C:UsersR.AghaeiDesktopoutlook.png", "image/png");
imagelink.ContentId = contentID;
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);

SmtpClient client = new SmtpClient();
client.Host = "mail.example.com";
client.Credentials = new NetworkCredential("from@example.com", "password");
client.Send(mail);

数据 Uri

您可以使用数据 uri (data:image/png;base64,....).

you can use data uri (data:image/png;base64,....).

不推荐,因为大多数邮件客户端支持较弱,我使用 Outlook.com(web)OutlookWebAccess(web) 对其进行了测试> 和 Office Outlook(Windows)Outlook(windows 8.1) 不幸的是它只适用于 OutlookWebAccess(web).

Not Recommended because of weak support in most of mail clients, I tested it with Outlook.com(web) and OutlookWebAccess(web) and Office Outlook(Windows) and Outlook(windows 8.1) and unfortunately it worked only on OutlookWebAccess(web).

这篇关于如何使用 razor 引擎进行带有图像 src 的电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆