Unable to send email: An attempt was made to access a socket in a way forbidden...

Discussion in 'Email' started by Alex Barberi, Aug 9, 2014.

  1. This just started happening and I am not sure why... When I try to send an email via one of my Winhost email addresses, I get the error message, "An attempt was made to access a socket in a way forbidden by its access permissions [Server IP : Port #]."

    Why would this have started happening all the sudden?

    Code:
            private string smtpHost = "mail.myhost.com";
            private int smtpPort = 25;
            public const string NoReplyAddress = "[email protected]";
            public const string NoReplyUsername = "[email protected]";
            public const string NoReplyPassword = "MyPassword1";
            private bool enableSsl = false;
    
            private string errorMsg = "";
    
            public void sendEmail(string addrTo, string subject, string msg, string addrFrom = NoReplyAddress,
                string username = NoReplyUsername, string password = NoReplyPassword)
            {
                try
                {
                    errorMsg = "";
    
                    // Setup the mail message
                    MailMessage mm = new MailMessage(addrFrom, addrTo);
                    mm.Subject = subject;
                    mm.Body = msg;
                    mm.IsBodyHtml = true;
                    mm.BodyEncoding = Encoding.Default;
    
                    // Setup SMTP client
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = smtpHost;
                    smtp.Port = smtpPort;
                    smtp.EnableSsl = enableSsl;
    
                    // Setup credentials
                    NetworkCredential nc = new NetworkCredential();
                    nc.UserName = username;
                    nc.Password = password;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = nc;
    
                    // Send the mail message
                    smtp.Send(mm);
    
                    // trigger Sent event
                    //Sent(this, EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
    
                    // trigger Failed event
                    //Failed(this, EventArgs.Empty);
                }
            }
        }
     
    Last edited by a moderator: Oct 14, 2015
  2. Nevermind. Problem solved.

    I started using port 587 instead of 25; everything works now.
     
  3. ComputerMan

    ComputerMan Winhost Staff

    Thank you for reporting what worked for you.
     

Share This Page