1

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


}

http://pastebin.com/m400a9aa4

3
  • 3
    No error message likely means no answer. Get rid of that empty catch right now and replace it with at least some kind of logging; that way, you'll know what the error was. Commented Feb 9, 2010 at 4:01
  • i don't know how to log the error. Also, I tested it again this morning using the same code both locally on cassini and remotely on the live server. it works locally, but does not work remotely. Nothing changes including the smtp information. Commented Feb 9, 2010 at 13:23
  • 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: drop.io/xsvslel/asset/error-log Commented Feb 9, 2010 at 14:18

3 Answers 3

2

you are swallowing your exceptions. In this catch block:

61.                catch (Exception ex)
62.                {
63.                }

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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: drop.io/xsvslel/asset/error-log
Thanks to your advice I was able to get the problem figured out. Thank you.
Ok, that helps a bit. The bottom line is your app doesn't have access to the System.Net.Mail namespace. You need to give it permissions. I don't know enough about your setup, but here is a google link to get you started: google.com/…
1

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

Comments

0

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>

1 Comment

winhost blocks that functionality so it's not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.