Пример #1
0
        public IActionResult Index()
        {
            //MailerModel mdl = new MailerModel("d:\\others\\ES_Emails",@"Views\Email\_EmailLayout.html")//Host name and port number
            //{
            //    User = "******",
            //    Key = "Your Key",
            //    FromAddress = "*****@*****.**",// [email protected]
            //    IsHtml = true,
            //    ViewFile = @"Views\Email\EmailContent.html"
            //};
            MailerModel mdl = new MailerModel
            {
                User               = "******",
                Key                = "Your Key",
                FromAddress        = "*****@*****.**",// [email protected]
                IsHtml             = false,
                PickupPath         = "d:\\others\\ES_Emails",
                UsePickupDirectory = true,
                Message            = "Hi {UserName}," + Environment.NewLine +
                                     $"This is test email {Environment.NewLine}" +
                                     $"Regards, {Environment.NewLine}" +
                                     "{OrganizationName}"
            };

            mdl.Model = new UserModel
            {
                UserName         = "******",
                OrganizationName = "Riy Technologies AB"
            };
            mdl.ToAddresses.Add("*****@*****.**");
            _coreMailer.Send(mdl);
            return(View());
        }
Пример #2
0
 public async Task SendAsync(MailerModel mailer)
 {
     await Task.Run(() =>
     {
         Send(mailer);
     });
 }
Пример #3
0
        public virtual MvcMailMessage PasswordReset(MailerModel model)
        {
            ViewBag.UserName = model.UserName;
            ViewBag.Password = model.Password;

            return(Populate(x =>
            {
                x.Subject = model.Subject;
                x.ViewName = "PasswordReset";
                x.To.Add(model.ToEmail);
                x.IsBodyHtml = true;
            }));
        }
Пример #4
0
        public IActionResult SendToFolder()
        {
            MailerModel mdl = new MailerModel("E:\\Home\\emailPickup")            //Host name and port number
            {
                User        = "******",
                Key         = "Your Key",
                FromAddress = "*****@*****.**",                // [email protected]
                IsHtml      = true,
            };


            mdl.ViewFile = "Home/email_view";
            mdl.Model    = new UserModel
            {
                OrganizationName = "Test Organization"
            };
            mdl.ToAddresses.Add("*****@*****.**");
            _mailer.Send(mdl);
            return(Ok());
        }
Пример #5
0
        // GET: /<controller>/
        public IActionResult Index(bool v = false)
        {
            MailerModel mdl = new MailerModel("Host address", 1234)            //Host name and port number
            {
                User        = "******",
                Key         = "Your Key",
                FromAddress = "Sender email",                // [email protected]
                IsHtml      = v,
            };

            mdl.ToAddresses.Add("receiver email");             // add receiver as many as you want

            var view = _render.RenderView("Home/email_view", mdl.Model = new UserModel
            {
                OrganizationName = "Test Organization"
            });
            var viewHtml = _render.RenderViewToHtml("Home/email_view", mdl.Model = new UserModel
            {
                OrganizationName = "Test Organization"
            });

            if (v)
            {
                mdl.ViewFile = "Home/email_view";
                mdl.Model    = new UserModel
                {
                    OrganizationName = "Test Organization"
                };
            }
            else
            {
                mdl.Message = "This is test message with only text";
            }
            _mailer.Send(mdl);
            return(View());
        }
Пример #6
0
        public void Send(MailerModel mailer)
        {
            if (mailer == null)
            {
                throw new Exception("No valid mailer model found");
            }

            if (mailer.IsValid())
            {
                var messageBody = "";
                if (mailer.HasLayout)
                {
                    messageBody = mailer.Layout.GetLayout();
                }

                if (mailer.IsHtml)
                {
                    if (mailer.HasViewName)
                    {
                        var emailContent = mailer.Model.GetHtmlEmailContent(mailer.ViewFile);
                        messageBody = Regex.Replace(messageBody, @"(?<![\w]){EMAILCONTENT}(?![\w])", emailContent ?? "");
                    }
                    else
                    {
                        var emailContent = mailer.Model.GetTextEmailContent(mailer.Message);
                        messageBody = string.IsNullOrWhiteSpace(messageBody) == false?Regex.Replace(messageBody, @"(?<![\w]){EMAILCONTENT}(?![\w])", emailContent ?? "") : emailContent;
                    }
                }
                else
                {
                    var emailContent = mailer.Model.GetTextEmailContent(mailer.Message);
                    messageBody = string.IsNullOrWhiteSpace(messageBody) == false?Regex.Replace(messageBody, @"(?<![\w]){EMAILCONTENT}(?![\w])", emailContent ?? "") : emailContent;
                }
                var emailMessage =
                    new MailMessage()
                {
                    From       = new MailAddress(mailer.FromAddress),
                    IsBodyHtml = mailer.IsHtml,
                    Subject    = mailer.Subject,
                    Body       = messageBody
                };

                foreach (var toAddress in mailer.ToAddresses)
                {
                    emailMessage.To.Add(toAddress);
                }

                foreach (var replyTo in mailer.ReplyTo)
                {
                    emailMessage.ReplyToList.Add(replyTo);
                }

                foreach (var attachment in mailer.Attachments)
                {
                    emailMessage.Attachments.Add(attachment);
                }

                foreach (var ccAddress in mailer.CC)
                {
                    emailMessage.CC.Add(ccAddress);
                }

                foreach (var bccAddress in mailer.BCC)
                {
                    emailMessage.Bcc.Add(bccAddress);
                }

                if (mailer.UsePickupDirectory)
                {
                    _client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    if (!Directory.Exists(mailer.PickupPath))
                    {
                        Directory.CreateDirectory(mailer.PickupPath);
                    }
                    _client.PickupDirectoryLocation = mailer.PickupPath;
                }
                else
                {
                    _client.Host        = mailer.Host;
                    _client.Port        = mailer.Port;
                    _client.Credentials = new NetworkCredential(mailer.User, mailer.Key);
                }
                _client.Send(emailMessage);
            }
        }
Пример #7
0
        public virtual ActionResult ForgotPassword(ForgotPasswordViewModel model)
        {
            string userName = membershipService.GetUserNameByEmail(model.Email);

            // Get the userName by the email address
            if (string.IsNullOrEmpty(userName))
            {
                ModelState.AddModelError("Email", "Email address does not exist. Please check your spelling and try again.");
                return(RedirectToAction("ForgotPassword"));
            }

            MembershipUser user = membershipService.GetUser(userName);

            if (user == null)
            {
                ModelState.AddModelError("", "The user does not exist.  Please check your entry and try again.");
                return(RedirectToAction("ForgotPassword"));
            }

            if (model.RequireSecretQuestionAndAnswer && model.Checked == false)
            {
                // Get the SecretQuestion
                model.SecretQuestion = user.PasswordQuestion;
                model.Checked        = true;

                return(RedirectToAction("EnterSecretAnswer", model));
            }

            if (model.RequireSecretQuestionAndAnswer && model.Checked == true)
            {
                if (string.IsNullOrEmpty(model.SecretAnswer))
                {
                    ModelState.AddModelError("SecretAnswer", "The Secret Answer is required.");
                    return(RedirectToAction("EnterSecretAnswer", model));
                }
            }



            // Now reset the password
            string newPassword = string.Empty;

            if (membershipService.RequiresQuestionAndAnswer)
            {
                try
                {
                    newPassword = user.ResetPassword(model.SecretAnswer);
                }
                catch (NullReferenceException)
                {
                    ModelState.AddModelError("PasswordAnswer", "The Secret Password is required.");
                    return(RedirectToAction("EnterSecretAnswer", model));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("PasswordAnswer", ex.Message);
                    return(RedirectToAction("EnterSecretAnswer", model));
                }
            }
            else
            {
                newPassword = user.ResetPassword();
            }

            // Email the new pasword to the user
            try
            {
                SmtpSection smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

                // Set the MailerModel properties that will be passed to the MvcMailer object.
                // Feel free to modify the properties as you need.
                MailerModel m = new MailerModel();
                m.UserName  = user.UserName;
                m.Password  = newPassword;
                m.FromEmail = smtp.From;
                m.Subject   = ConfigSettings.SecurityGuardEmailSubject;
                m.ToEmail   = model.Email;

                Mailer.PasswordReset(m).Send();
            }
            catch (Exception ex)
            {
                string a = ex.ToString();
            }

            return(RedirectToAction("ForgotPasswordSuccess"));
        }
Пример #8
0
        public void Send(MailerModel mailer)
        {
            if (mailer == null)
            {
                throw new Exception("No valid mailer model found");
            }

            if (mailer.IsValid())
            {
                string messageBody;
                if (_renderer != null)
                {
                    messageBody = mailer.HasViewName
                        ? _renderer.RenderView(mailer.ViewFile, mailer.Model)
                        : mailer.Message;
                }
                else
                {
                    messageBody = mailer.Message;
                }


                var emailMessage =
                    new MailMessage()
                {
                    From       = new MailAddress(mailer.FromAddress),
                    IsBodyHtml = mailer.IsHtml,
                    Subject    = mailer.Subject,
                    Body       = messageBody
                };

                foreach (var toAddress in mailer.ToAddresses)
                {
                    emailMessage.To.Add(toAddress);
                }

                foreach (var replyTo in mailer.ReplyTo)
                {
                    emailMessage.ReplyToList.Add(replyTo);
                }

                foreach (var attachment in mailer.Attachments)
                {
                    emailMessage.Attachments.Add(attachment);
                }

                foreach (var ccAddress in mailer.CC)
                {
                    emailMessage.CC.Add(ccAddress);
                }

                foreach (var bccAddress in mailer.BCC)
                {
                    emailMessage.Bcc.Add(bccAddress);
                }

                if (mailer.UsePickupDirectory)
                {
                    _client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    if (!Directory.Exists(mailer.PickupPath))
                    {
                        Directory.CreateDirectory(mailer.PickupPath);
                    }
                    _client.PickupDirectoryLocation = mailer.PickupPath;
                }
                else
                {
                    _client.Host        = mailer.Host;
                    _client.Port        = mailer.Port;
                    _client.Credentials = new NetworkCredential(mailer.User, mailer.Key);
                }



                _client.Send(emailMessage);
            }
        }