How to email a word document 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 email a Word document in C# and VB.NET.
Steps to email a Word document 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, enter your login details and recipient mail IDs to email a Word document.
C#
//Creates new Word document instance for Word processing using (WordDocument document = new WordDocument()) { //Opens the Word template document document.Open("Template.docx", FormatType.Docx); //Save the HTML file as string MemoryStream docStream = new MemoryStream(); document.SaveOptions.HtmlExportOmitXmlDeclaration = true; document.Save(docStream, FormatType.Html); //Releases the resources occupied by WordDocument instance document.Dispose(); docStream.Position = 0; StreamReader reader = new StreamReader(docStream); string mailBody = reader.ReadToEnd(); docStream.Dispose(); if (mailBody.StartsWith("<!DOCTYPE")) mailBody = mailBody.Remove(0, 97); //Update the required e-mail id here //Creates and sends the email message MailMessage emailMessage = new MailMessage("MailId@outlook.com", "RecipientMailId@outlook.com"); //Adds the subject for email emailMessage.Subject = "You order #10295 has been shipped"; //Sets the HTML string as email body emailMessage.IsBodyHtml = true; emailMessage.Body = mailBody; //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("MailId@outlook.com", "password"); client.Port = 587; client.EnableSsl = true; client.Send(emailMessage); } }
VB
'Creates new Word document instance for Word processing Using document As WordDocument = New WordDocument 'Opens the Word template document document.Open("Template.docx", FormatType.Docx) 'Save the HTML file as string Dim docStream As MemoryStream = New MemoryStream document.SaveOptions.HtmlExportOmitXmlDeclaration = True document.Save(docStream, FormatType.Html) 'Releases the resources occupied by WordDocument instance document.Dispose() docStream.Position = 0 Dim reader As StreamReader = New StreamReader(docStream) Dim mailBody As String = reader.ReadToEnd docStream.Dispose() If mailBody.StartsWith("<!DOCTYPE") Then mailBody = mailBody.Remove(0, 97) End If 'Update the required e-mail id here 'Creates And sends the email message Dim emailMessage As MailMessage = New MailMessage("MailId@outlook.com", "RecipientMailId@outlook.com") 'Adds the subject for email emailMessage.Subject = "You order #10295 has been shipped" 'Sets the HTML string as email body emailMessage.IsBodyHtml = True emailMessage.Body = mailBody '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("MailId@outlook.com", "password") client.Port = 587 client.EnableSsl = True client.Send(emailMessage) End Using End Using
A complete working example of email a Word document using C# can be downloaded from email a Word document.zip
By executing the program, you can email a Word document 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.