I do this:
Creating HTML:
I do not like writing HTML in C # code. It is bad to read and maintain in my opinion, especially when it has many attributes and CSS and has to be giving escape in the strings. I create a Resource.resx file (Add New Item, Resource File). So in it I create a string that is a template for HTML.
ThenI'lldothereplacement:
stringstrMensagem=Resource.htmlEmail;strMensagem=strMensagem.Replace("{nome}", txtNome.Text);
strMensagem = strMensagem.Replace("{contato}", txtTelefone.Text);
Send - there are several ways to send as per paid APIs or by SMTP-Client
var email = new MailMessage();
var strSenha = "aaaaa";
using (var smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("[email protected]", strSenha);
smtp.EnableSsl = true;
email.To.Add(strDestinatario);
email.IsBodyHtml = true;
email.Subject = strAssunto;
email.Body = strMensagem;
smtp.Send(email);
}
As I said this is the way I like to do it, but you could create the direct HTML in a string. With the new string interpolation of C # 6.0 this gets a bit easier.
string html = "<table>" +
" <thead>" +
" <tr>" +
" <th>Empresa</th>" +
" <th>Contato</th>" +
" </tr>" +
" </thead>" +
" <tbody>" +
" <tr>" +
$" <td>{txtNome.Text}</td>" +
$" <td>{txtTelefone.Text}</td>" +
" </tr>" +
" </tbody>" +
"</table>";