简体   繁体   English

使用 ASP.NET MVC 中的 Email 模板发送 Email

[英]Sending Email by using Email Templates in ASP.NET MVC

How can I send an email in ASP.NET MVC using email Template?如何使用 email 模板在 ASP.NET MVC 中发送 email? I have tried to send an email but the receiver received the email with html tags.我尝试发送 email 但接收方收到了带有 html 标签的 email。 Can anyone please help me solve this problem?谁能帮我解决这个问题?

<!DOCTYPE html>
<html>
<head>
    <title></title>
        <meta charset="utf-8" />
    <style>
        table, th, td {
    border: 1px solid black;
}
    </style>
</head>
<body>
    <br />
    <table width="70%">
        <tr>
            <td align="center" style="background-color:yellow">
                <span style="font-size:25px;">&nbsp;&nbsp; Welcome to home &nbsp;&nbsp;</span>
                <br />
                <br />
            </td>
        </tr>
    </table>
</body>
</html>

Try this:尝试这个:

Controller: Controller:

string body = string.Empty;
var root = AppDomain.CurrentDomain.BaseDirectory; using (var reader = new System.IO.StreamReader(root + @"/Templates/ForgotPassword.txt"))
{
    string readFile = reader.ReadToEnd();
    string StrContent = string.Empty;
    StrContent = readFile;
    //Assing the field values in the template
    StrContent = StrContent.Replace("[FullName]", user.FullName);
    StrContent = StrContent.Replace("[Code]", callbackUrl);
    StrContent = StrContent.Replace("[Year]", DateTime.Now.Year.ToString());
    body = StrContent.ToString();
}                
await UserManager.SendEmailAsync(user.Id, "Password Reset", body);


public Task SendEmailAsync(IdentityMessage message)
{
    // Credentials:
    var credentialUserName = "xxx@gmail.com";
    var sentFrom = "xxx@gmail.com";
    var pwd = "******";

    // Configure the client:
    System.Net.Mail.SmtpClient client =
        new System.Net.Mail.SmtpClient("smtp.gmail.com");

    client.Port = 587;
    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;

    // Creatte the credentials:
    System.Net.NetworkCredential credentials =
        new System.Net.NetworkCredential(credentialUserName, pwd);

    client.EnableSsl = true;
    client.Credentials = credentials;

    // Create the message:
    var mail =
        new System.Net.Mail.MailMessage(sentFrom, message.Destination);

    mail.From = new MailAddress(sentFrom, "Demo System"); //Set display name of the user
    mail.Subject = message.Subject;
    mail.Body = message.Body;

    //Other parameters:
    mail.IsBodyHtml = true; //Do not forget to set this property to "true" if you use html in message body
    mail.Priority = MailPriority.Normal;
    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    mail.ReplyToList.Add("xxx@gmail.com");

    // Send:
    return client.SendMailAsync(mail);
}

MyEmailTemplate.txt:我的电子邮件模板.txt:

<!doctype html>     
<html lang="tr">
<head>
  <meta charset="utf-8">
</head>     
<body>
  <p>Dear [CustomerName]</p>
  <p>Thanks for your subscription</p>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM