locked
I deliberately do not enter contact information in the fields in order to see what will be limited, This message is broken: "The value can not be null. Parameter name: address" RRS feed

  • Question

  • using WebApplication1.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using System.Web;
    using System.Web.Mvc;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }

            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";

                return View();
            }
            [Authorize]
            [HttpGet]
            public ActionResult Contact()
            {

                return View();
            }
            [HttpPost] 
            public ActionResult Contact(ContactModel contact)
            {
                var mail = new MailMessage();
                var loginInfo = new NetworkCredential("","");
                mail.From = new MailAddress(contact.Email);
                mail.To.Add(new MailAddress(""));
                mail.Subject = contact.Objet;
                mail.IsBodyHtml = true;
                String body = "Nom de l'expéditeur : " + contact.Nom + "<br>" +
                              "Email de L'expéditeur : " + contact.Email + "<br>" +
                               "Objet : " + contact.Objet + "<br>" +
                               "Message : "  + contact.Message + "<b>";
                mail.Body = body;

                var smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = loginInfo;
                smtpClient.Send(mail);
                return RedirectToAction("Index");
            }
        }
    }

                    ___________________________________________________________________________________________________

    using System;

    using System.Collections.Generic;

    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;

    namespace WebApplication1.Models
    {
        public class ContactModel
        {
            [Required]
            public String Nom { get; set; }
            [Required]
            public String Email { get; set; }
            [Required]
            public String Objet { get; set; }
            [Required]
            public String Message { get; set; }

        }
    }

                

    Monday, February 26, 2018 2:14 PM

All replies

  • First, I really hope they are not your real gmail credentials. If they are - CHANGE YOUR PASSWORD NOW!

    Second, not sure what your issue is. If you do not enter an Email address then you will get an error telling you that the address cannot be null. Which you are. This is expected.

    Monday, February 26, 2018 2:23 PM
  • thank you 

    I want this message to be displayed when the values are not entered into the fields: The XXX field is required.

    Monday, February 26, 2018 2:38 PM
  • You would be better off performing your own validation.

    If you are using ASP.NET (?) then search for "ASP.NET Validation" for documentation and examples (if you are using a different technology then obviously search for that instead).

    With your own validation you can control the message that gets displayed. You should never just rely on system exceptions to validate user input.

    (But you should still handle any exceptions when the email is sent anyway, just in case).


    • Edited by RJP1973 Monday, February 26, 2018 2:48 PM
    Monday, February 26, 2018 2:48 PM
  • Hi Achraf-help,

    Thank you for posting here.

    For your question is more related to MVC, you could post a new thread in MVC forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 5:57 AM