【问题标题】:Use Mailkit to send out Email using Html template in .Net Core Application使用 Mailkit 在 .Net Core Application 中使用 Html 模板发送电子邮件
【发布时间】:2020-09-22 04:26:39
【问题描述】:

我能够发送普通电子邮件(带有纯文本正文)。我现在想使用 html 模板发送电子邮件。我的模板看起来有点像这样。

<div
    style="
      @import url('https://fonts.googleapis.com/css2?family=Varela+Round&display=swap');
      font-family: 'Varela Round', sans-serif;
      max-width: 600px;
      margin: 0 auto;
    "
  >
    <div
      style="
        width: 100%;
        height: 200px;
        border: solid 1px #cd5383;
        border-radius: 5px;
        padding: 5px;
        text-align: justify;
        font-size: 20px;
        line-height: 30px;
      "
    >
      Hello {Ashish}, <br />
      We have received your request to change password in our Portal<br />
      <div style="text-align: center; margin-top: 10px;">
        <a target="_blank" href="https://www.google.com">
          <button
            style="
              font-family: 'Varela Round', sans-serif;
              width: 150px;
              height: 37px;
              margin-left: 10px;
              background-color: #cd5383;
              border: none;
              color: #fff;
              box-shadow: 1px 1px 5px 1px gray;
            "
          >
            Reset Password
          </button>
        </a>
      </div>
      <p style="font-size: 15px; text-align: center; color: gray;">
        Facing any issue, contact {support@portal.com}
      </p>
    </div>
  </div>

我创建电子邮件的代码如下所示:

 var message = new MimeMessage();
 message.From.Add(new MailboxAddress("Portal",_configuration.GetSection("Email").GetSection("EmailAddress").Value));
 message.To.Add(new MailboxAddress(userDetails.CompanyEmail));
 message.Subject = "Password Reset Link";
 message.Body = new TextPart("html")
 {                   

 };

我现在如何使用此模板发送电子邮件?

【问题讨论】:

    标签: html asp.net-core mailkit mime-message


    【解决方案1】:

    将您的电子邮件模板存储在某处、文件、数据库...读取为字符串,使用 Replace 或使用 string.format 将变量替换为真实数据 然后完成后 - 使用修改后的字符串作为您的 html 电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2016-11-03
      • 2020-03-02
      • 2013-12-29
      • 2019-10-14
      • 2023-03-18
      相关资源
      最近更新 更多