Black Rock Reporting Azure Function
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailSenderFunction.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.IO;
  3. using Microsoft.Azure.Functions.Worker;
  4. using Microsoft.Extensions.Logging;
  5. //using SendGrid.Helpers.Mail;
  6. using Microsoft.Azure.WebJobs;
  7. using MailKit;
  8. using MimeKit;
  9. using MimeKit.Text;
  10. using MailKit.Net.Smtp;
  11. using MailKit.Security;
  12. using GemBox.Spreadsheet;
  13. namespace BlackRockReportFunction
  14. {
  15. public class MailSenderFunction
  16. {
  17. private readonly ILogger _logger;
  18. public MailSenderFunction(ILoggerFactory loggerFactory)
  19. {
  20. _logger = loggerFactory.CreateLogger<MailSenderFunction>();
  21. }
  22. [Function("MailSenderFunction")]
  23. public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
  24. {
  25. //var msg = new SendGridMessage()
  26. //{
  27. // From = new EmailAddress("nikola.jovanovic@dilig.net", "Nikola Jovanovic"),
  28. // Subject = "Test SendGrid Azure Function",
  29. // PlainTextContent = String.Format("If you read this text, then congratulations," +
  30. // " you did it! :)")
  31. //};
  32. //msg.AddTo(new EmailAddress("nikolajovanovic3579@gmail.com", "Nikola Jovanovic"));
  33. //return msg;
  34. string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
  35. var email = new MimeMessage();
  36. email.From.Add(MailboxAddress.Parse("nikola.jovanovic@dilig.net"));
  37. email.To.Add(MailboxAddress.Parse("greta.bartoletti@ethereal.email"));
  38. email.Subject = "BlackRock Report";
  39. var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };
  40. MemoryStream memoryStream = new MemoryStream(fileData);
  41. memoryStream.Position = 0;
  42. var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  43. {
  44. Content = new MimeContent(memoryStream),
  45. ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
  46. ContentTransferEncoding = ContentEncoding.Base64,
  47. FileName = fileName
  48. };
  49. var multipart = new Multipart("mixed");
  50. multipart.Add(body);
  51. multipart.Add(attachment);
  52. email.Body = multipart;
  53. SmtpClient smtp = new SmtpClient();
  54. smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
  55. smtp.Authenticate("greta.bartoletti@ethereal.email", "ecsdGZxWHk4yfjZpD5"); //"shaniya.blick76@ethereal.email", "8pPsjCbwCFMrEeKNef"
  56. smtp.Send(email);
  57. smtp.Disconnect(true);
  58. _logger.LogInformation("Email sent successfully!");
  59. }
  60. }
  61. }