示例#1
0
        public async Task SendEmailAsync(MailMessage message)
        {
            var sendGridMessage = new SendGridMessage
            {
                Subject          = message.Subject,
                From             = new EmailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName),
                HtmlContent      = message.HtmlContent,
                PlainTextContent = message.TextContent
            };

            sendGridMessage.SetClickTracking(true, false);
            sendGridMessage.SetOpenTracking(true, null);
            sendGridMessage.AddTos(message.ToEmails.Select(e => new EmailAddress(e)).ToList());
            if (message.BccEmails?.Any() ?? false)
            {
                sendGridMessage.AddBccs(message.BccEmails.Select(e => new EmailAddress(e)).ToList());
            }

            if (message.MetaData?.ContainsKey("SendGridTemplateId") ?? false)
            {
                sendGridMessage.HtmlContent      = " ";
                sendGridMessage.PlainTextContent = " ";
                sendGridMessage.TemplateId       = message.MetaData["SendGridTemplateId"].ToString();
            }

            if (message.MetaData?.ContainsKey("SendGridSubstitutions") ?? false)
            {
                var subs = message.MetaData["SendGridSubstitutions"] as Dictionary <string, string>;
                sendGridMessage.AddSubstitutions(subs);
            }

            var cats = new List <string> {
                "ByteGarden Server"
            };

            if (!string.IsNullOrWhiteSpace(message.Category))
            {
                cats.Add(message.Category);
            }
            sendGridMessage.AddCategories(cats);

            if (message.MetaData?.ContainsKey("SendGridBypassListManagement") ?? false)
            {
                var bypass = message.MetaData["SendGridBypassListManagement"] as bool?;
                sendGridMessage.SetBypassListManagement(bypass.GetValueOrDefault(false));
            }

            try
            {
                await SendAsync(sendGridMessage, false);
            }
            catch (HttpRequestException)
            {
                await SendAsync(sendGridMessage, true);
            }
            catch (WebException)
            {
                await SendAsync(sendGridMessage, true);
            }
        }
        public async Task SendEmailAsync(MailMessage message)
        {
            var sendGridMessage = new SendGridMessage
            {
                Subject          = message.Subject,
                From             = new EmailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName),
                HtmlContent      = message.HtmlContent,
                PlainTextContent = message.TextContent,
            };

            sendGridMessage.AddTos(message.ToEmails.Select(e => new EmailAddress(e)).ToList());

            if (message.MetaData?.ContainsKey("SendGridTemplateId") ?? false)
            {
                sendGridMessage.HtmlContent      = " ";
                sendGridMessage.PlainTextContent = " ";
                sendGridMessage.TemplateId       = message.MetaData["SendGridTemplateId"].ToString();
            }

            if (message.MetaData?.ContainsKey("SendGridSubstitutions") ?? false)
            {
                var subs = message.MetaData["SendGridSubstitutions"] as Dictionary <string, string>;
                sendGridMessage.AddSubstitutions(subs);
            }

            if (message.MetaData?.ContainsKey("SendGridCategories") ?? false)
            {
                var cats = message.MetaData["SendGridCategories"] as List <string>;
                sendGridMessage.AddCategories(cats);
            }

            if (message.MetaData?.ContainsKey("SendGridBypassListManagement") ?? false)
            {
                var bypass = message.MetaData["SendGridBypassListManagement"] as bool?;
                sendGridMessage.SetBypassListManagement(bypass.GetValueOrDefault(false));
            }

            await _client.SendEmailAsync(sendGridMessage);
        }