Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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