|
|
|
@@ -2,7 +2,7 @@ using System; |
|
|
|
using System.IO; |
|
|
|
using Microsoft.Azure.Functions.Worker; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
//using SendGrid.Helpers.Mail; |
|
|
|
|
|
|
|
using Microsoft.Azure.WebJobs; |
|
|
|
using MailKit; |
|
|
|
using MimeKit; |
|
|
|
@@ -10,6 +10,9 @@ using MimeKit.Text; |
|
|
|
using MailKit.Net.Smtp; |
|
|
|
using MailKit.Security; |
|
|
|
using GemBox.Spreadsheet; |
|
|
|
using RestSharp; |
|
|
|
using RestSharp.Authenticators; |
|
|
|
//using System.Net.Mail; |
|
|
|
|
|
|
|
namespace BlackRockReportFunction |
|
|
|
{ |
|
|
|
@@ -17,6 +20,11 @@ namespace BlackRockReportFunction |
|
|
|
{ |
|
|
|
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>(); |
|
|
|
@@ -25,48 +33,63 @@ namespace BlackRockReportFunction |
|
|
|
[Function("MailSenderFunction")] |
|
|
|
public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData) |
|
|
|
{ |
|
|
|
//var msg = new SendGridMessage() |
|
|
|
|
|
|
|
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") |
|
|
|
//{ |
|
|
|
// From = new EmailAddress("[email protected]", "Nikola Jovanovic"), |
|
|
|
// Subject = "Test SendGrid Azure Function", |
|
|
|
// PlainTextContent = String.Format("If you read this text, then congratulations," + |
|
|
|
// " you did it! :)") |
|
|
|
// Content = new MimeContent(memoryStream), |
|
|
|
// ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), |
|
|
|
// ContentTransferEncoding = ContentEncoding.Base64, |
|
|
|
// FileName = fileName |
|
|
|
//}; |
|
|
|
|
|
|
|
//msg.AddTo(new EmailAddress("[email protected]", "Nikola Jovanovic")); |
|
|
|
//var multipart = new Multipart("mixed"); |
|
|
|
//multipart.Add(body); |
|
|
|
//multipart.Add(attachment); |
|
|
|
//email.Body = multipart; |
|
|
|
|
|
|
|
//return msg; |
|
|
|
string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx"); |
|
|
|
|
|
|
|
var email = new MimeMessage(); |
|
|
|
email.From.Add(MailboxAddress.Parse("[email protected]")); |
|
|
|
email.To.Add(MailboxAddress.Parse("[email protected]")); |
|
|
|
|
|
|
|
email.Subject = "BlackRock Report"; |
|
|
|
var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) }; |
|
|
|
//SmtpClient smtp = new SmtpClient(); |
|
|
|
//smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls); |
|
|
|
|
|
|
|
MemoryStream memoryStream = new MemoryStream(fileData); |
|
|
|
memoryStream.Position = 0; |
|
|
|
//smtp.Authenticate("[email protected]", "Nacional99!"); //"[email protected]", "ecsdGZxWHk4yfjZpD5" |
|
|
|
//smtp.Send(email); |
|
|
|
//smtp.Disconnect(true); |
|
|
|
|
|
|
|
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.ethereal.email", 587, SecureSocketOptions.StartTls); |
|
|
|
smtp.Authenticate("[email protected]", "ecsdGZxWHk4yfjZpD5"); //"[email protected]", "8pPsjCbwCFMrEeKNef" |
|
|
|
smtp.Send(email); |
|
|
|
smtp.Disconnect(true); |
|
|
|
|
|
|
|
_logger.LogInformation("Email sent successfully!"); |
|
|
|
} |