mardi 1 décembre 2015

How do i use parameters from a model to populate the body of an email in mvc

Hello everyone i have made a entity and i have made a view using a model, "clients" now i have tried sending an email via the fields entered in the view this is what i have, for security reasons i have only used one field for a test and the port numbers, host etc is random text

the view:

  @model _2.Models.Client
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
    <fieldset>
            <legend>Client</legend>
    <div class="editor-label">
                @Html.LabelFor(model => model.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
<div class="editor-label">
                @Html.LabelFor(model => model.Initials)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Initials)
                @Html.ValidationMessageFor(model => model.Initials)
            </div>
<div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
      <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

Controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(Client model)
        {
            try
            {


                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("test.co.za"));
                    message.To.Add(new MailAddress("test.co.za")); 
                    message.From = new MailAddress("test.co.za"); 
                    message.Subject = "Your email subject";
                    message.Body = string.Format(body, model.Title, model.Initials, model.FirstName);

                    message.IsBodyHtml = true;

                    using (var smtp = new SmtpClient())
                    {

                        {
                            smtp.Host = "host";
                            smtp.Port = port number;
                            await smtp.SendMailAsync(message);
                            smtp.Dispose();
                            return RedirectToAction("Sent");
                        };



                    }

                }
                return View(model);
            }

            catch (NullReferenceException ex)
            {
              return Content("Data added successfully" + ex); 
            }
        }


        public ActionResult Sent()
        {
            return View();
        }

That is the error i get

System.NullReferenceException: Object reference not set to an instance of an object

it is picking up null values even though i have entered text in the fields, please help me resolve this thank you for your time

Aucun commentaire:

Enregistrer un commentaire