0

I have the following method for sending a simple email:

private void WelcomeMail(string recipient)
{
    MailMessage mailMessage = new MailMessage("MyEmail", recipient);
    StringBuilder sbEmailBody = new StringBuilder();

    sbEmailBody.Append("How can I attach .html file here instead of writing the whole code");

    mailMessage.IsBodyHtml = true;
    mailMessage.Body = sbEmailBody.ToString();
    mailMessage.Subject = "Welcome to domain.com";
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
    smtpClient.Credentials = new System.Net.NetworkCredential()
    {
        UserName = "MyEmail",
        Password = "MyPassword"
    };
    smtpClient.EnableSsl = true;
    smtpClient.Send(mailMessage);
}  

What should I remove/change/add to send an HTML formatted email?

The HTML file is called responsivemail.html and contains more than 100+ lines of html code (that's the problem).

15
  • What HTML are you wanting to append? You're doing everything correctly, you just lack the HTML. Do you want the HTML as an attachment, or in the body? Commented Apr 7, 2017 at 21:04
  • Looks like you've already got it? You just need to actually put HTML tags in the strings you add to sbEmailBody. So I'm not really sure what you're asking for? Anways, if you're going to be doing HTML email, you should probably look into Postal. Commented Apr 7, 2017 at 21:05
  • I took a HTML Email from a website (it's an html file with all the <html> <head> <body> etc tags). I asked on how can I attach that .html file instead of copy paste-ing the whole code (which is quite a lot) in the append area Commented Apr 7, 2017 at 21:06
  • You haven't shown that in your question. Where's the variable containing the HTML file? Commented Apr 7, 2017 at 21:06
  • 1
    What about just var html = File.ReadAllText(Server.MapPath("~/EmailTemplate.html"))? Commented Apr 7, 2017 at 21:07

1 Answer 1

1

If you have the HTML file in your site root in a file called emailtemplate.html, you can simply read the HTML into memory and assign it to the body of the email.

mailMessage.Body = File.ReadAllText(Server.MapPath("~/emailtemplate.html"));

I don't recommend this, but you could also embed the HTML into your code directly. Since the HTML spans multiple lines, we'll need to use a string literal (@"this is a string literal"). The HTML likely has double quotes in it. You'd need to escape them by doubling your double quotes.

mailMessage.Body = @"
  <h1>This is HTML in an email!</h1>
  <a href=""http://google.com\"">This is a test link.</a>
";

Long term, if you're going to be sending emails with HTML and you need to inject values into them, I suggest you look into Postal or other libraries. Postal can even make it easy to embed images.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.