Esempio n. 1
0
        public async Task SendEmailAsync(Email email, CancellationToken cancellationToken = default)
        {
            // To not send emails from dev environments, don't store an apikey password in the local secrets
            if (string.IsNullOrWhiteSpace(ApiKey) || ApiKey == "x")
            {
                return;
            }

            var from = new SendGrid.Helpers.Mail.EmailAddress(Constants.TrashMobEmailAddress, Constants.TrashMobEmailName);

            var tos = new List <SendGrid.Helpers.Mail.EmailAddress>();

            foreach (var address in email.Addresses)
            {
                tos.Add(new SendGrid.Helpers.Mail.EmailAddress(address.Email, address.Name));
            }

            try
            {
                var client   = new SendGridClient(ApiKey);
                var message  = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, email.Subject, email.Message, email.HtmlMessage);
                var response = await client.SendEmailAsync(message, cancellationToken).ConfigureAwait(false);

                Console.WriteLine(response);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Esempio n. 2
0
        private static SendGridMessage ConvertFrom(Email email)
        {
            var from = new SendGrid.Helpers.Mail.EmailAddress(email.From.Address, email.From.Name);
            var to   = new SendGrid.Helpers.Mail.EmailAddress(email.To.Address, email.To.Name);

            return(MailHelper.CreateSingleEmail(from, to, email.Subject, email.PlainTextContent, email.HtmlContent));
        }
Esempio n. 3
0
 public async void Send(IEmailAddress fromEmail, IEmailAddress toEmail, string subject, string body)
 {
     var apiKey   = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? throw new Exception("SENDGRID_API_KEY environment variable not found.");
     var client   = new SendGridClient(apiKey);
     var from     = new SendGrid.Helpers.Mail.EmailAddress(fromEmail.Email, fromEmail.Name);
     var to       = new SendGrid.Helpers.Mail.EmailAddress(toEmail.Email, toEmail.Name);
     var msg      = MailHelper.CreateSingleEmail(from, to, subject, body, null);
     var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
 }
        /// <summary>
        /// Istanzia un SendGridMessage partendo daòl'EmailMessage passato.
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        private SendGridMessage EmailMessageToSendGridMessage(EmailMessage email)
        {
            var from = new SendGrid.Helpers.Mail.EmailAddress(email.From.Email, email.From.Name);
            var tos  = email.Tos.Select(a => new SendGrid.Helpers.Mail.EmailAddress(a.Email, a.Name)).ToList();
            var ccs  = email.Ccs.Select(a => new SendGrid.Helpers.Mail.EmailAddress(a.Email, a.Name)).ToList();
            var bccs = email.Bccs.Select(a => new SendGrid.Helpers.Mail.EmailAddress(a.Email, a.Name)).ToList();

            var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, email.Subject, email.PlainTextContent, email.HtmlContent);

            if (email.ReplyTo != null)
            {
                msg.ReplyTo = new SendGrid.Helpers.Mail.EmailAddress()
                {
                    Email = email.ReplyTo.Email, Name = email.ReplyTo.Name
                };
            }

            if (ccs != null && ccs.Count > 0)
            {
                msg.AddCcs(ccs);
            }

            if (bccs != null && bccs.Count > 0)
            {
                msg.AddBccs(bccs);
            }

            if (email.Categories.Count > 0)
            {
                msg.AddCategories(email.Categories);
            }

            if (email.CustomArgs.Count > 0)
            {
                msg.AddCustomArgs(email.CustomArgs);
            }

            if (email.Headers.Count > 0)
            {
                msg.AddHeaders(email.Headers);
            }

            if (email.Attachments.Count > 0)
            {
                msg.AddAttachments(email.Attachments.Select(a => new SendGrid.Helpers.Mail.Attachment()
                {
                    Content     = a.Content,
                    Disposition = a.Disposition,
                    ContentId   = a.ContentId,
                    Filename    = a.Filename,
                    Type        = a.Type
                }).ToList());
            }

            return(msg);
        }
        private static SendGridMessage CreateSendGridMessage(MailRequest request)
        {
            var from = new EmailAddress(request.From.Email, request.From.Name);
            var to   = new EmailAddress(request.To.Email, request.To.Name);

            var message = MailHelper.CreateSingleTemplateEmail(from, to, request.Template.Id, request.Template.Data);

            message.AddHeader("notification-message-id", $"{request.MessageId}");

            return(message);
        }
        public IActionResult Email()
        {
            SendGrid.Helpers.Mail.EmailAddress From    = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Shiham M");
            SendGrid.Helpers.Mail.EmailAddress ReplyTo = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**");
            string Subject          = "Testing SendGrid Email Service";
            string HtmlContent      = "<p>This is a Test!</p>";
            string PlainTextContent = "This is a Test!";

            var msg = MailHelper.CreateSingleEmail(From, ReplyTo, Subject, PlainTextContent, HtmlContent);

            _emailService.Send(msg);

            return(View());
        }
Esempio n. 7
0
        public async Task <IActionResult> Post([FromBody] EmailMessage message)
        {
            string         apiKey  = config.Value.ApiKey;
            SendGridClient client  = new SendGridClient(apiKey);
            var            from    = new SendGrid.Helpers.Mail.EmailAddress(message.Sender.Email, message.Sender.Name);
            var            subject = message.Subject;
            var            tos     = message.Recipients.Select(sourceAddr =>
                                                               new SendGrid.Helpers.Mail.EmailAddress(sourceAddr.Email, sourceAddr.Name)).ToList();
            var             plainTextContent = message.Body;
            var             htmlContent      = message.Body;
            SendGridMessage msg      = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent);
            var             response = await client.SendEmailAsync(msg);

            return(Ok(response));
        }
Esempio n. 8
0
        public Task SendMailAsync(string subject, string body, string mailTo, string naamTo, string mailFrom, string naamFrom)

        {
            var apiKey           = new ApiKey.ApiKey();
            var client           = new SendGridClient(apiKey.Key);
            var msg              = new SendGridMessage();
            var From             = new SendGrid.Helpers.Mail.EmailAddress(mailFrom, naamFrom);
            var To               = new SendGrid.Helpers.Mail.EmailAddress(mailTo, naamTo);
            var plainTextContent = body;
            var htmlContent      = body;

            msg = MailHelper.CreateSingleEmail(From, To, subject, plainTextContent, htmlContent);

            return(client.SendEmailAsync(msg));
        }
Esempio n. 9
0
        public async Task SendAsync(IdentityMessage message)
        {
            var apiKey    = Environment.GetEnvironmentVariable("SendGridApiKey");
            var fromEmail = Environment.GetEnvironmentVariable("SendGridFromEmail");
            var fromName  = Environment.GetEnvironmentVariable("SendGridFromName");

            var client           = new SendGridClient(apiKey);
            var from             = new EmailAddress(fromEmail, fromName);
            var to               = new EmailAddress(message.Destination);
            var subject          = message.Subject;
            var plainTextContent = message.Body;
            var htmlContent      = message.Body;
            var msg              = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response         = await client.SendEmailAsync(msg);
        }
        public async Task SendMailAsync(FullDataADFMonitoringModel model, TimeSpan duration, string key, int aDFThresholdModel)
        {
            var celiedDuration = Math.Ceiling(duration.TotalMinutes);
            var client         = new SendGridClient(key);
            var from           = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Dummy User");
            var subject        = "ADF - long running ADF job detected";
            var toEmails       = new List <EmailAddress>()
            {
                new EmailAddress("*****@*****.**", "Dummy User")
            };

            // Customize body for Email
            var htmlContent = "Issue with Adf";

            var msg      = MailHelper.CreateSingleEmailToMultipleRecipients(from, toEmails, subject, null, htmlContent);
            var response = await client.SendEmailAsync(msg);
        }
Esempio n. 11
0
        public IActionResult SendMailViaSendGrid()
        {
            SendGridClient client = new SendGridClient(_configuration.GetValue <string>("SENDGRID_APIKEY"));

            SendGrid.Helpers.Mail.EmailAddress        from = new SendGrid.Helpers.Mail.EmailAddress(_configuration.GetValue <string>("MicrosoftGraph:UserName"));
            List <SendGrid.Helpers.Mail.EmailAddress> tos  = new List <SendGrid.Helpers.Mail.EmailAddress>
            {
                new SendGrid.Helpers.Mail.EmailAddress(_configuration.GetValue <string>("EmailRecipient"))
            };

            string          subject           = "Test message from SendGrid";
            string          textContent       = "Test message from SendGrid";
            bool            displayRecipients = false; // set this to true if you want recipients to see each others mail id
            SendGridMessage msg      = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, textContent, "", displayRecipients);
            Response        response = client.SendEmailAsync(msg).Result;

            return(View());
        }
Esempio n. 12
0
        /// <summary>
        /// Send email with a list of roles applied to new users in set groups
        /// </summary>
        /// <param name="graphServiceClient"></param>
        /// <param name="groups"></param>
        /// <returns></returns>
        public async static Task SendGridEmailNewGroups(List <string> groups)
        {
            List <KeyValuePair <string, string> >     accounts   = ReadCsv.GetEmailAccounts();
            List <SendGrid.Helpers.Mail.EmailAddress> recipients = new List <SendGrid.Helpers.Mail.EmailAddress>();

            foreach (var account in accounts)
            {
                string name    = account.Key;
                string address = account.Value;

                var emailAddress = new SendGrid.Helpers.Mail.EmailAddress()
                {
                    Name  = name,
                    Email = address
                };
                recipients.Add(emailAddress);
            }
            await SendGridNewGroupRolesEmail(groups, recipients);
        }
Esempio n. 13
0
        /// <summary>
        /// Send error email
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <param name="exMsg"></param>
        /// <returns></returns>
        public async static Task SendGridErrorEmail(string errorMessage, string exMsg)
        {
            List <KeyValuePair <string, string> >     accounts   = ReadCsv.GetEmailAccounts();
            List <SendGrid.Helpers.Mail.EmailAddress> recipients = new List <SendGrid.Helpers.Mail.EmailAddress>();

            foreach (var account in accounts)
            {
                string name    = account.Key;
                string address = account.Value;

                var emailAddress = new SendGrid.Helpers.Mail.EmailAddress()
                {
                    Name  = name,
                    Email = address
                };
                recipients.Add(emailAddress);
            }
            await SendGridErrorEmail(errorMessage, exMsg, recipients);
        }
Esempio n. 14
0
        static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("MyApikey");

            var client = new SendGridClient(apiKey);

            // Send a Single Email using the Mail Helper
            var from             = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Swaach Cleaning Services");
            var subject          = "A new booking request has been received";
            var to               = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Swaach Cleaning Services");
            var plainTextContent = "Plain text content";
            var htmlContent      = "html content";
            var msg              = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

            var response = await client.SendEmailAsync(msg);

            Console.WriteLine(msg.Serialize());
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Headers);
            Console.WriteLine("\n\nPress <Enter> to continue.");
            Console.ReadLine();
        }