Exemplo n.º 1
0
        public async Task <EmailSentResult> SendEmailMessage(EmailMessage emailMessage, string userId)
        {
            EmailSentResult emailSentResult = new EmailSentResult()
            {
                Success = false
            };

            try
            {
                var smtp = new SmtpClient
                {
                    Host                  = _cityGlassEmailConfig.Host,
                    Port                  = Convert.ToInt32(_cityGlassEmailConfig.Port),
                    EnableSsl             = Convert.ToBoolean(_cityGlassEmailConfig.Ssl),
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(_cityGlassEmailConfig.FromEmailAddress, _cityGlassEmailConfig.Password)
                };
                using (var smtpMessage = new MailMessage(new MailAddress(_cityGlassEmailConfig.FromEmailAddress, _cityGlassEmailConfig.FromEmailAddressDisplayName)
                                                         , new MailAddress(emailMessage.ReceiverEmail, emailMessage.ReceiverName)))
                {
                    smtpMessage.Subject                     = emailMessage.Subject;
                    smtpMessage.Body                        = emailMessage.Body;
                    smtpMessage.IsBodyHtml                  = emailMessage.IsHtml;
                    smtpMessage.Priority                    = MailPriority.High;
                    smtpMessage.SubjectEncoding             = Encoding.UTF8;
                    smtpMessage.BodyEncoding                = Encoding.UTF8;
                    smtpMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

                    smtp.Send(smtpMessage);

                    emailSentResult.Success = true;
                    emailSentResult.Id      = userId;

                    smtp.Dispose();
                }
            }
            catch (Exception ex)
            {
                emailSentResult.Ex = ex;
            }
            await Task.FromResult(0);

            return(emailSentResult);
        }
Exemplo n.º 2
0
        public EmailSentResult SendEmailMessage(EmailMessage emailMessage, string userId)
        {
            EmailConfiguration emailConfiguration = GetEmailConfiguration();
            EmailSentResult    emailSentResult    = new EmailSentResult()
            {
                Success = false
            };

            try
            {
                var smtp = new SmtpClient
                {
                    Host                  = emailConfiguration.Host,
                    Port                  = emailConfiguration.Port,
                    EnableSsl             = emailConfiguration.Ssl,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(emailConfiguration.FromEmailAddress, emailConfiguration.Password)
                };
                using (var smtpMessage = new MailMessage(new MailAddress(emailConfiguration.FromEmailAddress, emailConfiguration.DisplayName), new MailAddress(emailMessage.ReceiverEmail, emailMessage.ReceiverName)))
                {
                    smtpMessage.Subject                     = emailMessage.Subject;
                    smtpMessage.Body                        = emailMessage.Body;
                    smtpMessage.IsBodyHtml                  = emailMessage.IsHtml;
                    smtpMessage.Priority                    = MailPriority.High;
                    smtpMessage.SubjectEncoding             = Encoding.UTF8;
                    smtpMessage.BodyEncoding                = Encoding.UTF8;
                    smtpMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                    smtp.Send(smtpMessage);

                    emailSentResult.Success = true;
                    emailSentResult.Id      = userId;
                    log.Info("EmailSenderManager - SendEmailMessage - Email send successfully for user id: " + userId + " email: " + emailMessage.ReceiverEmail);

                    smtp.Dispose();
                }
            }
            catch (Exception ex)
            {
                emailSentResult.Ex = ex;
                log.Error(Log4NetMessageHelper.FormateMessageForException(ex, "SendEmailMessage", emailMessage.ReceiverEmail));
            }
            return(emailSentResult);
        }
Exemplo n.º 3
0
        public async Task <EmailSentResult> SendEmailMessageBySendGridAsync(List <EmailMessage> emailMessageList, string userId)
        {
            SendGridConfiguration sendGridConfiguration = GetSendGridConfiguration();
            EmailSentResult       emailSentResult       = new EmailSentResult()
            {
                Success = false
            };
            var    emailMessage      = emailMessageList.FirstOrDefault();
            string receiverEmailList = String.Join(", ", emailMessageList);

            try
            {
                var apiKey = sendGridConfiguration.ApiKey;
                var client = new SendGridClient(apiKey);

                var sendGridMessage = new SendGridMessage()
                {
                    From    = new EmailAddress(sendGridConfiguration.FromEmailAddress, sendGridConfiguration.DisplayName),
                    Subject = emailMessage.Subject,
                    //PlainTextContent = string.Empty,
                    HtmlContent = emailMessage.Body
                };

                foreach (var item in emailMessageList)
                {
                    sendGridMessage.AddTo(new EmailAddress(item.ReceiverEmail, item.ReceiverName));
                }

                var response = await client.SendEmailAsync(sendGridMessage);

                if (response.StatusCode == HttpStatusCode.Accepted)
                {
                    emailSentResult.Success = true;
                    emailSentResult.Id      = userId;
                    log.Info("EmailSenderManager - SendEmailMessageBySendGridAsync - StatusCode: " + response.StatusCode + " - Email send successfully by SendGrid for user id: " + userId + " email list: " + receiverEmailList);
                }
            }
            catch (Exception ex)
            {
                emailSentResult.Ex = ex;
                log.Error(Log4NetMessageHelper.FormateMessageForException(ex, "SendEmailMessageBySendGridAsync", receiverEmailList));
            }
            return(emailSentResult);
        }
        public async Task <EmailSentResult> SendMail(IMail input)
        {
            var result = new EmailSentResult();

            var resultSmtp = await SendViaSmtp(input.MailMessage);

            result.SentWithSmtp = resultSmtp;
            result.MailSent     = resultSmtp;
            //If was sent via smtp just return
            if (resultSmtp)
            {
                return(result);
            }
            //Implement httpServices here
            if (result.SentWithHttp || result.SentWithSmtp)
            {
                result.MailSent = true;
            }
            return(result);
        }
        public async Task <EmailSentResult> SendMail(IMail input)
        {
            var result = new EmailSentResult();

            var resultSmtp = await SendViaSmtp(input.MailMessage);

            //Issue: https://github.com/periface/AbpCinotamZero-SmartAdmin/issues/91
            result.SentWithSmtp = resultSmtp;
            //If was sent via smtp just return
            result.MailSent = resultSmtp;
            if (resultSmtp)
            {
                return(result);
            }

            if (result.SentWithHttp || result.SentWithSmtp)
            {
                result.MailSent = true;
            }
            return(result);
        }