| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.IO;
- using Microsoft.Azure.Functions.Worker;
- using Microsoft.Extensions.Logging;
-
- using Microsoft.Azure.WebJobs;
- using MailKit;
- using MimeKit;
- using MimeKit.Text;
- using MailKit.Net.Smtp;
- using MailKit.Security;
- using GemBox.Spreadsheet;
- using RestSharp;
- using RestSharp.Authenticators;
- //using System.Net.Mail;
-
- namespace BlackRockReportFunction
- {
- public class MailSenderFunction
- {
- private readonly ILogger _logger;
-
- private static string SenderAddress = Environment.GetEnvironmentVariable("SenderAdress");
- private static string SenderDisplayName = Environment.GetEnvironmentVariable("SenderDisplayName");
- private static string MailGunApiKey = Environment.GetEnvironmentVariable("MailGunApiKey");
- private static string Domain = Environment.GetEnvironmentVariable("Domain");
- private static string MailTo = Environment.GetEnvironmentVariable("MailTo");
- public MailSenderFunction(ILoggerFactory loggerFactory)
- {
- _logger = loggerFactory.CreateLogger<MailSenderFunction>();
- }
-
- [Function("MailSenderFunction")]
- public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
- {
-
- string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
-
- var client = new RestClient("https://api.mailgun.net/v3");
- client.Authenticator = new HttpBasicAuthenticator("api", MailGunApiKey);
-
-
- MemoryStream memoryStream = new MemoryStream(fileData);
- memoryStream.Position = 0;
-
- RestRequest request = new RestRequest();
- request.AddParameter("domain", Domain, ParameterType.UrlSegment);
- request.Resource = "{domain}/messages";
- request.AddParameter("from", $"{SenderDisplayName} <{SenderAddress}>");
- request.AddParameter("to", MailTo);
- request.AddParameter("subject", "BlackRock Report");
- request.AddParameter("html", string.Format("Here is yours report for current week. {0}", fileName));
- request.AddFile("attachment", memoryStream.ToArray(), fileName);
- request.Method = Method.Post;
- client.Execute(request);
-
- //var email = new MimeMessage();
- //email.From.Add(MailboxAddress.Parse("[email protected]"));
- //email.To.Add(MailboxAddress.Parse("[email protected]"));
- //email.Sender = MailboxAddress.Parse("Clockify");
-
- //email.Subject = "BlackRock Report";
- //var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };
-
- //MemoryStream memoryStream = new MemoryStream(fileData);
- //memoryStream.Position = 0;
-
-
-
- //var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- //{
- // Content = new MimeContent(memoryStream),
- // ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
- // ContentTransferEncoding = ContentEncoding.Base64,
- // FileName = fileName
- //};
-
- //var multipart = new Multipart("mixed");
- //multipart.Add(body);
- //multipart.Add(attachment);
- //email.Body = multipart;
-
-
- //SmtpClient smtp = new SmtpClient();
- //smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
-
- //smtp.Authenticate("[email protected]", "Nacional99!"); //"[email protected]", "ecsdGZxWHk4yfjZpD5"
- //smtp.Send(email);
- //smtp.Disconnect(true);
-
-
-
-
- _logger.LogInformation("Email sent successfully!");
- }
- }
- }
|