如何使用剃刀引擎的电子邮件模板与图像src
本文关键字:图像 src 电子邮件 何使用 剃刀 引擎 | 更新日期: 2023-09-27 18:14:21
我找到了这个关于如何在asp.net中使用Razor引擎制作电子邮件模板的链接,它工作得很好。但是我已经尝试在电子邮件模板中有一个带有图像的徽标。
像这样:
EmailTemplate.cshtml
(顺便说一下,这是一个强类型视图)
<html>
<body>
<img src="logo.jpg" />
</body>
</html>
,当我尝试在电子邮件上提交它时,似乎没有读取图像路径,它只在内容中呈现X。
我想传递图像路径作为模型的一部分,但这种方式似乎很奇怪。有什么办法可以做到这一点吗?
任何帮助都将非常感激。由于
要想看到到处都是图像,可以使用以下选项:
绝对Url
您可以简单地使用图像的完全绝对路径,例如"http://example.com/images/logo.png"
在我看来,这是最简单的选择,建议您解决您的问题。
附件正如Mason在评论中提到的,您可以将图像附加到邮件中,然后放置图像标签并使用附件的ContentId
:
//(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);
<<p> 数据Uri/strong> 可以使用data uri (data:image/png;base64,....)
不推荐因为在大多数邮件客户端中支持较弱,我在Outlook.com(web)
和OutlookWebAccess(web)
以及Office Outlook(Windows)
和Outlook(windows 8.1)
上测试了它,不幸的是它只在OutlookWebAccess(web)
上工作。
自上次接受答案以来,GraphAPI已更改。当你读到这篇文章的时候,它可能又变了。祝成功。
你说你正在使用Razor,而公认的解决方案包括一些find'replace,这就是Razor所做的。我们从这里开始。
在你的EmailTemplate.cshtml
中,你可以使用Razor语法调用logo路径:
<img src="@Model.MainLogo" />
在构建模板时在数据模型中设置:
string contentIDlogo = "logo.png";
string templateName = "EmailTemplate.cshtml";
object dataModel = new
{
MainLogo = $"cid:{contentIDlogo}"
};
// This is where you render the Razor to HTML body string.
string emailTemplate = _razorTemplateCache.RenderBasicTemplate(templateName, dataModel);
// Define a simple e-mail message.
var mailMessage = new Message
{
Subject = subject,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = emailTemplate
},
ToRecipients = toRecipients.ToList()
};
// Add the image as an attachment to the email.
mailMessage.HasAttachments = true;
byte[] logoBytes = System.IO.File.ReadAllBytes(contentIDlogo);
mailMessage.Attachments = new List<Attachment>
{
new FileAttachment
{
ContentBytes = logoBytes,
ContentType = "image/png",
Name = contentIDlogo,
IsInline = true,
ContentId = contentIDlogo,
Size = logoBytes.Length
}
};
现在你有了一条消息,你可以使用Graph API发布它。我的例子是使用Microsoft.Graph (5.9.0)
// Define our new Microsoft Graph client.
GraphServiceClient graphServiceClient = new GraphServiceClient(_credentials, _scopes, Endpoint);
var requestBody = new SendMailPostRequestBody
{
Message = theEmail,
SaveToSentItems = true
};
// Send mail as the given user.
var fromUser = graphServiceClient.Users[_sendAsUserObjectId];
await fromUser.SendMail.PostAsync(mailMessage, null, cancelToken).ConfigureAwait(false);