예제 #1
0
        public async Task <SendResponse> SendAsync(IFluentEmail email, CancellationToken?token = null)
        {
            var parameters = new List <KeyValuePair <string, string> >();

            parameters.Add(new KeyValuePair <string, string>("from", $"{email.Data.FromAddress.Name} <{email.Data.FromAddress.EmailAddress}>"));
            email.Data.ToAddresses.ForEach(x => {
                parameters.Add(new KeyValuePair <string, string>("to", $"{x.Name} <{x.EmailAddress}>"));
            });
            email.Data.CcAddresses.ForEach(x => {
                parameters.Add(new KeyValuePair <string, string>("cc", $"{x.Name} <{x.EmailAddress}>"));
            });
            email.Data.BccAddresses.ForEach(x => {
                parameters.Add(new KeyValuePair <string, string>("bcc", $"{x.Name} <{x.EmailAddress}>"));
            });
            email.Data.ReplyToAddresses.ForEach(x => {
                parameters.Add(new KeyValuePair <string, string>("h:Reply-To", $"{x.Name} <{x.EmailAddress}>"));
            });
            parameters.Add(new KeyValuePair <string, string>("subject", email.Data.Subject));

            parameters.Add(new KeyValuePair <string, string>(email.Data.IsHtml ? "html" : "text", email.Data.Body));

            if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
            {
                parameters.Add(new KeyValuePair <string, string>("text", email.Data.PlaintextAlternativeBody));
            }

            email.Data.Tags.ForEach(x =>
            {
                parameters.Add(new KeyValuePair <string, string>("o:tag", x));
            });

            var files = new List <HttpFile>();

            email.Data.Attachments.ForEach(x =>
            {
                string param;

                if (x.IsInline)
                {
                    param = "inline";
                }
                else
                {
                    param = "attachment";
                }

                files.Add(new HttpFile()
                {
                    ParameterName = param,
                    Data          = x.Data,
                    Filename      = x.Filename,
                    ContentType   = x.ContentType
                });
            });

            var response = await _httpClient.PostMultipart <MailgunResponse>("messages", parameters, files).ConfigureAwait(false);

            var result = new SendResponse();

            if (!response.Success)
            {
                result.ErrorMessages.AddRange(response.Errors.Select(x => x.ErrorMessage));
                return(result);
            }

            return(result);
        }
예제 #2
0
 public void Initialize()
 {
     email = new MockEmail();
     email.To(toEmail).Subject(subject).Body(body);
 }
예제 #3
0
 public SendResponse Send(IFluentEmail email, CancellationToken?token = null)
 {
     return(SendAsync(email, token).GetAwaiter().GetResult());
 }
예제 #4
0
 public SendResponse Send(IFluentEmail email, CancellationToken?token = null)
 {
     return(SendAsync(email, token).Result);
 }
예제 #5
0
        private MailMessage CreateMailMessage(IFluentEmail email)
        {
            var         data    = email.Data;
            MailMessage message = null;

            // Smtp seems to require the HTML version as the alternative.
            if (!string.IsNullOrEmpty(data.PlaintextAlternativeBody))
            {
                message = new MailMessage
                {
                    Subject    = data.Subject,
                    Body       = data.PlaintextAlternativeBody,
                    IsBodyHtml = false,
                    From       = new MailAddress(data.FromAddress.EmailAddress, data.FromAddress.Name)
                };

                var           mimeType  = new System.Net.Mime.ContentType("text/html; charset=UTF-8");
                AlternateView alternate = AlternateView.CreateAlternateViewFromString(data.Body, mimeType);
                message.AlternateViews.Add(alternate);
            }
            else
            {
                message = new MailMessage
                {
                    Subject         = data.Subject,
                    Body            = data.Body,
                    IsBodyHtml      = data.IsHtml,
                    BodyEncoding    = Encoding.UTF8,
                    SubjectEncoding = Encoding.UTF8,
                    From            = new MailAddress(data.FromAddress.EmailAddress, data.FromAddress.Name)
                };
            }

            foreach (var header in data.Headers)
            {
                message.Headers.Add(header.Key, header.Value);
            }

            data.ToAddresses.ForEach(x =>
            {
                message.To.Add(new MailAddress(x.EmailAddress, x.Name));
            });

            data.CcAddresses.ForEach(x =>
            {
                message.CC.Add(new MailAddress(x.EmailAddress, x.Name));
            });

            data.BccAddresses.ForEach(x =>
            {
                message.Bcc.Add(new MailAddress(x.EmailAddress, x.Name));
            });

            data.ReplyToAddresses.ForEach(x =>
            {
                message.ReplyToList.Add(new MailAddress(x.EmailAddress, x.Name));
            });

            switch (data.Priority)
            {
            case Priority.Low:
                message.Priority = MailPriority.Low;
                break;

            case Priority.Normal:
                message.Priority = MailPriority.Normal;
                break;

            case Priority.High:
                message.Priority = MailPriority.High;
                break;
            }

            data.Attachments.ForEach(x =>
            {
                System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(x.Data, x.Filename, x.ContentType);

                a.ContentId = x.ContentId;

                message.Attachments.Add(a);
            });

            return(message);
        }
예제 #6
0
 public SendResponse Send(IFluentEmail email, CancellationToken?token = null)
 {
     // Uses task.run to negate Synchronisation Context
     // see: https://stackoverflow.com/questions/28333396/smtpclient-sendmailasync-causes-deadlock-when-throwing-a-specific-exception/28445791#28445791
     return(Task.Run(() => SendAsync(email, token)).Result);
 }
예제 #7
0
 public EmailService(IFluentEmail fluentEmail, IConfiguration configuration)
 {
     _configuration = configuration;
     _fluentEmail   = fluentEmail;
 }