Black Rock Reporting Azure Function
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Function3.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.IO;
  3. using Microsoft.Azure.Functions.Worker;
  4. //using Microsoft.Azure.WebJobs;
  5. using Microsoft.Azure.WebJobs.Host;
  6. using Microsoft.Extensions.Logging;
  7. using RestSharp;
  8. using RestSharp.Authenticators;
  9. namespace ClockifyReport
  10. {
  11. public class Function3
  12. {
  13. private readonly ILogger _logger;
  14. private static string SenderAddress = Environment.GetEnvironmentVariable("SenderAddress");
  15. private static string SenderDisplayName = Environment.GetEnvironmentVariable("SenderDisplayName");
  16. private static string MailGunApiKey = Environment.GetEnvironmentVariable("MailGunApiKey");
  17. private static string Domain = Environment.GetEnvironmentVariable("Domain");
  18. private static string MailTo = Environment.GetEnvironmentVariable("MailTo");
  19. public Function3(ILoggerFactory loggerFactory)
  20. {
  21. _logger = loggerFactory.CreateLogger<Function3>();
  22. }
  23. [Function("Function3")]
  24. public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData, string name, ILogger log)
  25. {
  26. string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xls");
  27. var client = new RestClient("https://api.mailgun.net/v3");
  28. client.Authenticator = new HttpBasicAuthenticator("api", MailGunApiKey);
  29. MemoryStream memoryStream = new MemoryStream(fileData);
  30. memoryStream.Position = 0;
  31. RestRequest request = new RestRequest();
  32. request.AddParameter("domain", Domain, ParameterType.UrlSegment);
  33. request.Resource = "{domain}/messages";
  34. request.AddParameter("from", $"{SenderDisplayName} <{SenderAddress}>");
  35. request.AddParameter("to", MailTo);
  36. request.AddParameter("subject", "BlackRock Report");
  37. request.AddParameter("html", string.Format("Here is yours report for current week. {0}", fileName));
  38. request.AddFile("attachment", memoryStream.ToArray(), fileName);
  39. request.Method = Method.Post;
  40. client.Execute(request);
  41. _logger.LogInformation("Email sent successfully!");
  42. }
  43. }
  44. }