Convert DOCX to MHTML and Send Email Using Aspose.Words for .NET

Introduction

In today’s digital landscape, converting documents between formats and emailing them is a common task. This guide will walk you through converting a DOCX file to MHTML format and sending it via email using the powerful Aspose.Words and Aspose.Email libraries for .NET. We’ll break down each step clearly, ensuring you can follow along with ease. Let’s get started!

Prerequisites

Before diving into the process, ensure you have the following set up:

  1. Aspose.Words for .NET: Download and install the library from the Aspose releases page.
  2. Aspose.Email for .NET: Download and install this library from the Aspose releases page.
  3. .NET Framework: Ensure that you have the .NET Framework installed on your machine.
  4. SMTP Server: You will need access to an SMTP server to send emails.

Importing Necessary Namespaces

To utilize Aspose.Words and Aspose.Email in your project, you must import the required namespaces. Add the following using directives at the top of your C# file:

using Aspose.Words;
using Aspose.Words.Saving;
using Aspose.Email;
using Aspose.Email.Mime;
using Aspose.Email.Clients.Smtp;

Step 1: Load the DOCX Document

Begin by loading the DOCX document you wish to convert. Use the Document class from Aspose.Words to accomplish this.

// Specify the path to your document directory.
string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document(dataDir + "Document.docx");

Step 2: Save the Document as MHTML

Next, convert the loaded document into MHTML format. This is done using the Save method of the Document class.

using (Stream stream = new MemoryStream())
{
    doc.Save(stream, SaveFormat.Mhtml);
    // Reset the stream position to the beginning for reading.
    stream.Position = 0;
}

Step 3: Create an Email Message

Now, create an email message from the MHTML stream using Aspose.Email. You will utilize the MailMessage class for this purpose.

// Load the MHTML stream into an Aspose.Email MIME email message.
MailMessage message = MailMessage.Load(stream, new MhtmlLoadOptions());
message.From = "your_from@email.com";
message.To = "your_to@email.com";
message.Subject = "Aspose.Words + Aspose.Email MHTML Test Message";

Step 4: Send the Email

Finally, send the email using an SMTP client. Configure the SMTP client with your server details and use the Send method to dispatch the message.

// Configure and send the message using Aspose.Email.
using (SmtpClient client = new SmtpClient())
{
    client.Host = "your_smtp.com";
    client.Send(message);
}

Conclusion

Congratulations! You have successfully converted a DOCX document to MHTML and sent it via email using Aspose.Words and Aspose.Email for .NET. This process involves loading the document, converting it to MHTML, creating an email message, and sending it through an SMTP client. With these steps, you can automate the conversion and emailing of documents seamlessly in your applications.

FAQ’s

Can I use this method to convert other document formats?

Absolutely! Aspose.Words supports a wide range of formats, allowing you to convert DOC, DOCX, RTF, and more to MHTML.

How can I add attachments to the email?

You can easily add attachments using the Attachments property of the MailMessage class.

Is Aspose.Words compatible with .NET Core?

Yes, Aspose.Words is compatible with .NET Core, making it suitable for use in .NET Core applications.

Do I need a license for Aspose.Words and Aspose.Email?

Yes, both libraries require a license. You can obtain a temporary license for evaluation purposes from the Aspose purchase page.

Where can I find more documentation?

For detailed documentation, check out Aspose.Words here and Aspose.Email here.