| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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;
- using MimeKit.Text;
- using MailKit.Net.Smtp;
- using MailKit.Security;
- using GemBox.Spreadsheet;
-
- namespace BlackRockReportFunction
- {
- public class MailSenderFunction
- {
- private readonly ILogger _logger;
-
- public MailSenderFunction(ILoggerFactory loggerFactory)
- {
- _logger = loggerFactory.CreateLogger<MailSenderFunction>();
- }
-
- [Function("MailSenderFunction")]
- public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
- {
- //var msg = new SendGridMessage()
- //{
- // From = new EmailAddress("nikola.jovanovic@dilig.net", "Nikola Jovanovic"),
- // Subject = "Test SendGrid Azure Function",
- // PlainTextContent = String.Format("If you read this text, then congratulations," +
- // " you did it! :)")
- //};
-
- //msg.AddTo(new EmailAddress("nikolajovanovic3579@gmail.com", "Nikola Jovanovic"));
-
- //return msg;
- string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
-
- var email = new MimeMessage();
- email.From.Add(MailboxAddress.Parse("nikola.jovanovic@dilig.net"));
- email.To.Add(MailboxAddress.Parse("greta.bartoletti@ethereal.email"));
- //email.To.Add(MailboxAddress.Parse("greta.bartoletti@ethereal.email"));
- 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;
-
- //using var smtp = new SmtpClient();
- SmtpClient smtp = new SmtpClient();
- smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
- //smtp.UseDefaultCredentials = true;
- smtp.Authenticate("greta.bartoletti@ethereal.email", "ecsdGZxWHk4yfjZpD5"); //"shaniya.blick76@ethereal.email", "8pPsjCbwCFMrEeKNef"
- smtp.Send(email);
- smtp.Disconnect(true);
-
- _logger.LogInformation("Email sent successfully!");
- }
- }
- }
|