Read the complete article on how to send Excel data in Email's body in C# .NET: https://blog.aspose.com/2022/05/11/send-excel-data-in-email-body-using-csharp-net/
Last active
May 11, 2022 17:00
-
-
Save aspose-com-gists/e60b900feab31255be239912e7f54b4f to your computer and use it in GitHub Desktop.
Send Excel Data in Email's Body in C# .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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