using System; using System.IO; using Microsoft.Azure.Functions.Worker; using Microsoft.Extensions.Logging; //using SendGrid.Helpers.Mail; using Microsoft.Azure.WebJobs; using MailKit; using MimeKit; using MimeKit.Text; using MailKit.Net.Smtp; using MailKit.Security; using GemBox.Spreadsheet; namespace BlackRockReportFunction { public class MailSenderFunction { private readonly ILogger _logger; public MailSenderFunction(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger(); } //http://127.0.0.1:10000/devstoreaccount1/report-container [Function("MailSenderFunction")] public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData) { //var msg = new SendGridMessage() //{ // From = new EmailAddress("nikola.jovanovic@dilig.net", "Nikola Jovanovic"), // Subject = "Test SendGrid Azure Function", // PlainTextContent = String.Format("If you read this text, then congratulations," + // " you did it! :)") //}; //msg.AddTo(new EmailAddress("nikolajovanovic3579@gmail.com", "Nikola Jovanovic")); //return msg; string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx"); var email = new MimeMessage(); email.From.Add(MailboxAddress.Parse("nikola.jovanovic@dilig.net")); email.To.Add(MailboxAddress.Parse("justine.tromp31@ethereal.email")); email.Subject = "BlackRock Report"; var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) }; MemoryStream memoryStream = new MemoryStream(fileData); memoryStream.Position = 0; var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { Content = new MimeContent(memoryStream), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = fileName }; var multipart = new Multipart("mixed"); multipart.Add(body); multipart.Add(attachment); email.Body = multipart; using var smtp = new SmtpClient(); smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls); smtp.Authenticate("justine.tromp31@ethereal.email", "ztbDPEp9gEfPetwZFY"); smtp.Send(email); smtp.Disconnect(true); _logger.LogInformation("Email sent successfully!"); } } }