Wednesday 3 April 2013

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


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:
SMTP) Server: smtp.gmail.com
Port: 465/ 587
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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<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="xyz@x.x" 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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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:

Post a Comment