I'm trying to send email from my website using SMTP. I'm getting an error whenever I do. I tested it locally using my ISPs smtp server and it worked great. Now that i'm on the web though that's not the case. I'm not sure what the error I'm getting is other then it doesn't work and I get the error message I have programmed into the site. You can see my code below.
i've tried a couple of different servers with no luck. I know the login/password is good because I verified it. My hosting provider is winhost but my email goes through gmail. So, I setup an account on godaddy that allows 250 relays.
public class EmailMeController : Controller
{
//
// GET: /EmailMe/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(EmailModel emailModel)
{
if (ModelState.IsValid)
{
bool isOk = false;
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]", "Website Contact Form");
msg.To.Add("[email protected]");
msg.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.EmailAddress + "\n"
+ "Website: " + emailModel.WebSite + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ emailModel.Message;
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.secureserver.net", 80);
NetworkCredential Credentials = new NetworkCredential("[email protected]", "password");
smtp.Credentials = Credentials;
smtp.Send(msg);
msg.Dispose();
isOk = true;
MessageModel rcpt = new MessageModel();
rcpt.Title = "Thank You";
rcpt.Content = "Your email has been sent.";
return View("Message", rcpt);
}
catch (Exception ex)
{
}
// If we are here...something kicked us into the exception.
//
MessageModel err = new MessageModel();
err.Title = "Email Error";
err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
return View("Message", err);
}
else
{
return View();
}
}
}
catchright now and replace it with at least some kind of logging; that way, you'll know what the error was.