Sending Mail in Go-daddy Shared Hosting using Google Apps Mail

Most of the time we are facing problem when trying to use feedback form in the website which is hosted in GoDaddy servers in shared hosting and we are using Google Apps as mail for the domain.

When we are developing the application in local its working fine but when we deployed in production server it does not send any mail. This issue occures because GoDaddy does not allow external host server for sending mail.

For Google Apps normally we are using Gmails SMTP settings as:

   1) SMTP Server: smtp.gmail.com

   2) Port: 465/ 587

   3) Enable SLL = True

but these credentials are not working in GoDaddy Shared hosting.

Below is the Code i am using for sending mail in GoDaddy Shared Servers.

HTML Markup:

<div style="margin:0 auto; width:900px; padding-left:50px; padding-top:220px; background-image:url

(images/bg.gif); text-align:center; font-family:Verdana; font-size:10pt; color:#ffffff;">
      
      <table>
      
        <tr>
            <td colspan="3" align="left"><h2>Mail Details:</h2></td>
        </tr>
      
        <tr>
            <td align="left" width="150px"><strong>Mail From</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left">
                <asp:TextBox ID="txt_mailfrom" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
      
        <tr>
            <td align="left" width="150px"><strong>Mail To</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left">
                <asp:TextBox ID="txt_mailto" runat="server" Width="200px"></asp:TextBox>
            </td>
        </tr>
      
        <tr>
            <td align="left" width="150px" colspan="3"
                style="font-weight: 700; font-size: medium">
                <h2>
                    Feedback:</h2>
            </td>
        </tr>
      
        <tr>
            <td align="left" width="150px"><strong>Name</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_name" runat="server" Width="200px" 

Height="22px"></asp:TextBox></td>
        </tr>
        <tr>
            <td align="left" width="150px"><strong>Father's Name</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_father" runat="server" Width="200px" 

Height="22px"></asp:TextBox></td>
        </tr>
          
        <tr>
            <td align="left" width="150px"><strong>Contact Number</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_contact" runat="server" Width="200px" 

Height="22px"></asp:TextBox></td>
        </tr>
          
        <tr>
            <td align="left" width="150px"><strong>Email ID</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_mail" runat="server" Width="200px" 

Height="22px"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                    ErrorMessage="[email protected]" ControlToValidate="txt_mail"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w

+)*"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="left" width="150px"><strong>Address</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_address" runat="server" Width="200px"
                    Height="35px" TextMode="MultiLine"></asp:TextBox></td>
        </tr>
        <tr>
            <td align="left" width="150px"><strong>Message</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:TextBox ID="txt_msg" runat="server" Width="200px"
                    Height="35px" TextMode="MultiLine"></asp:TextBox></td>
        </tr>
          
        <tr>
            <td align="left" width="150px"><strong>Upload</strong></td>
            <td align="center" width="20px"><strong>:</strong></td>
            <td align="left"><asp:FileUpload ID="FileUpload1" runat="server" Width="200px" /></td>
        </tr>
          
        <tr>
            <td align="left" width="150px"><strong></strong></td>
            <td align="center" width="20px"><strong></strong></td>
            <td align="left">
                <asp:Button ID="btn_send" runat="server" Text="Submit" BackColor="#CC0000"
                    Height="25px" Width="90px" Font-Bold="True" ForeColor="White"
                    onclick="btn_send_Click" />  
                    <asp:Button ID="btnclear" runat="server" Text="Clear" BackColor="#CC0000"
                    Height="25px" Width="90px" Font-Bold="True" ForeColor="White"
                    onclick="btnclear_Click" />
            </td>
        </tr>
        <tr><td colspan="3" align="center">
            <asp:Label ID="lbl_msg" runat="server" ForeColor="#CC0000"></asp:Label>
            </td></tr>      
    </table>  
    </div>

Code Block should be as:

protected void Page_Load(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        if (!IsPostBack)
        {
            cleartext();
        }
  
    }
    protected void btn_send_Click(object sender, EventArgs e)
    {
        string from = txt_mailfrom.Text;
        string to = txt_mailto.Text;
        string body = "<b>Name : </b>" + txt_name.Text + "<br/><b>Father Name : </b>" + 

txt_father.Text + "<br><b>Contact No. : </b>" + txt_contact.Text + "<br/><b>Email : 

</b>" + txt_mail.Text + "<br/><b>Address : </b>" + txt_address.Text + "<br/><b>Message : 

</b>" + txt_msg.Text;
  
        string message = sendMail(from, to, body, "Feedback", FileUpload1);
        if (message == "")
        {
            lbl_msg.Text = "Message Send Successfully...";
        }
        else
        {
            lbl_msg.Text = message;
        }
          
        cleartext();
    }
    protected void btnclear_Click(object sender, EventArgs e)
    {
        cleartext();
    }
  
    private void cleartext()
    {
        txt_address.Text = string.Empty;
        txt_contact.Text = string.Empty;
        txt_father.Text = string.Empty;
        txt_mail.Text = string.Empty;
        txt_name.Text = string.Empty;
        txt_msg.Text = string.Empty;
        txt_mailfrom.Text = string.Empty;
        txt_mailto.Text = string.Empty;
    }
  
    public string sendMail(string from, string sendTo, string body, string subject, FileUpload file)
    {
        string message = "";
        try
        {
            const string SERVER = "relay-hosting.secureserver.net";
            using (System.Net.Mail.MailMessage mess = new System.Net.Mail.MailMessage())
            {
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                  
                mess.To.Add(sendTo);
                if (file.HasFile)
                    mess.Attachments.Add(new Attachment(file.PostedFile.InputStream, file.FileName));
                mess.Subject = subject;
                mess.Body = body;
                mess.IsBodyHtml = true;
                mess.From = new System.Net.Mail.MailAddress(from);
                sc.Host = SERVER;               
                sc.Send(mess);
           }
        }
        catch (Exception ex)
        {
            message = ex.ToString();
        }
        return message;
    }

Don't forget to add System.Net.MailNamespace

Note: Please make sure upload the sample files in production server then only it will work. For development server it will throw error.

Sample Attached: Sending Mail.rar (2.38 kb)


No Comments

Add a Comment