12

I am sending an email from .net core application using MailKit, and it will sent it successfully.

But I want to use HTML template to send email with MailKit in .Net core.

Here are the code currently sending email with static body part

 var emailMessage = new MimeMessage();
        if (!string.IsNullOrWhiteSpace(cc))
        {
            emailMessage.Cc.Add(new MailboxAddress(cc));
        }
        else if (!string.IsNullOrWhiteSpace(EmailUserNameCC))
        {
            emailMessage.Cc.Add(new MailboxAddress(EmailUserNameCC));
        }
        if (!string.IsNullOrWhiteSpace(EmailUserNameBCC))
        {
            emailMessage.Bcc.Add(new MailboxAddress(EmailUserNameBCC));
        }

        emailMessage.From.Add(new MailboxAddress(mailFrom));
        emailMessage.To.Add(new MailboxAddress(mailTo));
        emailMessage.Subject = subject;
        if (!string.IsNullOrWhiteSpace(replyTo))
        {
            emailMessage.InReplyTo = replyTo;
        }
        var builder = new BodyBuilder();// { TextBody = message };
        builder.HtmlBody = message;

        if (attachments != null && attachments.Count > 0)
        {
            foreach (var item in attachments)
            {
                builder.Attachments.Add(item.Key, item.Value);
            }

            builder.HtmlBody = builder.HtmlBody + " \n" + " PFA";

        }
        var multipart = new Multipart("mixed");
        multipart.Add(new TextPart("html") { Text = message });

        emailMessage.Body = builder.ToMessageBody();
        using (var client = new SmtpClient())
        {
            var credentials = new NetworkCredential
            {
                UserName = EmailUserName,
                Password = EmailPassword
            };

            if (!client.IsConnected)
            {
                client.Connect(SmtpHost, Convert.ToInt32(EmailHostPort));
                client.Authenticate(EmailUserName, EmailPassword);
            }
            client.MessageSent += c_EmailReached;

            client.Send(emailMessage);
        }

Now, I want to use HTML template to replace body part. So how can I use HTML Template with MailKit in .Net Core ?

Additional:

-> Also the special characters are not showing in actual email after sending email with html template. For some special characters it is displaying � . So how can I resolved this, to show special characters also.

Thanks.

Herin
  • 704
  • 3
  • 18
  • 34
  • hi are you using Azure? I saw at this article that you can only use SendGrid on Azure. How did you overcome it? https://stackoverflow.com/questions/42895082/asp-net-core-mailkit – Franva May 03 '19 at 02:06

1 Answers1

12

You can use StreamReader to read the source file and assign it to your builder.HtmlBody.

using (StreamReader SourceReader = System.IO.File.OpenText(path to your file))
{
   builder.HtmlBody = SourceReader.ReadToEnd();
}
Rachit Tyagi
  • 658
  • 1
  • 9
  • 24
  • I want to read html template from my solution folder and also want to replace some text like <> to "MyFirstName" and <> to "MyLastName" from that html template. – Herin Feb 27 '17 at 06:53
  • 1
    The above code will help you out. For replacing the text you can use string.Format function – Rachit Tyagi Feb 27 '17 at 10:48
  • 2
    You could also just use `builder.HtmlBody = text.Replace("<>", "my text2 value");` – jstedfast Feb 28 '17 at 00:10
  • Thanks, it works, but there is other problem of displaying special characters, I have edited my question under the section of "Additional:" – Herin Feb 28 '17 at 06:36
  • @Herin, accept the answer if it works for you and which special characters you are using. – Rachit Tyagi Feb 28 '17 at 07:04
  • There are German characters like ö, ä, ü and so on is displaying as � in actual sent email.. – Herin Feb 28 '17 at 08:49
  • 2
    You might need to entity-encode those special characters in the HTML or, if they are in the text file template, you might need to make sure that your StreamReader is using the correct System.Text.Encoding. – jstedfast Feb 28 '17 at 16:37