コード例 #1
0
 private void SaveToDatabase(ContactEntry entry)
 {
     using (SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
     {
         cnn.Open();
         cnn.Execute("INSERT ContactEntry(Name, EmailAddress, Message) VALUES (@name, @emailaddress, @message)", new[] { new { name = entry.Name, emailaddress = entry.EmailAddress, message = entry.Message } });
     }
 }
コード例 #2
0
        public ActionResult Index(ContactEntry entry)
        {
            //try
            //{
            //    var errors = DataAnnotationsValidationRunner.GetErrors(entry).ToList();
            //    if (errors.Any())
            //        throw new RulesException(errors);
            //}
            //catch (RulesException e)
            //{
            //    e.AddModelStateErrors(ModelState, "entry");
            //    return View(entry);
            //}

            SaveToDatabase(entry);
            SendEmail(entry);

            return View("Thanks");
        }
コード例 #3
0
        private static void SendEmail(ContactEntry entry)
        {
            var mailMessage = new MailMessage("*****@*****.**", "*****@*****.**")
                                  {
                                      Subject = "Website contact"
                                  };

            var builder = new StringBuilder();
            builder.AppendFormat("Name: {0}\n", entry.Name);
            builder.AppendFormat("Email address: {0}\n\n", entry.EmailAddress);
            builder.AppendFormat("Message: {0}", entry.Message);

            mailMessage.Body = builder.ToString();

            try
            {
                var client = new SmtpClient();
                client.Send(mailMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }