views:

694

answers:

4

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.

http://pastebin.com/m400a9aa4

+1  A: 

you are swallowing your exceptions. In this catch block:

  1. catch (Exception ex)
  2. {
  3. }

Just write out the error: Response.Write( ex.ToString() )

Also, be sure to loop through any inner exceptions like:

while( ex != null ){
Response.Write( "<HR>" + ex.ToString() );
ex = ex.InnerException;
}

Cheers!

Dave

dave wanta
I was able to get the error, it was quite long (longer then what would fit in the box, so I copy/pasted it here: http://drop.io/xsvslel/asset/error-log
Jason Shultz
Thanks to your advice I was able to get the problem figured out. Thank you.
Jason Shultz
dave wanta
A: 

Apart from doing what Dave is telling you, did you try using the local iis as the smtp server? You can do this in the web.config file, in the system.net section.

<system.net>
    <mailSettings>
        <smtp from="username@domain">
            <network host="localhost" />
        </smtp>
    </mailSettings>
</system.net>
uvita
winhost blocks that functionality so it's not an option.
Jason Shultz
A: 

The solution to the problem was this. I ended up using winhosts SMTP. My email is hosted is hosted on google. I couldn't send to an email address that was of the same domain as my hosting company though because it looked there first for the email address. When it didn't find it it errored out. I instead, had to have the email sent to my gmail account which then forwarded to my openskymedia address based upon return address and some certain text in the email. It's a little convoluted but it worked. The code below was able to work for me to send email from the form and works pretty nicely.

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("your.server.com");
                    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)
                {
                    //while (ex != null)
                    //{
                    //    Response.Write("<HR>" + ex.ToString());
                    //    ex = ex.InnerException;
                    //}

                }

                // 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();
            }
        }
Jason Shultz