I am implementing Email sending option in my website using below code:
HTML:
<table width="600px" align="center">
<tr>
<td> To </td>
<td> <asp:TextBox ID="txtToMail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> Subject </td>
<td> <asp:TextBox ID="txtSubject" runat="server" Width="400"></asp:TextBox>
</td>
</tr>
<tr>
<td> Message </td>
<td> <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Width="400px" Height="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Send" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" ></asp:Label>
</td>
</tr>
</table>
C#:
using System.Net.Mail;
using System.Net.Security;
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage(); // creating object
mail.From = new MailAddress("MymailId@gmail.com"); // deciding from which mail add to be send (it can be define at run time by taking value from form through txtbox)
mail.To.Add(txtToMail.Text); // adding mail id to which mail is to be send
mail.Subject = txtSubject.Text; // deciding subject...
mail.Body = txtMessage.Text; // taking mail from form
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); //setting mail client...it is different for every one
smtp.Credentials = new System.Net.NetworkCredential("MymailId@gmail.com", "Password");
smtp.EnableSsl = true; //setting complete mail
smtp.Send(mail); //sending mail.....
lblMsg.Text = "Email has been successfully sent..!!";
}
but it is giving me error:
System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.79.109:587
Line:
smtp.Send(mail);
I also tried pinging above mentioned IP i.e., 173.194.79.109.587, and is pingable through my network.
I am unable to understand the cause of error.
If any other code is there for e-mail option please do share.
Please help.