Created
February 4, 2019 19:22
-
-
Save mhingston/2569d91d9c35ec58bdb7da8ac9d4ee90 to your computer and use it in GitHub Desktop.
Stub example working with EPPlus and MailKit
This file contains hidden or 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
foreach (DataRow task in tasks) | |
{ | |
DataRow manufacturer = GetManufacturer(Convert.ToInt32(task["ManufacturerID"])); | |
DataRowCollection recipients = GetRecipients(Convert.ToInt32(task["ManufacturerID"])); | |
if (recipients.Count > 0) | |
{ | |
DataSet results = GetResults(Convert.ToDateTime(task["StartDate"]), Convert.ToDateTime(task["EndDate"]), Convert.ToInt32(task["ManufacturerID"])); | |
ExcelPackage package = new ExcelPackage(); | |
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); | |
worksheet.Cells["A1"].LoadFromDataTable(results.Tables[0], true); | |
worksheet.Column(5).Style.Numberformat.Format = "#0%"; | |
worksheet.Column(7).Style.Numberformat.Format = "#0%"; | |
worksheet.Column(9).Style.Numberformat.Format = "#0%"; | |
worksheet.Cells[worksheet.Dimension.Address].AutoFilter = true; | |
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); | |
byte[] workbook = package.GetAsByteArray(); | |
MemoryStream stream = new MemoryStream(workbook); | |
DataRowCollection sitesExcludedRows = results.Tables[1].Rows; | |
List<string> sitesExcluded = new List<string>(); | |
foreach (DataRow site in sitesExcludedRows) | |
{ | |
sitesExcluded.Add(site["Name"].ToString()); | |
} | |
MimePart attachment = new MimePart("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet") | |
{ | |
Content = new MimeContent(stream, ContentEncoding.Default), | |
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), | |
ContentTransferEncoding = ContentEncoding.Base64, | |
FileName = "Summary.xlsx" | |
}; | |
MimeMessage message = new MimeMessage(); | |
message.From.Add(new MailboxAddress(appSettings["EmailFromName"], appSettings["EmailFromAddress"])); | |
foreach (DataRow recipient in recipients) | |
{ | |
message.Bcc.Add(new MailboxAddress(recipient["EmailAddress"].ToString())); | |
} | |
message.Subject = $"{manufacturer["Name"].ToString()} Weekly Summary"; | |
string html = template.Render(new | |
{ | |
Title = $"{manufacturer["Name"].ToString()} Weekly Summary", | |
ImageUrl = $"{manufacturer["LogoPath"].ToString()}", | |
StartDate = Convert.ToDateTime(task["StartDate"]), | |
EndDate = Convert.ToDateTime(task["EndDate"]), | |
SitesExcluded = sitesExcluded.Count > 0 ? sitesExcluded : null | |
}); | |
TextPart plainBody = new TextPart("plain") | |
{ | |
Text = ReadSharp.HtmlUtilities.ConvertToPlainText(html) | |
}; | |
TextPart htmlBody = new TextPart("html") | |
{ | |
Text = html | |
}; | |
Multipart alternative = new Multipart("alternative") | |
{ | |
plainBody, | |
htmlBody | |
}; | |
Multipart multipart = new Multipart("mixed") | |
{ | |
alternative, | |
attachment | |
}; | |
message.Body = multipart; | |
try | |
{ | |
using (SmtpClient client = new SmtpClient()) | |
{ | |
client.Connect(appSettings["SMTPHost"], int.Parse(appSettings["SMTPPort"])); | |
client.Authenticate(appSettings["SMTPUser"], appSettings["SMTPPass"]); | |
client.Send(message); | |
client.Disconnect(true); | |
} | |
} | |
catch (Exception error) | |
{ | |
log.Fatal("Unable to send email.", error); | |
Environment.Exit((int)ExitCode.SmtpError); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excellent example
Thank you