Exemplo n.º 1
0
        public async Task <SendResponse> SendAsync(IBabouEmail email, CancellationToken?token = null)
        {
            var response = new SendResponse();

            var message = CreateMailMessage(email);

            if (token?.IsCancellationRequested ?? false)
            {
                response.ErrorMessages.Add("Message was cancelled by cancellation token.");
                return(response);
            }

            if (_smtpClient == null)
            {
                using (var client = _clientFactory())
                {
                    await client.SendMailAsync(message);
                }
            }
            else
            {
                await _smtpClient.SendMailAsync(message);
            }

            return(response);
        }
Exemplo n.º 2
0
        public async Task <SendResponse> SendAsync(IBabouEmail email, CancellationToken?token = null)
        {
            var response = new SendResponse();

            await SaveEmailToDisk(email);

            return(response);
        }
Exemplo n.º 3
0
        private async Task <bool> SaveEmailToDisk(IBabouEmail email)
        {
            var random   = new Random();
            var filename = $"{_directory.TrimEnd('\\')}\\{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{random.Next(1000)}";

            using (var sw = new StreamWriter(File.OpenWrite(filename)))
            {
                sw.WriteLine($"From: {email.EmailData.FromAddress.Name} <{email.EmailData.FromAddress.EmailAddress}>");
                sw.WriteLine($"To: {string.Join(",", email.EmailData.ToAddresses.Select(x => $"{x.Name} <{x.EmailAddress}>"))}");
                sw.WriteLine($"Cc: {string.Join(",", email.EmailData.CcAddresses.Select(x => $"{x.Name} <{x.EmailAddress}>"))}");
                sw.WriteLine($"Bcc: {string.Join(",", email.EmailData.BccAddresses.Select(x => $"{x.Name} <{x.EmailAddress}>"))}");
                sw.WriteLine($"ReplyTo: {string.Join(",", email.EmailData.ReplyToAddresses.Select(x => $"{x.Name} <{x.EmailAddress}>"))}");
                sw.WriteLine($"Subject: {email.EmailData.Subject}");
                foreach (var dataHeader in email.EmailData.Headers)
                {
                    sw.WriteLine($"{dataHeader.Key}:{dataHeader.Value}");
                }
                sw.WriteLine();
                await sw.WriteAsync(email.EmailData.Body);
            }

            return(true);
        }
Exemplo n.º 4
0
 public SendResponse Send(IBabouEmail email, CancellationToken?token = null)
 {
     return(SendAsync(email, token).GetAwaiter().GetResult());
 }
Exemplo n.º 5
0
        private MailMessage CreateMailMessage(IBabouEmail email)
        {
            var         data    = email.EmailData;
            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 =>
            {
                var a = new System.Net.Mail.Attachment(x.Data, x.Filename, x.ContentType)
                {
                    ContentId = x.ContentId
                };


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

            return(message);
        }
Exemplo n.º 6
0
 public SendResponse Send(IBabouEmail email, CancellationToken?token = null)
 {
     return(Task.Run(() => SendAsync(email, token)).Result);
 }
Exemplo n.º 7
0
        public async Task <SendResponse> SendAsync(IBabouEmail email, CancellationToken?token = null)
        {
            var request = new RestRequest();

            request.AddParameter("domain", _domainName, ParameterType.UrlSegment);
            request.Resource = _domainName + "/messages";

            request.AddParameter("from", $"{email.EmailData.FromAddress.Name} <{email.EmailData.FromAddress.EmailAddress}>");

            email.EmailData.ToAddresses.ForEach(x => {
                request.AddParameter("to", $"{x.Name} <{x.EmailAddress}>");
            });

            email.EmailData.CcAddresses.ForEach(x => {
                request.AddParameter("cc", $"{x.Name} <{x.EmailAddress}>");
            });

            email.EmailData.BccAddresses.ForEach(x => {
                request.AddParameter("bcc", $"{x.Name} <{x.EmailAddress}>");
            });

            email.EmailData.ReplyToAddresses.ForEach(x => {
                request.AddParameter("h:Reply-To", $"{x.Name} <{x.EmailAddress}>");
            });

            request.AddParameter("subject", email.EmailData.Subject);

            request.AddParameter(email.EmailData.IsHtml ? "html" : "text", email.EmailData.Body);

            if (!string.IsNullOrEmpty(email.EmailData.PlaintextAlternativeBody))
            {
                request.AddParameter("text", email.EmailData.PlaintextAlternativeBody);
            }

            email.EmailData.Tags.ForEach(x =>
            {
                request.AddParameter("o:tag", x);
            });

            foreach (var emailHeader in email.EmailData.Headers)
            {
                var key = emailHeader.Key;
                if (!key.StartsWith("h:"))
                {
                    key = "h:" + emailHeader.Key;
                }

                request.AddParameter(key, emailHeader.Value);
            }

            email.EmailData.Attachments.ForEach(x =>
            {
                var param = x.IsInline ? "inline" : "attachment";
                request.AddFileBytes(param, x.Data.ReadAsBytes(), x.Filename, x.ContentType);
            });

            request.Method = Method.POST;
            var restResponse = await _restClient.ExecuteTaskAsync(request);

            var mailGunResponse = JsonConvert.DeserializeObject <MailGunResponse>(restResponse.Content);

            var result = new SendResponse {
                MessageId = mailGunResponse.Id
            };

            if (restResponse.IsSuccessful)
            {
                return(result);
            }

            result.ErrorMessages.Add(restResponse.ErrorMessage);
            return(result);
        }