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).

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
bashbin
  • 415
  • 1
  • 7
  • 21
  • 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*? –  Apr 07 '17 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](https://github.com/andrewdavey/postal). – mason Apr 07 '17 at 21:05
  • I took a HTML Email from a website (it's an html file with all the 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 – bashbin Apr 07 '17 at 21:06
  • You haven't shown that in your question. Where's the variable containing the HTML file? – mason Apr 07 '17 at 21:06
  • @mason The problem is that the HTML code is way too long, so is there a way like StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.html")) or something? – bashbin Apr 07 '17 at 21:07
  • 1
    What about just `var html = File.ReadAllText(Server.MapPath("~/EmailTemplate.html"))`? – mason Apr 07 '17 at 21:07
  • @mason You mean string html right? And where should I put it? sbEmailBody.Append("html"); ? – bashbin Apr 07 '17 at 21:09
  • You can just assign it directly to the body unless you have more content to add. No need for the builder. It's just a simple string. – mason Apr 07 '17 at 21:10
  • @mason Aha, I see. Thanks for the tip! I'll try it out – bashbin Apr 07 '17 at 21:12
  • @Amy Sorry - Missed your comment, but I guess you understand the problem from above comments. I tried the string html = File.ReadAllText(Server.MapPath("~/EN/EmailTemplate.html")); and then mailMessage.Body = html.ToString(); But it doesnt seem to work – bashbin Apr 07 '17 at 21:47
  • Yes. FYI, you can format small snippets of code in comments using backticks, `\`like so\`` –  Apr 07 '17 at 21:56
  • If `html` is already a string, then there's no need to then call `.ToString()` on it. And you'll need to describe what "doesn't work". Do you get an error? What is the result? Does `html` contain your HTML content when you view it in the debugger? – mason Apr 07 '17 at 22:42
  • @mason All fine now! I had a logical error from something else, but that method of yours works perfectly. One thing I noticed is that the images won't appear on the email because of SSL certificate which I don't have :p Anyway, thanks again! – bashbin Apr 07 '17 at 23:05
  • You could embed the images directly in the email, or host them on a website yourself and change the URLs within the email to point to them. – mason Apr 07 '17 at 23:06
  • Possible duplicate of [Easiest way to read from and write to files](http://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – Tedd Hansen Apr 09 '17 at 14:47

1 Answers1

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.

mason
  • 31,774
  • 10
  • 77
  • 121