예제 #1
0
 public void QueueEmail(List <EmailAddress> recipients, EmailAddress sender, string subject, string textBody, string htmlBody, EmailPriority priority = EmailPriority.Normal, List <string> attachments = null)
 {
     QueueEmail(recipients, sender, null, subject, textBody, htmlBody, priority, attachments);
 }
예제 #2
0
        public void QueueEmail(EmailAddress recipient, EmailAddress sender, string replyTo, string subject, string textBody, string htmlBody, EmailPriority priority = EmailPriority.Normal, List <string> attachments = null)
        {
            ValidationException ex = new ValidationException();

            if (recipient == null)
            {
                ex.AddError("recipientEmail", "Recipient is not specified.");
            }
            else
            {
                var address = recipient.Address;
                if (address.StartsWith("cc:"))
                {
                    address = address.Substring(3);
                }
                if (address.StartsWith("bcc:"))
                {
                    address = address.Substring(4);
                }
                if (string.IsNullOrEmpty(address))
                {
                    ex.AddError("recipientEmail", "Recipient email is not specified.");
                }
                else if (!address.IsEmail())
                {
                    ex.AddError("recipientEmail", "Recipient email is not valid email address.");
                }
            }

            if (!string.IsNullOrWhiteSpace(replyTo))
            {
                if (!replyTo.IsEmail())
                {
                    ex.AddError("recipientEmail", "Reply To email is not valid email address.");
                }
            }

            if (string.IsNullOrEmpty(subject))
            {
                ex.AddError("subject", "Subject is required.");
            }

            ex.CheckAndThrow();

            Email email = new Email();

            email.Id     = Guid.NewGuid();
            email.Sender = sender ?? new EmailAddress {
                Address = DefaultSenderEmail, Name = DefaultSenderName
            };
            if (string.IsNullOrWhiteSpace(replyTo))
            {
                email.ReplyToEmail = DefaultReplyToEmail;
            }
            else
            {
                email.ReplyToEmail = replyTo;
            }
            email.Recipients = new List <EmailAddress> {
                recipient
            };
            email.Subject      = subject;
            email.ContentHtml  = htmlBody;
            email.ContentText  = textBody;
            email.CreatedOn    = DateTime.UtcNow;
            email.SentOn       = null;
            email.Priority     = priority;
            email.Status       = EmailStatus.Pending;
            email.ServerError  = string.Empty;
            email.ScheduledOn  = email.CreatedOn;
            email.RetriesCount = 0;
            email.ServiceId    = Id;

            email.Attachments = new List <string>();
            if (attachments != null && attachments.Count > 0)
            {
                DbFileRepository fsRepository = new DbFileRepository();
                foreach (var att in attachments)
                {
                    var filepath = att;

                    if (!filepath.StartsWith("/"))
                    {
                        filepath = "/" + filepath;
                    }

                    filepath = filepath.ToLowerInvariant();

                    if (filepath.StartsWith("/fs"))
                    {
                        filepath = filepath.Substring(3);
                    }

                    var file = fsRepository.Find(filepath);
                    if (file == null)
                    {
                        throw new Exception($"Attachment file '{filepath}' not found.");
                    }

                    email.Attachments.Add(filepath);
                }
            }

            new SmtpInternalService().SaveEmail(email);
        }
예제 #3
0
        public void SendEmail(EmailAddress recipient, string subject, string textBody, string htmlBody, List <string> attachments)
        {
            ValidationException ex = new ValidationException();

            if (recipient == null)
            {
                ex.AddError("recipientEmail", "Recipient is not specified.");
            }
            else
            {
                if (string.IsNullOrEmpty(recipient.Address))
                {
                    ex.AddError("recipientEmail", "Recipient email is not specified.");
                }
                else if (!recipient.Address.IsEmail())
                {
                    ex.AddError("recipientEmail", "Recipient email is not valid email address.");
                }
            }

            if (string.IsNullOrEmpty(subject))
            {
                ex.AddError("subject", "Subject is required.");
            }

            ex.CheckAndThrow();

            var message = new MimeMessage();

            if (!string.IsNullOrWhiteSpace(DefaultSenderName))
            {
                message.From.Add(new MailboxAddress(DefaultSenderName, DefaultSenderEmail));
            }
            else
            {
                message.From.Add(new MailboxAddress(DefaultSenderEmail));
            }

            if (!string.IsNullOrWhiteSpace(recipient.Name))
            {
                message.To.Add(new MailboxAddress(recipient.Name, recipient.Address));
            }
            else
            {
                message.To.Add(new MailboxAddress(recipient.Address));
            }

            if (!string.IsNullOrWhiteSpace(DefaultReplyToEmail))
            {
                message.ReplyTo.Add(new MailboxAddress(DefaultReplyToEmail));
            }

            message.Subject = subject;

            var bodyBuilder = new BodyBuilder();

            bodyBuilder.HtmlBody = htmlBody;
            bodyBuilder.TextBody = textBody;

            if (attachments != null && attachments.Count > 0)
            {
                foreach (var att in attachments)
                {
                    var filepath = att;

                    if (!filepath.StartsWith("/"))
                    {
                        filepath = "/" + filepath;
                    }

                    filepath = filepath.ToLowerInvariant();

                    if (filepath.StartsWith("/fs"))
                    {
                        filepath = filepath.Substring(3);
                    }

                    DbFileRepository fsRepository = new DbFileRepository();
                    var file  = fsRepository.Find(filepath);
                    var bytes = file.GetBytes();

                    var extension = Path.GetExtension(filepath).ToLowerInvariant();
                    new FileExtensionContentTypeProvider().Mappings.TryGetValue(extension, out string mimeType);

                    var attachment = new MimePart(mimeType)
                    {
                        Content                 = new MimeContent(new MemoryStream(bytes)),
                        ContentDisposition      = new ContentDisposition(ContentDisposition.Attachment),
                        ContentTransferEncoding = ContentEncoding.Base64,
                        FileName                = Path.GetFileName(filepath)
                    };

                    bodyBuilder.Attachments.Add(attachment);
                }
            }

            SmtpInternalService.ProcessHtmlContent(bodyBuilder);
            message.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
            {
                //accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect(Server, Port, ConnectionSecurity);

                if (!string.IsNullOrWhiteSpace(Username))
                {
                    client.Authenticate(Username, Password);
                }

                client.Send(message);
                client.Disconnect(true);
            }

            Email email = new Email();

            email.Id     = Guid.NewGuid();
            email.Sender = new EmailAddress {
                Address = DefaultSenderEmail, Name = DefaultSenderName
            };
            email.ReplyToEmail = DefaultReplyToEmail;
            email.Recipients   = new List <EmailAddress> {
                recipient
            };
            email.Subject      = subject;
            email.ContentHtml  = htmlBody;
            email.ContentText  = textBody;
            email.CreatedOn    = DateTime.UtcNow;
            email.SentOn       = email.CreatedOn;
            email.Priority     = EmailPriority.Normal;
            email.Status       = EmailStatus.Sent;
            email.ServerError  = string.Empty;
            email.ScheduledOn  = null;
            email.RetriesCount = 0;
            email.ServiceId    = Id;
            if (attachments != null && attachments.Count > 0)
            {
                DbFileRepository fsRepository = new DbFileRepository();
                foreach (var att in attachments)
                {
                    var filepath = att;

                    if (!filepath.StartsWith("/"))
                    {
                        filepath = "/" + filepath;
                    }

                    filepath = filepath.ToLowerInvariant();

                    if (filepath.StartsWith("/fs"))
                    {
                        filepath = filepath.Substring(3);
                    }

                    var file = fsRepository.Find(filepath);
                    if (file == null)
                    {
                        throw new Exception($"Attachment file '{filepath}' not found.");
                    }

                    email.Attachments.Add(filepath);
                }
            }
            new SmtpInternalService().SaveEmail(email);
        }