如何使用剃刀引擎用于电子邮件模板与图片src [英] How to use razor engine for email templating with image src

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

问题描述

我发现这个链接有关如何使用剃刀引擎在asp.net邮件模板和它的工作太棒了。但是,我一直试图在与图像的电子邮件模板的标识。

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?

任何帮助将是非常美联社preciated。谢谢

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.

附件

作为注释您可以将图像邮寄,然后把图像标记,并使用<提到梅森href=\"https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.attachment.contentid(v=exchg.80).aspx\"相对=nofollow> 内容识别 附件:

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:\Users\R.Aghaei\Desktop\outlook.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。(数据:图像/ PNG; BASE64,....)

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

不建议的,因为在大多数的邮件客户端的疲软支持,我与 Outlook.com(网络)和<$ C $测试它C> OutlookWebAccess(网络)和 Office Outlook中(视窗)展望(Windows 8.1中)不幸的是它 OutlookWebAccess才做(网络)

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).

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

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