Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 14, 2021 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/3837cf82825bad3a302f30f13a6b9c32 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/3837cf82825bad3a302f30f13a6b9c32 to your computer and use it in GitHub Desktop.
Send Word Document as Email using C#
// Set position to 0
mhtmlStream.Position = 0;
// Create email message from MHTML
MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());
// Set email fields
message.Subject = "Sending Invoice in Email";
message.From = "sender@gmail.com";
message.To = "recipient@gmail.com";
// Load a Word document from disk
Document wordDocument = new Document("Word.docx");
// Save document as MHTML into memory stream
MemoryStream mhtmlStream = new MemoryStream();
wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
// Send email via SMTP
SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd");
client.SecurityOptions = SecurityOptions.SSLExplicit;
client.Send(message);
// Load a Word document from disk
Document wordDocument = new Document("Word.docx");
// Save document as MHTML into memory stream
MemoryStream mhtmlStream = new MemoryStream();
wordDocument.Save(mhtmlStream, SaveFormat.Mhtml);
// Set position to 0
mhtmlStream.Position = 0;
// Create email message from MHTML
MailMessage message = MailMessage.Load(mhtmlStream, new MhtmlLoadOptions());
// Set email fields
message.Subject = "Sending Invoice in Email";
message.From = "sender@gmail.com";
message.To = "recipient@gmail.com";
// Send email via SMTP
SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender@gmail.com", "pwd");
client.SecurityOptions = SecurityOptions.SSLExplicit;
client.Send(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment