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 = "";
Željko PerićPosted Oct 26, 2021, 3:22 PM
Hello Bhavesh Vankar ,
I think that smtp port should be 587 if not read below reply.
First You need to open Google account.
https://support.google.com/mail/answer/56256?hl=en
Then You need to enable Gmail services for sending and recieving email trough other email platforms, like Your own C# application. There are two services POP and IMAP.
https://support.google.com/mail/answer/7126229?hl=en
https://support.google.com/mail/answer/7104828?hl=en
Since that security issue is important to Google, all external mail clients, like Your application are marked as less secure, and there is need to enable acess to Your Gmail account from less secure applications. If You skip this step, Your applicatin will not be able to connect to Gmail account. This option is something that Google can change any time, and that would prevent Your application to function correctly.
https://support.google.com/accounts/answer/6010255?hl=en-GB
After those four steps are done, You can start developing mail client application in C#.
Here is a peace of code that I have used for sending mail trough my Gmail account :
Do not forget using directive with System.Net.Mail reference:
There are a lot of articles written on this subject.
All the best,
Željko Peric
Sachin SinghPosted Oct 26, 2021, 7:58 AM
follow
https://stackoverflow.com/questions/39783421/attempt-was-made-to-access-a-socket-in-a-way-forbidden-by-its-access-permissions
also
go to your mail and allow less secure apps for the one you use "From" in smtp.
in google type allow less secure apps gmail it will redirect you to the related settings page if you are logged in and there you need to turn on less secure apps.