Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

GoDaddy Email Form

0.00/5 (No votes)
12 Mar 2010CPOL2 min read 1  
Introduction...

Introduction


I posted a response on bytes.com back in 2006 about how someone could get their GoDaddy Contact Form to work. On this response, I included a link to my website so the person could contact me, if they had any problems. Since then, I've had maybe 10 people a year write to me asking if they too could have the code. So, I decided to stop a moment and write a quick page on CodeProject to show everyone how this is done. 


Background


GoDaddy offers a great website service, but their Customer Support is really lacking when it comes to finding out how to get their website to doing what you want. Their standard response is something like, "Our Software Development Team is available for contract jobs at our standard rate" ...or something like that. So, GoDaddy website owners are left to figure out how to do this on their own or they must pay "the man" (i.e. GoDaddy). 


Using the Code


To use the code, simply modify two string variables:  domainName and mailTo. 


If your domain is http://www.joeswelding.biz and your email address is JohnDoe@joeswelding.biz, then you just need to modify the included code to look like this:


C#
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<script runat="server">
  public bool Ok;
  static readonly string domainName = "joeswelding.biz";
  static readonly string mailTo = "JohnDoe@" + domainName;
  static readonly string mailer = "mailer@" + domainName;
  string strName;
  string strEmail;
  string strMessage;
  public string FullName { get { return strName; } set { strName = value; } }
  public string Email { get { return strEmail; } set { strEmail = value; } }
  public string Message { get { return strMessage; } set { strMessage = value; } }
  protected void btnSubmit_Click(object sender, EventArgs e) {
    Ok = true;
    strMessage = txtMessage.Text;
    strName = string.Format("{0} {1}", txtFirst.Text, txtLast.Text);
    if (strMessage == "Write your message here.") {
      txtMessage.Text = "You must include a message in this textbox to send.";
      return;
    } else if (strMessage == "You must include a message in this textbox to send.") {
      txtMessage.Text = "Write your message here.";
      return;
    }
    string sEmail = txtEmail.Text;
    string[] split1 = sEmail.Split(new char[] { '@' });
    string fmtSmall = "Unaccepted Email Address. '{0}' is too small: This is not acceptable.";
    if (split1.Length == 2) {
      if ((1 < split1[0].Length) && (1 < split1[1].Length)) {
        string[] split2 = split1[1].Split(new char[] { '.' });
        if (1 < split2.Length) {
          foreach (string s in split2) {
            if (s.Length < 2) {
              txtMessage.Text = string.Format(fmtSmall, s);
              return;
            }
          }
        } else {
          txtMessage.Text = string.Format("{0} is invalid", sEmail);
          return;
        }
      } else {
        txtMessage.Text = string.Format(fmtSmall, split1[0]);
        return;
      }
    } else {
      txtMessage.Text = string.Format("{0} is invalid", sEmail);
      return;
    }
    string formattedMsg, historyMsg, strDate, strFrom;
    strFrom = string.Format("{0} {1} [{2}]", txtFirst.Text, txtLast.Text, txtEmail.Text);
    strEmail = txtEmail.Text;
    strDate = DateTime.Now.ToString();
    formattedMsg = "  <table><tr><td>" +
                   "   <table><tr><td>" +
                   "    <table><tr>" +
                   "     <td>" +
                   "      <font>On <i>" + strDate + 
                       "</i>, <b>" + strFrom + 
                           "</b> sent the following message:</font><br /><br />" +
                   "       <table>" +
                   "        <tr><td><font color=\"Green\">" + strMessage + 
                       "</font></td></tr>" +
                   "        <tr><td> </td></tr>" +
                   "        <tr><td><br><font>End of Message.</font></td></tr>" +
                   "       </table>" +
                   "      </td>" +
                   "    </tr></table>" +
                   "   </td></tr></table>" +
                   "   </td></tr>" +
                   "  </table>";
    historyMsg   = "<tr><td colspan=\"2\">On <i>" + strDate 
                     + "</i>, <b>" + strFrom + "</b> wrote:<br />" +
                   "<font color=\"Green\">" + strMessage + "</font>" +
                   "</font><br/><br/></td></tr>";
    MailAddress joe = new MailAddress(mailTo);
    MailAddress bob = new MailAddress(mailer);
    MailAddress you = new MailAddress(txtEmail.Text);
    System.Net.Mail.MailMessage Email = new System.Net.Mail.MailMessage(bob, joe);
    Email.ReplyTo = you;
    SmtpClient server = new SmtpClient("relay-hosting.secureserver.net");
    string strHtmlBody = "<html><body style=\"background-color:#cccccc\">" + 
        formattedMsg + "</body></html>";
    Email.Subject = "Website Contact";
    Email.Body = strHtmlBody;
    Email.IsBodyHtml = true;
    //Create a new cookie, passing the name into the constructor
    HttpCookie cookie = new HttpCookie(domainName);
    cookie["FormName"] = string.Format("{0} {1}", txtFirst.Text, txtLast.Text);
    cookie["FormEmail"] = txtEmail.Text;
    cookie["FormMessage"] = strMessage;
    //Set the cookie to expire in 1 minute
    DateTime dtNow = DateTime.Now;
    TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
    cookie.Expires = dtNow + tsMinute;
    //Add the cookie
    Response.Cookies.Add(cookie);
    try {
      server.Send(Email);
    } catch (Exception err) {
      lblAlarm.Text = "<b>Unable to send: <font color=\"red\">" + err.Message + 
          "</font></b><br/><br/>";
      Ok = false;
    }
    if (Ok) {
      string[] homepage = { "index.htm", "index.html", "index.php", "Default.aspx" };
      for (int i = 0; i < homepage.Length; i++) {
        if (File.Exists(homepage[i])) {
          Response.Redirect(homepage[i]);
          return;
        }
      }
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Website Contact</title>
  <meta http-equiv="content-TYPE"  content="TEXT/HTML; CHARSET=ISO-8859-1" />
  <meta http-equiv="CACHE-CONTROL" content="PUBLIC" />
  <meta http-equiv="PRAGMA" content="no-cache" />
  <meta name="Author" content="Joe Pool, Joe's Welding" />
  <meta name="City" content="Longview" />
  <meta name="Copyright" content="© 2010 Joe Pool" />
  <meta name="Country" content="USA, United States, United States of America" />
  <meta name="Description" 
  content="Contact Joe Pool, owner of Joe's Welding at http://www.joeswelding.biz/contact" />
  <meta name="Designer" content="Joe Pool, Joe's Welding" />
  <meta name="Distribution" content="Global" />
  <meta name="Geography" content="Longview, TX 75601" />
  <meta name="Language" content="English" />
  <meta name="Publisher" content="Joe Pool, Joe's Welding" />
  <meta name="Revisit-After" content="14 days" />
  <meta name="Robots" content="INDEX,FOLLOW" />
  <meta name="Subject" content="Contact Form for GoDaddy" />
</head>
<body>
  <div>
    <form id="jp2Code1" runat="Server">
      <table>
        <tr>
          <td colspan="2" style="text-align:center;">
            <h2 style="font-family:Arial; color:Red">CONTACT FORM</h2>
            <table style="text-align:center; width:550px"><tbody>
              <tr>
                <td align="right" style="width:225px">First Name:</td>
                <td align="left">
                  <asp:TextBox ID="txtFirst" runat="Server" />
                  <asp:RequiredFieldValidator ID="validFirst" runat="server" 
                      ErrorMessage="Can not be blank" ControlToValidate="txtFirst" />
                </td>
              </tr>
              <tr>
                <td align="right" style="width:225px">Last Name:</td>
                <td align="left">
                  <asp:TextBox ID="txtLast" runat="Server" />
                  <asp:RequiredFieldValidator ID="validLast" runat="Server"
                      ErrorMessage="Can not be blank" ControlToValidate="txtLast" />
                </td>
              </tr>
              <tr>
                <td align="right" style="width:225px">E-Mail:</td>
                <td align="left">
                  <asp:TextBox ID="txtEmail" runat="Server" />
                  <asp:RegularExpressionValidator ID="vaildEmail"
                      runat="Server" ErrorMessage="Invalid Email Syntax"
                      ControlToValidate="txtEmail"
                      ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
                </td>
              </tr>
              <tr>
                <td colspan="2" align="center">Message:<br />
                <asp:TextBox ID="txtMessage" Columns="40" Rows="10"
                    runat="Server" Text="Write your message here." TextMode="MultiLine" />
                    <br />
                <asp:RequiredFieldValidator ID="validMessage" runat="Server" 
                    ErrorMessage="Can not be blank" ControlToValidate="txtMessage" />
              </td>
              </tr>
              <tr>
                <td colspan="2" align="center">
                  <asp:Button ID="btnSubmit" runat="Server" Text="Send"
                      OnClick="btnSubmit_Click" />
                </td>
              </tr>
            </tbody></table>
            <asp:Label ID="lblAlarm" runat="server" ForeColor="Red"></asp:Label>
          </td>
        </tr>
      </table>
    </form>
  </div>
</body>
</html>

Points of Interest


GoDaddy's website will block certain domain names like Yahoo!, HotMail, GMail, etc. Their list is updated and changed by them, and they do not post whenever they make a change.


If someone tries to contact you from one of these blocked domain names, you will never receive the email *unless* you use the format setup above - namely, the person's email address should only be placed in the MailMessage.ReplyTo field! Always use a safe email address as the MailMessage.Sender address (use something like 'webmaster@yourdomain.com'). 


History


Initial version. No revisions yet. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)