Manan Kapoor
Manan Kapoor

Reputation: 327

Use Mailkit to send out Email using Html template in .Net Core Application

I'm able to send out normal emails(with plain text body). I now want to send out emails using html templates. My template looks somewhat like this.

<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 {[email protected]}
      </p>
    </div>
  </div>

My code for creating the email looks like this :

 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")
 {                   

 };

How can I send the email using this template now ?

Upvotes: 0

Views: 2713

Answers (1)

Tommix
Tommix

Reputation: 522

Store your email templates somewhere, files, db... Read as string, replace variables with real data by using Replace or by using string.format and then when done -use modified string as your html email.

Upvotes: 3

Related Questions