2016-09-22 87 views
0

我有点卡在这一点上 - 我想添加一个自定义的html电子邮件模板到我的asp.net身份注册,即当用户获得注册链接,那么它应该有某种格式/模板。如何将自定义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"); 
} 

在上面的代码中,链接发送给用户。我想以一种格式发送它,带有页眉,页脚,并且主体应该有链接。所以我的理解是我需要放置一个模板。如何做到这一点?

谢谢。

回答

0

我认为在您的确认/邀请方法,你可以使用这样的发送个性化的电子邮件确认/邀请:

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 = [email protected]" 
    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 : {"[email protected]"}<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 protected]" /> 
     <add key="EmailTo" value="[email protected]" /> 
     <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> 

UPDATE : 问题已经更新,所以这里有一点点信息: 根据MSDN UserManager.SendEmailAsync Method

方法的语法是

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的字符串插值你可以阅读更多关于它在这里How C# 6.0 Simplifies, Clarifies and Condenses Your Code

记住的是,电子邮件的正文是html,所以你可以使它变得漂亮和格式化,只要你想。

我希望它有帮助。

+0

非常感谢回复。我编辑了我的问题,你能帮我解决吗? – newbie

+0

@newbie我添加了更多信息,我希望它有帮助。 –

相关问题