Friday, 30 June 2017 02:31

Send Email with HTML Body in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

Following code snippets demonstrate how to send an email with HTML body using Spire.Email in C# and VB.NET.

Step 1: Create an instance of MailMessage class and specify sender and recipient in its constructor.

MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);

Step 2: Set the creation date, subject and html body of the message.

message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";   
message.BodyHtml = htmlString;

Step 3: Create a SmtpClient instance, set its properties, and send the email using SendOne() medthod.

SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);

Output:

Send Email with HTML Body in C#, VB.NET

Full Code:

[C#]
MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);

message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";   
message.BodyHtml = htmlString;

SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);

Console.WriteLine("Sent Successfully!");
Console.Read();
[VB.NET]
using Spire.Email;
using Spire.Email.IMap;
using Spire.Email.Smtp;
using System;

namespace SendEmailwithHTMLBody
{
    class Program
    {
        static void Main(string[] args)
        {
            MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
            MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
            MailMessage message = new MailMessage(addressFrom, addressTo);

            message.Date = DateTime.Now;
            message.Subject = "Sending Email with HTML Body";
            string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";
            message.BodyHtml = htmlString;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.outlook.com";
            client.Port = 587;
            client.Username = addressFrom.Address;
            client.Password = "password";
            client.ConnectionProtocols = ConnectionProtocols.Ssl;
            client.SendOne(message);

            Console.WriteLine("Sent Successfully!");
            Console.Read();
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Monday, 16 November 2020 03:00