Send Html Email with Attachment in C# and VB.NET

The following example shows how you can send an email with a rich HTML body and with a file attachment by using C# or VB.NET code. You can have an alternative plain text body, together with a HTML body, in the same email message.

Also, instead of just adding your images as attachments, what you can do is place your images inside the HTML body itself. You can find below the different ways to achieve this, and how you can send an email with images in its body.

Screenshot of generated HTML email with attachment
Sending a HTML email with attachment in C# and VB.NET
using GemBox.Email;
using GemBox.Email.Smtp;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new email message.
        MailMessage message = new MailMessage("sender@example.com", "receiver@example.com");

        // Add subject.
        message.Subject = "Send HTML Email with Image and Attachment";

        // Add HTML body with CID embeded image.
        string cid = "image001@gembox.com";
        message.BodyHtml = "<html><body>" +
            "<p>Hi 👋,</p>" +
            "<p>This message was created and sent with:</p>" +
            "<p><img src='cid:" + cid + "'/></p>" +
            "<p>Read more about it on <a href='https://www.gemboxsoftware.com/email'>GemBox.Email Overview</a> page.</p>" +
            "</body></html>";

        // Add image as inline attachment.
        message.Attachments.Add(new Attachment("%#gembox-email-logo.png%") { ContentId = cid });

        // Add file as attachment.
        message.Attachments.Add(new Attachment("%#sample-file.pdf%"));

        // Create new SMTP client and send an email message.
        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
        {
            smtp.Connect();
            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
            smtp.SendMessage(message);
        }
    }
}
Imports GemBox.Email
Imports GemBox.Email.Smtp

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new email message.
        Dim message As New MailMessage("sender@example.com", "receiver@example.com")

        ' Add subject.
        message.Subject = "Send HTML Email with Image and Attachment"

        ' Add HTML body with CID embeded image.
        Dim cid As String = "image001@gembox.com"
        message.BodyHtml = "<html><body>" &
            "<p>Hi 👋,</p>" &
            "<p>This message was created and sent with:</p>" &
            "<p><img src='cid:" & cid & "'/></p>" &
            "<p>Read more about it on <a href='https://www.gemboxsoftware.com/email'>GemBox.Email Overview</a> page.</p>" &
            "</body></html>"

        ' Add image as inline attachment.
        message.Attachments.Add(New Attachment("%#gembox-email-logo.png%") With {.ContentId = cid})

        ' Add file as attachment.
        message.Attachments.Add(New Attachment("%#sample-file.pdf%"))

        ' Create new SMTP client and send an email message.
        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
            smtp.Connect()
            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
            smtp.SendMessage(message)
        End Using

    End Sub
End Module

The MailMessage class provides two properties for message text: plain text via MailMessage.BodyText property and rich formatted text via MailMessage.BodyHtml property.

If you specify both body types, the settings of the receiver's email client will affect which body is displayed. In general, most clients will by default have HTML messages enabled, but it's considered useful to specify both body types so that the receiver can read the message (in plain text) even if its email client doesn't accept HTML messages.

The HTML message that you specify in the MailMessage.BodyHtml property will not be validated by GemBox.Email, it will be stored as a string value. However, some email clients may experience difficulties with showing irregular HTML content, so you should consider checking your message's HTML markup.

To add extra content or files that you want to send along with your message, create a new Attachment object and add it to the MailMessage.Attachments collection. If the Attachment.ContentId property is specified on some Attachment object, then it'll be treated as an inline attachment; otherwise, it'll be a regular attachment.

To send a HTML message with images, you can use one of the following options:

  1. Create an image with a source that is linked to an external image file, e.g. <img src="https://www.example.com/image.png" />.
  2. Create an image with a source that contains data URI, an image data embedded directly into HTML, e.g. <img src="data:image/png;base64,AABBCCDD..." />.
  3. Create an image with a source that contains content ID (CID) and add the image as an inline attachment with a same CID, as shown in the above example.

Check next example or download examples from GitHub.