How to send a word document in an email using C#, VB.NET
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can send a Word document in an email in C# and VB.NET.
Steps to send a Word document in an email using C#:
- Create a new C# console application project.
- Install Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO.DLS; using Syncfusion.DocIO; using System.Net.Mail; using System.IO;
VB
Imports Syncfusion.DocIO.DLS Imports Syncfusion.DocIO Imports System.Net.Mail Imports System.IO
- Use the following code to create the mail contents with the Word document as an attachment.
C#
//Creates new Word document instance for Word processing using (WordDocument document = new WordDocument()) { //Opens the Word template document as an attachment document.Open("Template.docx", FormatType.Docx); MemoryStream docStream = new MemoryStream(); document.Save(docStream, FormatType.Docx); //Releases the resources occupied by WordDocument instance document.Dispose(); docStream.Position = 0; //Opens the Word template document as an email body document.Open("BodyTemplate.docx", FormatType.Docx); //Save the HTML file as string MemoryStream bodyStream = new MemoryStream(); document.SaveOptions.HtmlExportOmitXmlDeclaration = true; document.Save(bodyStream, FormatType.Html); //Releases the resources occupied by WordDocument instance document.Dispose(); bodyStream.Position = 0; StreamReader reader = new StreamReader(bodyStream); string mailBody = reader.ReadToEnd(); bodyStream.Dispose(); if (mailBody.StartsWith("<!DOCTYPE")) mailBody = mailBody.Remove(0, 97); // Sends the email message // Update the required e-mail id here SendEMail("MailId@outlook.com", "RecipientMailId@outlook.com", "Essential DocIO generated Word document", mailBody, docStream); }
VB
'Creates new Word document instance for Word processing Using document As WordDocument = New WordDocument 'Opens the Word template document as an attachment document.Open("Template.docx", FormatType.Docx) Dim docStream As MemoryStream = New MemoryStream document.Save(docStream, FormatType.Docx) 'Releases the resources occupied by WordDocument instance document.Dispose() docStream.Position = 0 'Opens the Word template document as an email body document.Open("BodyTemplate.docx", FormatType.Docx) Dim bodyStream As MemoryStream = New MemoryStream document.SaveOptions.HtmlExportOmitXmlDeclaration = True document.Save(bodyStream, FormatType.Html) 'Releases the resources occupied by WordDocument instance document.Dispose() bodyStream.Position = 0 Dim reader As StreamReader = New StreamReader(bodyStream) Dim mailBody As String = reader.ReadToEnd bodyStream.Dispose() If mailBody.StartsWith("<!DOCTYPE") Then mailBody = mailBody.Remove(0, 97) End If 'Sends the email message 'Update the required e-mail id here SendEMail("MailId@outlook.com", "RecipientMailId@outlook.com", "Essential DocIO generated Word document", mailBody, docStream) End Using
- Use the following code, enter your login details and recipient mail IDs to send a Word document in an email.
C#
private static void SendEMail(string from, string recipients, string subject, string body, MemoryStream docStream) { //Creates the email message MailMessage emailMessage = new MailMessage(from, recipients); //Adds the subject for email emailMessage.Subject = subject; //Sets the HTML string as email body emailMessage.IsBodyHtml = true; emailMessage.Body = body; //Add the file attachment to this e-mail message. emailMessage.Attachments.Add(new Attachment(docStream, "Sample.docx")); //Sends the email with prepared message using (SmtpClient client = new SmtpClient()) { //Update your SMTP Server address here client.Host = "smtp.outlook.com"; client.UseDefaultCredentials = false; //Update your email credentials here client.Credentials = new System.Net.NetworkCredential(from, "password"); client.Port = 587; client.EnableSsl = true; client.Send(emailMessage); } }
VB
Private Sub SendEMail(ByVal from As String, ByVal recipients As String, ByVal subject As String, ByVal body As String, ByVal docStream As MemoryStream) 'Creates the email message Dim emailMessage As MailMessage = New MailMessage(from, recipients) 'Adds the subject for email emailMessage.Subject = subject 'Sets the HTML string as email body emailMessage.IsBodyHtml = True emailMessage.Body = body 'Add the file attachment to this e-mail message. emailMessage.Attachments.Add(New Attachment(docStream, "Sample.docx")) 'Sends the email with prepared message Using client As SmtpClient = New SmtpClient 'Update your SMTP Server address here client.Host = "smtp.outlook.com" client.UseDefaultCredentials = False 'Update your email credentials here client.Credentials = New System.Net.NetworkCredential(from, "password") client.Port = 587 client.EnableSsl = True client.Send(emailMessage) End Using End Sub
A complete working example to send a Word document in an email using C# can be downloaded from send a Word document in an email.zip
By executing the program, you can send a Word document in an email as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.