How to send html email from C#

The System.Net classes offer functionalities similar to the Microsoft WinInet API. In this case, we are utilizing the System.Net class to send an HTML email through the following C# program. When sending an HTML email, the body of the email contains HTML codes, similar to HTML pages. In contrast to the previous C# program, which demonstrated how to send an email with a text body, we are now sending an email with an HTML body.

System.Net class

Using the capabilities provided by the System.Net classes, we can construct an email message with HTML content and send it using SMTP (Simple Mail Transfer Protocol). This allows us to create visually appealing and formatted email messages that can include images, hyperlinks, and other HTML elements.

mail.IsBodyHtml = true; string htmlBody = "create html page" ; mail.Body = htmlBody;

By referring to the SMTP email from C# program, you can understand the basic concepts of sending emails and modifying the content to include HTML formatting. This enables you to craft professional-looking emails with customized styles and layouts.

The following C# source code shows how to send an email with HTML body part from a Gmail address . The Gmail SMTP server name is and the port using for send mail is 587 . Here using NetworkCredential for password based authentication.

SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
Full Source C#
using System; using System.Windows.Forms; using System.Net.Mail; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(""); mail.From = new MailAddress(""); mail.To.Add("to_address"); mail.Subject = "Test Mail - 1"; mail.IsBodyHtml = true; string htmlBody; htmlBody = "Write some HTML code here"; mail.Body = htmlBody; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); MessageBox.Show("mail Send"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }

You have to provide the necessary information like your gmail username and password etc.