Monday, 20 May 2013

MailMessage using.System.Net.Mail not working for me

MailMessage using.System.Net.Mail not working for me

I am new here and new to MVC4. There seem to be little guidance in MVC4 and I am trying to learn it on my own. So far I have created a site that is working well except a few glitches that I would like to get worked out and hopefully learn from this.
SO I created a simple contact form. But it is useless. I need it to send the information in the form to my email. Very easy process in PHP, and HTML but .NET seems to be very difficult.
Here is what I have in my Controller for the contact page (contact form)
   [HttpPost]
    public ActionResult Contact(ContactsModel model)
    {
        if (ModelState.IsValid)
        {
            InsertContact(model.Name, model.Phone, model.Email, model.Comments);

            MailMessage message = new MailMessage();
            message.From = new MailAddress(sender1@foo.bar.com);
            message.To.Add(new MailAddress(lotusms@outlook.com));
            message.Subject = "This is my subject";
            message.Body = "This is the content";
            SmtpClient client = new SmtpClient();
            client.Send(message);

            TempData["notice"] = "Someone will reply as soon as possible.";
            return RedirectToAction("Index", "Home");
        }

        return View();

    }
And this is my actual form code in my view
@using(Html.BeginForm())
{
@Html.LabelFor(model => model.Name)<br />
@Html.TextBoxFor(model => model.Name)<br />
@Html.ValidationMessageFor(model => model.Name)<br />

@Html.LabelFor(model => model.Phone)<br />
@Html.TextBoxFor(model => model.Phone)<br />   
@Html.ValidationMessageFor(model => model.Phone)<br />

@Html.LabelFor(model => model.Email)<br />
@Html.TextBoxFor(model => model.Email)<br />
@Html.ValidationMessageFor(model => model.Email)<br />

@Html.LabelFor(model => model.Comments)<br />
@Html.TextAreaFor(model => model.Comments)<br />
@Html.ValidationMessageFor(model => model.Comments)<br />

<button type="submit">SEND</button> //onClick="document.getElementById('contacts-form').submit()"
<button type="reset">CLEAR</button> // onClick="document.getElementById('contacts-form').reset()"
}</pre>
I am getting this error when I submit
    The remote name could not be resolved: 'smtpserver1'

     Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      Exception Details: System.Net.WebException: The remote name could not be resolved: 'smtpserver1'
And this is the site if you'd like to check it out
http://djfitz.azurewebsites.net/[^]
Thank you so much

No comments:

Post a Comment