示例#1
0
        public IResultModel SendTestEmail(string email, string message)
        {
            IResultModel             rm = new ResultModel();
            EmailMessageServiceModel em = new EmailMessageServiceModel();

            try
            {
                em.Body = this.emailTemplateService.TestMessage(message);
                em.DontReplyThisMessage = true;
                em.IsHtml          = true;
                em.Subject         = "[GRAFIDEC] Email de Prueba";
                em.TargetUserEmail = email;

                var rmEmail = this.emailService.SendMail(em);
                if (rmEmail.Success)
                {
                    rm.OnSuccess(rmEmail.Message);
                }
                else
                {
                    rm.OnError(rmEmail.ErrorMessage, rmEmail.ErrorCode);
                }
            }
            catch (Exception ex)
            {
                rm.OnException(ex);
            }

            return(rm);
        }
示例#2
0
        public IResultModel SendReportToUsers(string subject, string[] listOfEmails, string message)
        {
            IResultModel             rm = new ResultModel();
            IResultModel             rmEmail;
            EmailMessageServiceModel em = new EmailMessageServiceModel();

            try
            {
                em.Body = message;
                em.DontReplyThisMessage = true;
                em.IsHtml  = true;
                em.Subject = subject;

                em.SendToSeveral = true;
                em.UserEmailList = new List <string>(listOfEmails);

                rmEmail = this.emailService.SendMailToSeveral(em);

                if (!rmEmail.Success)
                {
                    return(rm.OnError(rmEmail));
                }
                else
                {
                    rm.OnSuccess();
                }
            }
            catch (Exception ex)
            {
                rm.OnException(ex);
            }

            return(rm);
        }
示例#3
0
        public IResultModel SendEmailNewCustomerSignUp(Customer customer, string hostAddress)
        {
            IResultModel             rm = new ResultModel();
            EmailMessageServiceModel em = new EmailMessageServiceModel();

            //hostAddress += "#/login";
            hostAddress += "#/login/{0}".sf(customer.Email);

            try
            {
                em.Body = this.emailTemplateService.CustomerSignUp(customer.Email, hostAddress);
                em.DontReplyThisMessage = true;
                em.IsHtml          = true;
                em.Subject         = CustomerResources.SignUpMailSubject;
                em.TargetUserEmail = customer.Email;

                var rmEmail = this.emailService.SendMail(em);
                if (rmEmail.Success)
                {
                    rm.OnSuccess(rmEmail.Message);
                }
                else
                {
                    rm.OnError(rmEmail.ErrorMessage, rmEmail.ErrorCode);
                }
            }
            catch (Exception ex)
            {
                rm.OnException(ex);
            }

            return(rm);
        }
示例#4
0
        public IResultModel SendMailToSeveral(EmailMessageServiceModel em)
        {
            var response = new ResultModel();

            if (!em.SendToSeveral)
            {
                return(response.OnError());
            }

            try
            {
                var         message = new System.Net.Mail.MailMessage();
                MailAddress userEmail;
                MailAddress systemEmail = new MailAddress(this.systemEmailUser);
                message.IsBodyHtml = em.IsHtml;
                //message.Sender = systemEmail;

                foreach (string address in em.UserEmailList)
                {
                    userEmail = new MailAddress(address);
                    message.To.Add(userEmail);
                }
                message.Subject = em.Subject;
                message.Body    = em.Body;

                if (!string.IsNullOrEmpty(em.FromEmail))
                {
                    message.From = new MailAddress(em.FromEmail);
                }
                else
                {
                    message.From = systemEmail;
                }
                if (!string.IsNullOrWhiteSpace(em.ReplyToEmail))
                {
                    message.ReplyToList.Add(new MailAddress(em.ReplyToEmail));
                    //message.Headers.Add("Reply-To", em.ReplyToEmail);
                }

                var smtp = new System.Net.Mail.SmtpClient(this.smtp, Convert.ToInt32(25));
                smtp.UseDefaultCredentials = true;
                var credentials = new NetworkCredential(this.systemEmailUser, this.systemEmailPassword);
                smtp.Credentials = credentials;
                smtp.EnableSsl   = false;

                //TODO: Transient fault!!
                smtp.Send(message);

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success   = false;
                response.Exception = ex;
                response.Message   = ErrorResources.ErrorAtSendingEmailMessage;
            }

            return(response);
        }
示例#5
0
        public List <EmailMessageServiceModel> ReceiveMessage(int maxCount = 10)
        {
            try
            {
                using (var emailClient = new Pop3Client())
                {
                    emailClient.Connect(_emailConfiguration.PopServer, _emailConfiguration.PopPort, true);

                    emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

                    emailClient.Authenticate(_emailConfiguration.PopUsername, _emailConfiguration.PopPassword);

                    List <EmailMessageServiceModel> emails = new List <EmailMessageServiceModel>();
                    for (int i = 0; i < emailClient.Count && i < maxCount; i++)
                    {
                        var message      = emailClient.GetMessage(i);
                        var emailMessage = new EmailMessageServiceModel
                        {
                            Content = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody,
                            Subject = message.Subject
                        };
                        emailMessage.ToAddresses.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new EmailAddress {
                            Address = x.Address, Name = x.Name
                        }));
                        emailMessage.FromAddresses.AddRange(message.From.Select(x => (MailboxAddress)x).Select(x => new EmailAddress {
                            Address = x.Address, Name = x.Name
                        }));
                        emails.Add(emailMessage);
                    }

                    return(emails);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#6
0
        public void Send(EmailMessageServiceModel emailMessage)
        {
            try
            {
                var message = new MimeMessage();
                message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
                message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));

                message.Subject = emailMessage.Subject;
                //We will say we are sending HTML. But there are options for plaintext etc.
                message.Body = new TextPart(TextFormat.Html)
                {
                    Text = emailMessage.Content
                };

                //Be careful that the SmtpClient class is the one from Mailkit not the framework!
                using (var emailClient = new SmtpClient())
                {
                    //The last parameter here is to use SSL (Which you should!)
                    emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, true);

                    //Remove any OAuth functionality as we won't be using it.
                    emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

                    emailClient.Authenticate(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword);

                    emailClient.Send(message);

                    emailClient.Disconnect(true);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#7
0
        //  Get reset password token
        public string GetResetToken(string email)
        {
            try
            {
                if (string.IsNullOrEmpty(email))
                {
                    return("");
                }
                if (CheckValidEmail(email))
                {
                    return(null);
                }
                //  Modify list email send
                List <EmailAddress> fromEmail = new List <EmailAddress>();
                fromEmail.Add(new EmailAddress()
                {
                    Address = "*****@*****.**",
                    Name    = "Tien Dao"
                });
                List <EmailAddress> toEmail = new List <EmailAddress>();
                toEmail.Add(new EmailAddress()
                {
                    Address = email,
                    Name    = _repo.GetActive().Where(p => p.Email == email).FirstOrDefault().Name
                });

                EmailMessageServiceModel message = new EmailMessageServiceModel()
                {
                    Subject       = "Reset token for unilog",
                    Content       = "Please use this token to change password: "******"*****@*****.**", "Tien Dao");
                //    //  Send to List
                //    // foreach (var item in listTo)
                //    // {
                //    //     message.To.Add(new MailAddress("*****@*****.**", "To Name"));
                //    // }
                //    //foreach (var item in listCC)
                //    // {
                //    //     message.CC.Add(new MailAddress("*****@*****.**", "To Name"));
                //    // }
                //    //foreach (var item in listBcc)
                //    // {
                //    //     message.Bcc.Add(new MailAddress("*****@*****.**", "To Name"));
                //    // }
                //    messageRegion.To.Add(new MailAddress(email, _repo.GetActive().Where(p => p.Email == email).FirstOrDefault().Name));
                //    messageRegion.CC.Add(new MailAddress(email, "CC Name"));
                //    messageRegion.Bcc.Add(new MailAddress(email, "BCC Name"));
                //    messageRegion.Subject = "Reset Token";
                //    messageRegion.Body = "Please use this token to change password: "******"smtp.gmail.com"))
                //    {
                //        client.Port = 587;
                //        client.Credentials = new NetworkCredential("*****@*****.**", "Tienthang123");
                //        client.EnableSsl = true;
                //        client.Send(messageRegion);
                //    }
                //}
                //#endregion
                return("Token sent, please check your email");
            }
            catch (Exception)
            {
                throw;
            }
        }