0

I am new to trying the SMTPClient and would like to send a HTML formatted email to customers. The email message instead contains all the tags and not a formatted document. This is my code:

public void SendMailMessage(string FromAddress, string FromName, string ToName, string ToAddress, string Subject)
{
    MailAddress fromAdd = new MailAddress(FromAddress, FromName);
    MailAddress toAdd = new MailAddress(ToAddress);
    MailMessage msg = new MailMessage(fromAdd, toAdd);
    msg.IsBodyHtml = true;
    msg.BodyEncoding = System.Text.Encoding.UTF8;
    msg.Subject = Subject;
    string HtmlContent = "<div id='emailFormat' style='width: 100%; background-color: #efeac9;'>" +
                       "< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' ><h2>Hello World</h2></div></div>");
    msg.Body = HtmlContent;
    SmtpClient client = new SmtpClient();
    client.Send(msg);
}

}

The email message looks like this:

Susan Farrar Today at 5:45 PM Tosusan.farrar@sbcglobal.net

Message body

< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' >

Hello World

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
user2816714
  • 67
  • 3
  • 9

2 Answers2

1

The problem is that the content of string HtmlContent is not a well-formed HTML document. You are missing tags like <html>, <head>, <title>, <body>. Also, the additional space in < div is incorrect.

Xaver
  • 1,035
  • 9
  • 17
0

The whitespace is causing the issue. You should change this:

< div id = 'emailMsg' ...

To this:

<div id = 'emailMsg' ...
Henrik Nielsen
  • 410
  • 2
  • 6