Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi everyone,
I wrote the code for sending email but it is showing the following error.Please help.
An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:25

My code is
C#
protected void SendMail()
   {
       // Gmail Address from where you send the mail
       var fromAddress = "rajeshkumar.pani@gmail.com";
       // any address where the email will be sending
       var toAddress = "dsssweta@gmail.com";
       //Password of your gmail address
       const string fromPassword = "1234";
       // Passing the values and make a email formate to display
       string subject = "Hi";
       string body = "From: ";
      
       // smtp settings
       var smtp = new System.Net.Mail.SmtpClient();
       {
           smtp.Host = "localhost";
           smtp.Port = 25;
           smtp.EnableSsl = true;
           smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
           smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
          // smtp.Timeout = 20000;
       }
       // Passing values to smtp object
       smtp.Send(fromAddress, toAddress, subject, body);
   }
Posted
Updated 25-Jun-14 20:36pm
v2

I doubt that you are running a SMTP server on localhost. If you knew about this server, you would probably mention it. You need to have a "real" functional SMTP server and the account on it, with credentials allowing you to use it.

—SA
 
Share this answer
 
Comments
Member 10578683 26-Jun-14 2:32am    
I am not understanding what u r saying . Please kindly mention the details why this error is coming and how to write code for this in Detail. If any link you have please share.
Please check whether SMTP server is installed on your local machine or not? If it is installed then check the port is valid or not.
 
Share this answer
 
try smtp.EnableSsl = false;
because for some smtps ssl will not be enabled..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900