Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 11, 2022 17:00
Show Gist options
  • Save aspose-com-gists/e60b900feab31255be239912e7f54b4f to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e60b900feab31255be239912e7f54b4f to your computer and use it in GitHub Desktop.
Send Excel Data in Email's Body in C# .NET
// Load the desired Excel workbook
Workbook workbook = new Workbook("Data.xlsx");
// Save the workbook to MemoryStream in HTML format
MemoryStream ms = new MemoryStream();
workbook.Save(ms, SaveFormat.Html);
ms.Position = 0;
// Create a StreamReader object for the above MemoryStream
StreamReader sr = new StreamReader(ms);
// Load the saved HTML from StreamReader into a string variable
string strHtmlBody = sr.ReadToEnd();
// Define a new email message and set its HtmlBody
MailMessage message = new MailMessage();
message.HtmlBody = strHtmlBody;
message.Subject = "Inline Excel Message";
message.From = "sender@abc.com";
message.To = "receiver@xyz.com";
message.IsBodyHtml = true;
// Create SMTP client
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Username = "Username";
client.Password = "Password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.Auto;
// Send email
client.Send(message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment