예제 #1
0
        public async Task <ActionResult> Contact(EmailFormModel model)
        {
            if (ModelState.IsValid)
            {
                var body    = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message = new MailMessage();
                message.To.Add(new MailAddress(model.RecipientEmail)); // replace with valid value
                message.From       = new MailAddress(model.FromEmail); // replace with valid value
                message.Subject    = "Your email subject";
                message.Body       = string.Format(body, model.FromName, model.FromEmail, model.Message);
                message.IsBodyHtml = true;

                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = ApiKeys.Email(),        // replace with valid value
                        Password = ApiKeys.EmailPassword() // replace with valid value
                    };
                    smtp.Credentials = credential;
                    smtp.Host        = "smtp-mail.outlook.com";
                    smtp.Port        = 587;
                    smtp.EnableSsl   = true;
                    await smtp.SendMailAsync(message);

                    return(RedirectToAction("Sent"));
                }
            }
            return(View(model));
        }