Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using smtp google's server to send a email through my contact form..In local server it works fine but in web server it shows this error.First ,I've used port number 587 which gives error "Security Exception" and error was on the line using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) , but I've changed port to 25 and it gives this new error on the line .

smtp.Send(mail);

Description:

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 74.125.206.108:25

My code is:

protected void send_Click(object sender, EventArgs e)
    string smtpAddress = "smtp.gmail.com";
    int portNumber = 25;
    bool enableSSL = true;
    string emailfrom = "isl@gmail.com";
    string password = "******";
    string subject = "Contact Form Data";
    string emailto = "isl@gmail.com";
    string name = n.Value;       
    string useremail = em.Value;
    string phone = tel.Value;
    string dept = dep.Value;
    string dest = des.Value;
    string adu = ad.Value;
    string msg = mes.Value;
    string body = "Name: " + name + " ;" + " Email: " + useremail + " ;" + "Telephone: " + phone + " ;" + " Departure Place: " + dept + " ;" + "Destination Place: " + dest + " ;" + " Adults: " + adu + " ;" + " ;" + "Children: " + chil + " ;" + "Message: " + msg + " ;";
    using (MailMessage mail = new MailMessage())
        mail.From = new MailAddress(emailfrom);
        mail.To.Add(emailto);
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
        using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
            smtp.Credentials = new NetworkCredential(emailfrom, password);
            smtp.EnableSsl = enableSSL;
            smtp.Send(mail);

Stack Trace

[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions 74.125.206.108:25] System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +208 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +464

[WebException: Unable to connect to the remote server] System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6662436 System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +307 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +19 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +324 System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141 System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +170 System.Net.Mail.SmtpClient.GetConnection() +44 System.Net.Mail.SmtpClient.Send(MailMessage message) +1554

[SmtpException: Failure sending mail.] System.Net.Mail.SmtpClient.Send(MailMessage message) +1906 Contact.send_Click(Object sender, EventArgs e) in \smb-whst-www02\whst_www02$\ff8b1b\user\medviewair.uk\web\Contact.aspx.cs:51 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628462 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

If it works locally but not on the web server, check the firewall on the web server to make sure outbound connections on your chosen port are allowed. Try to connect from the web server to your smtp server using telnet over your chosen port. Also, have you tried connecting using port 465? Or try port 25 with smtp.EnableSsl = false; – knobcreekman Sep 30, 2016 at 4:27 Possible duplicate of An attempt was made to access a socket in a way forbidden by its access permissions – Michael Freidgeim Mar 31, 2017 at 1:58

After Windows Update, some ports are reserved by Windows and applications cannot bind to these ports. please check this command

netsh interface ipv4 show excludedportrange protocol=tcp

This was my problem (other usecase however). Now up for a solution to free the port for me... – Geert Aug 23, 2021 at 7:58 see superuser.com/questions/1579346/… netsh int ipv4 add excludedportrange protocol=tcp startport=9999(your port) numberofports=1 – Geert Aug 23, 2021 at 9:01

Kindly check that your web server is properly configured and it is up and running.

In case, if your application is throwing an exception then follow the below steps

  • Go to "Start" --> "Control Panel"

  • Click on "Windows Firewall"

  • Inside Windows Firewall, click on "Allow a program or feature through Windows Firewall"

  • Now inside of Allow Programs, Click on "Change Settings" button. Once you clicked on Change Settings button, the "Allow another program..." button gets enabled.

  • Click on "Allow another program..." button , a new dialog box will be opened. Choose the programs or application for which you are getting the socket exception and click on "Add"

  • Click OK, and restart your machine.

  • Try to run your application (which earlier had an exception) with administrative rights.

    I hope this helps.

    Peace,

    Sunny Makode

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •