I’ve been lulled into a cushy existence of late, doing a lot of work within the confines of WordPress. Pop in the Contact Form 7 plugin, setup the page and magically email is flowing from your site into your inbox. Ah, its a beautiful thing. Once you leave the tranquil kingdom of WordPress and its wonderful open source plugins, things start to get a bit thornier.

So we currently have a client who we is developing an affiliate tracking system utilizing ASP.NET. The system itself is the meat of the website; tracking sales, serving up promotional graphics as well as automated javascript elements for the affiliates. Still, the client needs a quick website to stand between them and the outside world, telling visitors what they are about. We pulled together a quick, responsive HTML5 design that has a trendy, modern feel. It is the essence of a no fuss web presence, but relays all of the information necessary to sway a potential clients. The only dynamic element on the site is an email contact form.

The site is being hosted on a Windows server with Godaddy. You’d think implementing a contact form would be pretty straightforward matter, but unfortunately its not. I started doing research online, and I found every coder seemed to have a different “solution.” I seemingly tried them all before I finally got it working so I thought I would demystify the process to uncover what is tripping up so many people when it comes to implementing ASP.NET forms on Godaddy.

First, you have to define what kind of email address you are sending from. Is this a Godaddy email hosted account (i.e. a website you have registered and are hosting through Godaddy that you’ve setup an email account for) or are you running it through another email provider that allows relaying such as Gmail? Alternately, if you have a Godaddy email account, but you are hosting the site outside of Godaddy that brings up a completely different configuration. For sanities sake, let’s start with the basics:

Sending to an Email Address Setup in Godaddy Email Hosting & the Website is Hosted on Godaddy

I’ve broken the code out as a code behind file written in c# (see below). Most of this is pretty elementary as you are loading the .Net mail libraries and defining the pieces of your mail message: to, from, subject, body text, etc. I’d imagine this is old hat to most of you by now.

The tricky part comes nestled within the SMTP settings. The SMTP.Host will be set as relay-hosting.secureserver.net and your SMTP.Port will be 25. I’ve seen others recommend alternate ports like 3535, but I haven’t had any luck clearing the exception wall with anything other than 25. That is it. I know you’re itching to tinker further, but resist that powerful urge. Don’t pass across account credentials. Don’t define EnableSSL. Don’t set a DeliveryMethod. Don’t define UseDefaultCredentials. Messing around with these values is only likely to trigger a litany of errors (I know because I got tangled in this maze of exceptions). Keep it simple.




using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("mr@soundout.net");
            //Configure the address we are sending the mail from
            MailAddress address = new MailAddress("mr@soundout.net");
            msg.From = address;
            msg.Subject = txtSubject.Text;
            msg.Body = txtName.Text + "n" + txtEmail.Text + "n" + txtMessage.Text;
 
    	    SmtpClient client = new SmtpClient();
    	    client.Host = "relay-hosting.secureserver.net";
    	    client.Port = 25;
 
            //Send the msg
            client.Send(msg);
 
            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";
         }
        catch (Exception ex)
        {
            //If the message failed at some point, let the user know
            lblResult.Text = ex.ToString(); //alt text "Your message failed to send, please try again."
        }
    }
}

Sending to an Email Address Setup in Godaddy Email Hosting Using Another Host

Godaddy hosting not floating your boat? Its not a problem, but it does warp your configuration settings slightly. In this circumstance, you would still use the code block above with a few key exceptions. Since you are operating outside of the Godaddy wall, credentials become an important element so go ahead and pass them across.

//Setup credentials to login to our sender email address ("UserName", "Password")
client.UseDefaultCredentials = false;
NetworkCredential credentials = new NetworkCredential("GodaddyEmail@YourDomain.com", "YourPassword");
client.Credentials = credentials;

Also you’ll need to revise your SMTP server to your default outbound SMTP server so it can push those messages through.

client.Host = "smtpout.secureserver.net";

Sending an Email within Godaddy Hosting Using an Outside Email Address

In the code file presented below, you’ll notice several key differences. We are using Gmail’s servers as our mail relay so we have to pass it the proper values to gain entry. First, set your Gmail address in the To & From message values. As before, the SMTPClient values are the key to our configuration. Gmail uses SSL so we must set EnableSSL to true. The SMTP host for Gmail is “smtp.gmail.com,” and we communicate with it via port 25. Several people online were using 465 to talk to the Gmail SMTP server, but I never got it to work with that port.

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){}
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("YourEmailAddress@gmail.com");
            //Configure the address we are sending the mail from
            MailAddress address = new MailAddress("YourEmailAddress@gmail.com ");
            msg.From = address;
            msg.Subject = txtSubject.Text;
            msg.Body = txtName.Text + "n" + txtEmail.Text + "n" + txtMessage.Text;
 
            //Configure an SmtpClient to send the mail.            
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true; 
            client.Host = "smtp.gmail.com"; 
            client.Port = 25;  
 
	    //Setup credentials to login to our sender email address ("UserName", "Password")
            NetworkCredential credentials = new NetworkCredential("YourEmailAddress@gmail.com", "YourPassword");
	    client.UseDefaultCredentials = true;
            client.Credentials = credentials;
 
            //Send the msg
            client.Send(msg);
 
            //Display some feedback to the user to let them know it was sent
            lblResult.Text = "Your message was sent!";
        }
        catch (Exception ex)
        {
            //If the message failed at some point, let the user know
            lblResult.Text = ex.ToString();
		//"Your message failed to send, please try again."
        }
    }
}

One key element to be aware of is that have to explicitly allow that outside application to have access your Google account. When you are logged into Gmail, you can enable this access via this link.

If you fail to enable this, you’ll encounter an error like this:

The server response was: 5.5.1 Authentication Required.

The lesson to be learned here is everyone comes to the table with slightly different needs. Depending on whether you are using a Godaddy hosted email account or an outside email like Gmail, it can radically alter your SMTP settings. If you pin down your setup going in, you can save yourself considerable headaches and hassles with your ASP.NET email setup.