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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //http://127.0.0.1:10000/devstoreaccount1/report-container
  23. [Function("MailSenderFunction")]
  24. public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
  25. {
  26. //var msg = new SendGridMessage()
  27. //{
  28. // From = new EmailAddress("nikola.jovanovic@dilig.net", "Nikola Jovanovic"),
  29. // Subject = "Test SendGrid Azure Function",
  30. // PlainTextContent = String.Format("If you read this text, then congratulations," +
  31. // " you did it! :)")
  32. //};
  33. //msg.AddTo(new EmailAddress("nikolajovanovic3579@gmail.com", "Nikola Jovanovic"));
  34. //return msg;
  35. string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
  36. var email = new MimeMessage();
  37. email.From.Add(MailboxAddress.Parse("nikola.jovanovic@dilig.net"));
  38. email.To.Add(MailboxAddress.Parse("justine.tromp31@ethereal.email"));
  39. email.Subject = "BlackRock Report";
  40. var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };
  41. MemoryStream memoryStream = new MemoryStream(fileData);
  42. memoryStream.Position = 0;
  43. var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  44. {
  45. Content = new MimeContent(memoryStream),
  46. ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
  47. ContentTransferEncoding = ContentEncoding.Base64,
  48. FileName = fileName
  49. };
  50. var multipart = new Multipart("mixed");
  51. multipart.Add(body);
  52. multipart.Add(attachment);
  53. email.Body = multipart;
  54. using var smtp = new SmtpClient();
  55. smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
  56. smtp.Authenticate("justine.tromp31@ethereal.email", "ztbDPEp9gEfPetwZFY");
  57. smtp.Send(email);
  58. smtp.Disconnect(true);
  59. _logger.LogInformation("Email sent successfully!");
  60. }
  61. }
  62. }