コード例 #1
0
        public ActionResult Contact(ContactFormModel model)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "-Selecteer een optie-", Value = "0" });
            list.Add(new SelectListItem { Text = "Er doet zich een probleem voor", Value = "Probleem" });
            list.Add(new SelectListItem { Text = "Ik wil iets melden", Value = "Melding" });
            list.Add(new SelectListItem { Text = "Een gebruiker stoort onrust", Value = "Boosdoener" });
            list.Add(new SelectListItem { Text = "Ik heb een algemene vraag", Value = "Algemene vraag" });
            list.Add(new SelectListItem { Text = "Andere", Value = "Andere" });
            ViewData["Subjects"] = list;

            if (ModelState.IsValid)
            {
                //SENDING EMAIL
                try
                {
                    //1. GET EMAIL TO INFORMATION
                    string emailToName = "SocialGeo WAY";
                    string emailTo = "*****@*****.**";

                    //2. GET TEMPLATES FOR CONTACT MAIL
                    //2.1. PLAIN TEMPLATE
                    string physicalPath = Server.MapPath("~/Templates/Mail/ContactPlain.txt");
                    string fileContent = System.IO.File.ReadAllText(physicalPath);
                    ListDictionary dictionary = new ListDictionary();
                    dictionary.Add("<%FROMNAME%>", model.Name);
                    dictionary.Add("<%FROMEMAIL%>", model.Email);
                    dictionary.Add("<%SUBJECT%>", model.Subject);
                    dictionary.Add("<%BODY%>", model.Body);
                    dictionary.Add("<%TONAME%>", emailToName);
                    foreach (string key in dictionary.Keys)
                    {
                        fileContent = fileContent.Replace(key, (string)dictionary[key]);
                    }

                    //3. SETUP MAILMANAGER
                    MailManager mailManager = new MailManager();
                    #region GMAIL
                    mailManager.SmtpHost = "smtp.gmail.com";
                    mailManager.SmtpPort = 587;
                    mailManager.IsSSL = true;
                    mailManager.SmtpLogin = "******";
                    mailManager.SmtpPassword = "******";
                    #endregion
                    mailManager.EmailFrom = new MailAddress(model.Email, model.Name);
                    mailManager.EmailTos.Add(new MailAddress(emailTo, emailToName));
                    mailManager.EmailSubject = model.Subject;
                    mailManager.EmailBodyPlain = fileContent;
                    mailManager.SendMail();

                    ModelState.AddModelError("", "Correct");
                    return View();
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.ToString());
                    return View();
                }
            }
            ModelState.AddModelError("", "Niet alle ingevulde velden zijn valid.");
            return View();
        }
コード例 #2
-1
        public ActionResult SendContactForm(ContactFormModel model)
        {
            if (ModelState.IsValid)
            {
                //SENDING EMAIL
                try
                {
                    //1. GET EMAIL TO INFORMATION
                    string emailToName = "Louis Van de Calseyde";
                    string emailTo = "*****@*****.**";

                    //2. GET TEMPLATES FOR CONTACT MAIL
                    //2.1. PLAIN TEMPLATE (NO HTML MAIL FOR CONTACT THINS
                    string physicalPath = Server.MapPath("~/Templates/Mail/ContactPlain.txt");
                    string fileContent = System.IO.File.ReadAllText(physicalPath);
                    ListDictionary dictionary = new ListDictionary();
                    dictionary.Add("<%FROMNAME%>", model.Name);
                    dictionary.Add("<%FROMEMAIL%>", model.Email);
                    dictionary.Add("<%SUBJECT%>", model.Subject);
                    dictionary.Add("<%BODY%>", model.Body);
                    dictionary.Add("<%TONAME%>", emailToName);
                    foreach (string key in dictionary.Keys)
                    {
                        fileContent = fileContent.Replace(key, (string)dictionary[key]);
                    }

                    //3. SETUP MAILMANAGER
                    MailManager mailManager = new MailManager();
                    #region GMAIL
                    mailManager.SmtpHost = "smtp.gmail.com";
                    mailManager.SmtpPort = 587;
                    mailManager.IsSSL = true;
                    mailManager.SmtpLogin = "******";
                    mailManager.SmtpPassword = "******";
                    #endregion
                    mailManager.EmailFrom = new MailAddress(model.Email, model.Name);
                    mailManager.EmailTos.Add(new MailAddress(emailTo, emailToName));
                    mailManager.EmailSubject = model.Subject;
                    mailManager.EmailBodyPlain = fileContent;
                    mailManager.SendMail();

                    return Json(new JsonBack { Result = 2, Message = "Uw aanvraag werd verstuurd!" });
                }
                catch (Exception e)
                {
                    return Json(new JsonBack { Result = 1, Message = e.ToString() });
                }
            }
            return Json(new JsonBack { Result = 0, Message = "Niet alle ingevulde velden zijn valid" });
        }