Send Email from GoDaddy Asp.Net Application

In my previous article we looked at how to use Google account and SMTP server to programatically send mail to an email address. We can use the same code with minor changes for GoDaddy application which is using shared hosting. We will develop a “Contact us” page here using SMTP server from GoDaddy.

The first step is to create a web service

using System;
using System.Configuration;
using System.Net.Mail;
using System.Web.Services;

namespace GoDaddy.Email
    /// <summary>
    /// Summary description for Mail
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    public class Mail : System.Web.Services.WebService
        public string SendEmail(string email, string name, string body)
                var toEmailAddress = ConfigurationManager.AppSettings["ToEmailAddress"].ToString();
                var smtpHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
                var smtpPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();

                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress(email, name);
                mailMessage.Subject = "Contact Us";
                mailMessage.Body = body;
                SmtpClient smtpClient = new SmtpClient(smtpHost, Convert.ToInt32(smtpPort));

                return "Success";
            catch (Exception ex)
                return ex.Message;

The app settings are as following

    <add key="ToEmailAddress" value=""/>
    <add key="SMTPHost" value=""/>
    <add key="SMTPPort" value="25"/>

The HTML page code for the contact us page is as following

<!DOCTYPE html>
    <title>Send Email</title>
	<meta charset="utf-8" />
    <link rel="stylesheet" href="css/style.css" />
    <script src="js/jquery.min.js"></script>
    <div id="contact-area">
            <input id="email" type="email" maxlength="50" />
            <input id="name" type="text" maxlength="50" />
            <textarea id="message" type="text" rows="5" cols="50" maxlength="1000"></textarea>
            <input id="btnSend" type="button" name="Send" value="Send" class="submit-button"/>
        <div id="error" class="error"></div>
        <div id="success" class="success"></div>

Now add the java script code as following in our HTML

    <script type="text/javascript">
        function isValidEmail(email) {
            var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
            return pattern.test(email);

        function validate() {
            var isValid = false;
            var email = $('#email').val();
            if (isValidEmail(email)) {
                var name = $('#name').val();
                var message = $('#message').val();
                if (name === '' || message === '') {
                    $('#error').html("Name or Message cannot be blank");
                } else {
                    isValid = true;
            } else {
                $('#error').html("Invalid Email");
            return isValid;

        function sendMail() {
            $('#btnSend').prop('disabled', true);
            if (validate()) {
                var email = $('#email').val();
                var name = $('#name').val();
                var body = $('#message').val();

                var param = "{'email':" + JSON.stringify(email) + ", 'name':" + JSON.stringify(name) + ", 'body':" + JSON.stringify(body) + "}"; //JSON.stringify(mail);

                    type: "POST",
                    url: "/Mail.asmx/SendEmail",
                    contentType: "application/json; charset=utf-8",
                    data: param,
                    dataType: "json",
                    success: function (response) {
                        var data = response.d;
                        if (data != 'Success') {
                        } else {
                            $('#success').html("Mail sent");
                        $('#btnSend').prop('disabled', false);
                    failure: function (response) {
                        $('#btnSend').prop('disabled', false);
            } else {
                $('#btnSend').prop('disabled', false);

        document.getElementById("btnSend").addEventListener("click", sendMail);

The complete code can be downloaded from here.

Be aware that if you are setting email address entered by the user in the “from” address while sending the email, it may end up in SPAM folder of your mailbox. Alternatively you can use “from” and “to”addresses from your application domain itself and append user’s email address in the mail body.

Leave A Comment

Your email address will not be published.