Reputation: 1480
Here is my web.config
<system.net>
<mailSettings>
<smtp from="myemail">
<network host="ispsmtpaddress" port="25" userName="user" password="pass" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
this is below the system.web closing tag
Here is my controller;
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult ContactUs()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult ContactUs(ContactMessage message)
{
string successMessage = "Your message was sent successfully.";
try
{
MailMessage email = new MailMessage()
{
Subject = "Email from EmailForm",
Body = message.MessageBody,
IsBodyHtml = false,
From = new MailAddress(message.Email, message.Name)
};
email.To.Add(new MailAddress("[email protected]", "Name Of My Project"));
SmtpClient server = new SmtpClient();
server.Send(email);
}
catch (Exception ex)
{
successMessage = ex.Message;
}
ViewData["SuccessMessage"] = successMessage;
return View();
}
here is my model;
namespace NewAtAClick.Models
{
public class ContactMessage
{
public string Name { get; set; }
public string Email { get; set; }
public string MessageBody { get; set; }
}
}
And my view
@using (Html.BeginForm())
{
<div>
<p>@ViewData["SuccessMessage"]</p>
Name
<div>
@Html.TextBox("Name", String.Empty, new { @class = "cls-text-input" })
</div>
Email
<div>
@Html.TextBox("Email", String.Empty, new { @class = "cls-text-input" })
</div>
Message
<div>
@Html.TextArea("MessageBody")
</div>
<div>
<input type="submit" value="Submit Message" />
</div>
</div>
This is the error I'm getting on screen
'The smtp host is not specified". I have included my ISP's smtp address in the web.config. I have tried a few different configurations but it won't work. I'm developing on my local machine so I've alse tried locaLhost with no user and pass but nothing. Any help would be greatly appreciated. Thanks!
Upvotes: 1
Views: 1304
Reputation: 877
You should set the defaultcredentials in the web.config to false to use user supplied credentials.
Also set the deliveryMethod on SMTP to network
Upvotes: 1