Bhavesh Vankar

Bhavesh Vankar

  • 762
  • 1.1k
  • 87.2k

While sending mail generate error

Oct 26 2021 7:34 AM

while sending mail generate error as below.

below is my code...

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]","mailpass");
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;        MailMessage msg = new MailMessage();
msg.Subject = "Hello" + txtFname.Text;
msg.Body = txtQuery.Text;
string toAddress = "";
msg.To.Add(toAddress);
string fromAddress = "[email protected]";
msg.From =  new MailAddress(fromAddress);
try
{
    smtp.Send(msg);
    lblMessage.Text = "Your Mail is sent";
    txtFname.Text = "";
    txtEmailid.Text = "";
    txtQuery.Text = "";
}
catch (Exception ex)
{
    Response.Write("" + ex);
}

below generate error while sending mail.

An attempt was made to access a socket in a way forbidden by its access permissions 
73.120.201.105:25Description: An unhandled exception occurred during the execution of the current 
web request. Please review the stack trace for more information about the error and where it 
originated in the code.Exception Details: System.Net.Sockets.SocketException: An attempt was made to 
access a socket in a way forbidden by its access permissions 73.120.201.105:25Source Error:Line 83:         //try
Line 84:         //{
Line 85:             smtp.Send(msg);
Line 86:             lblMessage.Text = "Your Mail is sent";
Line 87:             txtFname.Text = "";

Answers (2)