Esempio n. 1
0
        public IActionResult Put(EmailAccount entityModel)
        {
            var emailAccount = _emailAccountService.FirstOrDefault(x => x.Id == entityModel.Id);

            //save it and respond
            emailAccount.DisplayName = entityModel.DisplayName;
            emailAccount.Host        = entityModel.Host;

            emailAccount.Port     = entityModel.Port;
            emailAccount.Email    = entityModel.Email;
            emailAccount.Username = entityModel.Username;
            if (entityModel.Password.IsNotNullOrEmpty())
            {
                // TODO encrypt password
                emailAccount.Password = entityModel.Password;
                // emailAccount.Password = _cryptographyService.Encrypt(entityModel.Password);
            }

            emailAccount.IsDefault = entityModel.IsDefault;


            _emailAccountService.Update(emailAccount);

            VerboseReporter.ReportSuccess("Sửa EmailAccount thành công", "post");

            return(RespondSuccess(emailAccount));
        }
Esempio n. 2
0
        public bool SendEmail(EmailMessage emailMessage, bool verboseErrorOnFailure = false)
        {
            var emailAccount = emailMessage.EmailAccount ?? _emailAccountService.FirstOrDefault(x => x.IsDefault == true);//x => x.IsDefault

            if (emailAccount == null)
            {
                throw new Exception("Can't send email without account");
                //return false; //can't send email without account
            }

            if (emailMessage.Tos == null)
            {
                throw new Exception("At least one of Tos, CCs or BCCs must be specified to send email");
            }
            bool response = false;

            if (emailMessage.Tos != null)
            {
                foreach (var userInfo in emailMessage.Tos)
                {
                    response = SendEmail(emailAccount,
                                         emailMessage.Subject,
                                         emailMessage.EmailBody,
                                         emailAccount.Username,
                                         emailAccount.DisplayName,
                                         userInfo.Email,
                                         userInfo.Name);
                }
            }

            return(response);
        }
        public IHttpActionResult Put(EmailAccountEntityModel entityModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            //get the account
            var emailAccount = _emailAccountService.Get(entityModel.Id);

            if (emailAccount == null)
            {
                return(NotFound());
            }

            emailAccount.FromName              = entityModel.FromName;
            emailAccount.Host                  = entityModel.Host;
            emailAccount.IsDefault             = entityModel.IsDefault;
            emailAccount.Port                  = entityModel.Port;
            emailAccount.UseDefaultCredentials = entityModel.UseDefaultCredentials;
            emailAccount.UseSsl                = entityModel.UseSsl;
            emailAccount.UserName              = entityModel.UserName;
            emailAccount.Email                 = entityModel.Email;

            //we'll change password if it's provided
            if (!string.IsNullOrEmpty(entityModel.Password))
            {
                emailAccount.Password = _cryptographyService.Encrypt(entityModel.Password);
            }
            //save it
            _emailAccountService.Update(emailAccount);

            //if this is a default account now, update the previous one
            if (emailAccount.IsDefault)
            {
                var oldDefaultEmailAccount = _emailAccountService.FirstOrDefault(x => x.IsDefault && x.Id != emailAccount.Id);
                if (oldDefaultEmailAccount != null)
                {
                    oldDefaultEmailAccount.IsDefault = false;
                    _emailAccountService.Update(emailAccount);
                }
            }
            VerboseReporter.ReportSuccess("Successfully updated email account", "put_emailaccount");
            return(RespondSuccess(new {
                EmailAccount = emailAccount.ToEntityModel()
            }));
        }
Esempio n. 4
0
        private bool GetMailObjects(EmailMessage emailMessage, out MailMessage mailMessage, out SmtpClient smtpClient)
        {
            mailMessage = null;
            smtpClient  = null;
            //we need an email account
            var emailAccount = emailMessage.EmailAccount ??
                               _emailAccountService.Get(_emailSenderSettings.DefaultEmailAccountId) ??
                               _emailAccountService.FirstOrDefault(x => true);

            if (emailAccount == null)
            {
                _logger.Log <EmailService>(LogLevel.Error, $"Failed to send email. No default email account could be loaded.");
                return(false); //can't send email without account
            }

            var message = new MailMessage();

            //from, to, reply to
            message.From = new MailAddress(emailAccount.Email, emailAccount.FromName);

            if (emailMessage.Tos == null && emailMessage.Ccs == null && emailMessage.Bccs == null)
            {
                throw new EvenCartException("At least one of Tos, CCs or BCCs must be specified to send email");
            }

            if (emailMessage.Tos != null)
            {
                foreach (var userInfo in emailMessage.Tos)
                {
                    message.To.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.ReplyTos != null)
            {
                foreach (var userInfo in emailMessage.ReplyTos)
                {
                    message.ReplyToList.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.Bccs != null)
            {
                foreach (var userInfo in emailMessage.Bccs)
                {
                    message.Bcc.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.Ccs != null)
            {
                foreach (var userInfo in emailMessage.Ccs)
                {
                    message.Bcc.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            //content
            message.Subject    = emailMessage.Subject;
            message.Body       = emailMessage.EmailBody;
            message.IsBodyHtml = emailMessage.IsEmailBodyHtml;

            //headers
            if (emailMessage.Headers != null)
            {
                foreach (var header in emailMessage.Headers)
                {
                    message.Headers.Add(header.Key, header.Value);
                }
            }

            if (emailMessage.Attachments != null)
            {
                foreach (var attachment in emailMessage.Attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            //send email

            var password = _cryptographyService.Decrypt(emailAccount.Password);

            smtpClient = new SmtpClient
            {
                UseDefaultCredentials = emailAccount.UseDefaultCredentials,
                Host        = emailAccount.Host,
                Port        = emailAccount.Port,
                EnableSsl   = emailAccount.UseSsl,
                Credentials = emailAccount.UseDefaultCredentials
                    ? CredentialCache.DefaultNetworkCredentials
                    : new NetworkCredential(emailAccount.UserName, password)
            };
            mailMessage = message;
            return(true);
        }
Esempio n. 5
0
        public bool SendEmail(EmailMessage emailMessage, bool verboseErrorOnFailure = false)
        {
            //we need an email account
            var emailAccount = emailMessage.EmailAccount ?? _emailAccountService.FirstOrDefault(x => x.IsDefault);

            if (emailAccount == null)
            {
                return(false); //can't send email without account
            }
            var message = new MailMessage();

            //from, to, reply to
            message.From = new MailAddress(emailAccount.Email, emailAccount.FromName);

            if (emailMessage.Tos == null && emailMessage.Ccs == null && emailMessage.Bccs == null)
            {
                throw new mobSocialException("At least one of Tos, CCs or BCCs must be specified to send email");
            }

            if (emailMessage.Tos != null)
            {
                foreach (var userInfo in emailMessage.Tos)
                {
                    message.To.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.ReplyTos != null)
            {
                foreach (var userInfo in emailMessage.ReplyTos)
                {
                    message.ReplyToList.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.Bccs != null)
            {
                foreach (var userInfo in emailMessage.Bccs)
                {
                    message.Bcc.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            if (emailMessage.Ccs != null)
            {
                foreach (var userInfo in emailMessage.Ccs)
                {
                    message.Bcc.Add(new MailAddress(userInfo.Email, userInfo.Name));
                }
            }

            //content
            message.Subject    = emailMessage.Subject;
            message.Body       = emailMessage.EmailBody;
            message.IsBodyHtml = emailMessage.IsEmailBodyHtml;

            //headers
            if (emailMessage.Headers != null)
            {
                foreach (var header in emailMessage.Headers)
                {
                    message.Headers.Add(header.Key, header.Value);
                }
            }

            if (emailMessage.Attachments != null)
            {
                foreach (var attachment in emailMessage.Attachments)
                {
                    message.Attachments.Add(attachment);
                }
            }

            //send email

            var password = _cryptographyService.Decrypt(emailAccount.Password);

            using (var smtpClient = new SmtpClient())
            {
                smtpClient.UseDefaultCredentials = emailAccount.UseDefaultCredentials;
                smtpClient.Host        = emailAccount.Host;
                smtpClient.Port        = emailAccount.Port;
                smtpClient.EnableSsl   = emailAccount.UseSsl;
                smtpClient.Credentials = emailAccount.UseDefaultCredentials ?
                                         CredentialCache.DefaultNetworkCredentials :
                                         new NetworkCredential(emailAccount.UserName, password);
                try
                {
                    smtpClient.Send(message);
                    //update the send status
                    emailMessage.IsSent = true;
                    Update(emailMessage);
                    return(true);
                }
                catch (Exception ex)
                {
                    if (verboseErrorOnFailure)
                    {
                        var verboseReporterService = mobSocialEngine.ActiveEngine.Resolve <IVerboseReporterService>();
                        verboseReporterService.ReportError(ex.Message, "send_email");
                    }
                    return(false);
                }
            }
        }