Send a Word file as an Email body in C# and VB.NET

With GemBox.Email you can write emails in EML, MHT, and MSG formats.
With GemBox.Document you can read documents from formats like DOC and DOCX.

By combining these components, you can create emails from Word files by using MHTML format as an intermediate. In other words, you can load a Word file into a DocumentModel object, save it as HtmlType.Mhtml, and then load it as a MailMessageFormat.Mhtml email into MailMessage.

The following example shows how you can send a Word document as a mail message from a C# or VB.NET application by converting the DOC or DOCX file into the body of an email and adding additional content to it (like subject, date, sender, and receiver). The generated email retains images, styles, and formatting from the Word template.

Sent email with a body that's generated from Word file in C# and VB.NET
Screenshot of a converted Word document to an email message
using System;
using System.IO;
using GemBox.Document;
using GemBox.Email;
using GemBox.Email.Smtp;

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

        // If using the Professional version, put your GemBox.Document serial key below.
        GemBox.Document.ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load a Word file.
        var document = DocumentModel.Load("%#BodyTemplate.docx%");

        using (var stream = new MemoryStream())
        {
            // Save as MHTML document.
            document.Save(stream, new HtmlSaveOptions()
            {
                HtmlType = HtmlType.Mhtml,
                UseContentIdHeaders = true
            });

            // Load MHTML content as a mail message.
            var message = MailMessage.Load(stream, MailMessageFormat.Mhtml);

            message.Subject = "Word message example";
            message.Date = DateTime.Now;

            message.From.Add(new MailAddress("sender@example.com"));
            message.To.Add(new MailAddress("receiver@example.com"));

            // Send the email.
            using (var smtp = new SmtpClient("<HOST>"))
            {
                smtp.Connect();
                smtp.Authenticate("<USERNAME>", "<PASSWORD>");
                smtp.SendMessage(message);
            }
        }
    }
}
Imports System
Imports System.IO
Imports GemBox.Document
Imports GemBox.Email
Imports GemBox.Email.Smtp

Module Program
    Sub Main()

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

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

        ' Load a Word file.
        Dim document = DocumentModel.Load("%#BodyTemplate.docx%")

        Using stream As New MemoryStream()
            ' Save as MHTML document.
            document.Save(stream, New HtmlSaveOptions() With
            {
                .HtmlType = HtmlType.Mhtml,
                .UseContentIdHeaders = True
            })

            ' Load MHTML content as a mail message.
            Dim message = MailMessage.Load(stream, MailMessageFormat.Mhtml)

            message.Subject = "Word message example"
            message.Date = DateTime.Now

            message.From.Add(New MailAddress("sender@example.com"))
            message.To.Add(New MailAddress("receiver@example.com"))

            ' Send the email.
            Using smtp As New SmtpClient("<HOST>")
                smtp.Connect()
                smtp.Authenticate("<USERNAME>", "<PASSWORD>")
                smtp.SendMessage(message)
            End Using
        End Using

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.