Black Rock Reporting Azure Function
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MailSenderFunction.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.To.Add(MailboxAddress.Parse("greta.bartoletti@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. SmtpClient smtp = new SmtpClient();
  56. smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
  57. //smtp.UseDefaultCredentials = true;
  58. smtp.Authenticate("greta.bartoletti@ethereal.email", "ecsdGZxWHk4yfjZpD5"); //"shaniya.blick76@ethereal.email", "8pPsjCbwCFMrEeKNef"
  59. smtp.Send(email);
  60. smtp.Disconnect(true);
  61. _logger.LogInformation("Email sent successfully!");
  62. }
  63. }
  64. }