Black Rock Reporting Azure Function
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

MailSenderFunction.cs 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.IO;
  3. using Microsoft.Azure.Functions.Worker;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Azure.WebJobs;
  6. using MailKit;
  7. using MimeKit;
  8. using MimeKit.Text;
  9. using MailKit.Net.Smtp;
  10. using MailKit.Security;
  11. using GemBox.Spreadsheet;
  12. using RestSharp;
  13. using RestSharp.Authenticators;
  14. //using System.Net.Mail;
  15. namespace BlackRockReportFunction
  16. {
  17. public class MailSenderFunction
  18. {
  19. private readonly ILogger _logger;
  20. private static string SenderAddress = Environment.GetEnvironmentVariable("SenderAdress");
  21. private static string SenderDisplayName = Environment.GetEnvironmentVariable("SenderDisplayName");
  22. private static string MailGunApiKey = Environment.GetEnvironmentVariable("MailGunApiKey");
  23. private static string Domain = Environment.GetEnvironmentVariable("Domain");
  24. private static string MailTo = Environment.GetEnvironmentVariable("MailTo");
  25. public MailSenderFunction(ILoggerFactory loggerFactory)
  26. {
  27. _logger = loggerFactory.CreateLogger<MailSenderFunction>();
  28. }
  29. [Function("MailSenderFunction")]
  30. public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
  31. {
  32. string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
  33. var client = new RestClient("https://api.mailgun.net/v3");
  34. client.Authenticator = new HttpBasicAuthenticator("api", MailGunApiKey);
  35. MemoryStream memoryStream = new MemoryStream(fileData);
  36. memoryStream.Position = 0;
  37. RestRequest request = new RestRequest();
  38. request.AddParameter("domain", Domain, ParameterType.UrlSegment);
  39. request.Resource = "{domain}/messages";
  40. request.AddParameter("from", $"{SenderDisplayName} <{SenderAddress}>");
  41. request.AddParameter("to", MailTo);
  42. request.AddParameter("subject", "BlackRock Report");
  43. request.AddParameter("html", string.Format("Here is yours report for current week. {0}", fileName));
  44. request.AddFile("attachment", memoryStream.ToArray(), fileName);
  45. request.Method = Method.Post;
  46. client.Execute(request);
  47. //var email = new MimeMessage();
  48. //email.From.Add(MailboxAddress.Parse("[email protected]"));
  49. //email.To.Add(MailboxAddress.Parse("[email protected]"));
  50. //email.Sender = MailboxAddress.Parse("Clockify");
  51. //email.Subject = "BlackRock Report";
  52. //var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };
  53. //MemoryStream memoryStream = new MemoryStream(fileData);
  54. //memoryStream.Position = 0;
  55. //var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  56. //{
  57. // Content = new MimeContent(memoryStream),
  58. // ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
  59. // ContentTransferEncoding = ContentEncoding.Base64,
  60. // FileName = fileName
  61. //};
  62. //var multipart = new Multipart("mixed");
  63. //multipart.Add(body);
  64. //multipart.Add(attachment);
  65. //email.Body = multipart;
  66. //SmtpClient smtp = new SmtpClient();
  67. //smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
  68. //smtp.Authenticate("[email protected]", "Nacional99!"); //"[email protected]", "ecsdGZxWHk4yfjZpD5"
  69. //smtp.Send(email);
  70. //smtp.Disconnect(true);
  71. _logger.LogInformation("Email sent successfully!");
  72. }
  73. }
  74. }