gpt4 book ai didi

c# - 将 ASP.NET MVC View 返回为 HTML 文件,以便在电子邮件模板中使用

转载 作者:太空狗 更新时间:2023-10-29 22:27:53 28 4
gpt4 key购买 nike

我正在构建一个应用程序,它将向最终用户发送自定义电子邮件。

我已经构建了一个 HTML 模板,用于构建电子邮件。

我目前在模板中填充了标签作为自定义内容的占位符...

|Text-Placeholder|

我一直将 html 文件作为字符串返回到我的 CreateEMail 方法:

string html = System.IO.File.ReadAllText(Server.MapPath("~/EmailTemplates/emailTemplate.html"));

然后使用 String.Replace 替换自定义文本/内容

html = html.Replace("|Name-Placeholder|", username);

我很好奇是否有一种方法可以让我将模板构造为强类型为 ViewModel 的 RazorView,它将对自定义文本/内容进行建模,并将 View 作为 HTML 文件或直接作为字符串返回到传递到我的 SMTPClient 实例的 body 属性以发送给用户?

有没有人完成过类似的事情?

最佳答案

看看这些库:

有 ActionMailer。受 Ruby 的 ActionMailer 启发。

ActionMailer.Net aims to be an easy, and relatively painless way to send email from your ASP.NET MVC application. The concept is pretty simple. We render HTML by utilizing some pretty snazzy view engines, so why can't we do the same thing for email?

http://nuget.org/packages/ActionMailer

我认为支持许多 View 引擎,当然包括 Razor。并允许您将模型传递到 View 中。看: https://bitbucket.org/swaj/actionmailer.net/wiki/Home

代码:

public class MailController : MailerBase
{
public EmailResult VerificationEmail(User model)
{
To.Add(model.EmailAddress);
From = "no-reply@mycoolsite.com";
Subject = "Welcome to My Cool Site!";
return Email("VerificationEmail", model);
}
}

View :

@using ActionMailer.Net
@model User
@{
Layout = null;
}
Welcome to My Cool Site, @Model.FirstName. We need you to verify your email.
Click this nifty link to get verified!

还有一个库:

MvcMailer lets you use your MVC Views to produce stunning emails

http://nuget.org/packages/MvcMailer
https://github.com/smsohan/MvcMailer

关于c# - 将 ASP.NET MVC View 返回为 HTML 文件,以便在电子邮件模板中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com