| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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.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.ethereal.email", 587, SecureSocketOptions.StartTls);
- smtp.Authenticate("greta.bartoletti@ethereal.email", "ecsdGZxWHk4yfjZpD5"); //"shaniya.blick76@ethereal.email", "8pPsjCbwCFMrEeKNef"
- smtp.Send(email);
- smtp.Disconnect(true);
-
- _logger.LogInformation("Email sent successfully!");
- }
- }
- }
|