如何将自定义html模板添加到asp.net标识
本文关键字:添加 asp net 标识 自定义 html | 更新日期: 2023-09-27 18:08:33
我有点卡在这一点上-我想添加一个自定义html电子邮件模板到我的asp.net身份注册,即当用户得到一个链接注册,然后它应该有某种格式/模板。
if (result.Succeeded) {
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new {
userId = user.Id, code = code
}, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href='"" + callbackUrl + "'">link</a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail");
}
在上面的代码中,向用户发送了一个链接。我想发送它的格式,与头部,脚注和主体应该有链接。所以我的理解是,我需要放入一个模板。怎么才能做到呢?
谢谢。
我假设在您的确认/邀请方法中,您可以使用以下内容来发送个性化的电子邮件确认/邀请:
public ActionResult Your_Method()
{
//... your auth login ....
//Load email setting from config
var EmailFrom = ConfigurationManager.AppSettings["EmailFrom"];
var EmailTo = ConfigurationManager.AppSettings["EmailTo"];
var EmailSubject = ConfigurationManager.AppSettings["EmailSubject"];
var currentUser = //your current user data for example from session or from the code/database;
try
{
if (currentUser != null)
{
using (var smtp = new SmtpClient())
{
using (var message = new MailMessage())
{
var from = new MailAddress(EmailFrom);
var to = new MailAddress(EmailTo);
message.To.Add(to);
message.From = from;
message.Subject = EmailSubject;
message.IsBodyHtml = true;
message.Body = $@"
Dear user: {currentUser.FName ?? string.Empty} {currentUser.LName ?? string.Empty} <br/>
Welcome to our website! Your id: {currentUser.Id ?? string.Empty} <br/>
Other information: {currentUser.information ?? string.Empty} <br/>
<br/>
<br/>
Contact Us Email : {"website@website.ru"}<br/>
Contact Us Phone : {"+7 913 123 4567"} <br/>
";
smtp.Send(message);
}
}
}
}
}
catch (Exception e)
{
//Log your exception somethere
//e.HandleException(LogLevel.Fatal);
}
//And then you can return some JSON
var response = JsonConvert.SerializeObject(your_return_object);
return new ContentResult {Content = response, ContentEncoding = Encoding.UTF8, ContentType = "application/json"};
//Or show a View or redirect
return View();
//Or show nothing
return return new EmptyResult();
//It depends on your logic
}
还需要包含
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<!-- Email configuration -->
<add key="EmailFrom" value="email@email.net" />
<add key="EmailTo" value="yourcustomer@email.net" />
<add key="EmailSubject" value="subject of your email" />
</appSettings>
<system.net>
<!-- SMTP configuration -->
<mailSettings>
<smtp>
<network host="MAIL_SERVER" port="25" userName="USER_NAME" password="USER_PASSWORD" />
</smtp>
</mailSettings>
</system.net>
...
</configuration>
更新:问题已经更新了,所以这里有一些更多的信息:根据MSDN用户管理器。SendEmailAsync方法
方法的语法是
public virtual Task SendEmailAsync(
TKey userId,
string subject,
string body
)
所以对你来说更简单。你可以这样自定义电子邮件的"正文":
var header = "Dear user: <br/>";
var footer = "Thank you <br/> website.com";
var originalBody = "Please confirm your account by clicking this link: <a href='"" + callbackUrl + "'">link</a>"
var newEmailBody = header+"<br/>"+originalBody+"<br/>"+footer;
await UserManager.SendEmailAsync(user.Id, "Confirm your account", newEmailBody);
我鼓励你使用c# 6.0进行字符串插值,你可以在这里阅读更多关于c# 6.0如何简化,澄清和压缩你的代码
请记住,电子邮件的主体是html,所以你可以把它做得很漂亮,并按照你想要的格式。
我希望它有帮助。