尝试以访问权限禁止的方式访问套接字

Uma*_*mad 7 c# sockets asp.net email smtp

我使用SMTP谷歌的服务器,通过我接触form..In本地服务器发送电子邮件,它工作正常,但在Web服务器时,它显示了这个error.First,我使用的端口号587,其提供了错误“安全例外”的错误在线using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)),但我已将端口更改为25,它在线上给出了这个新错误。

smtp.Send(mail);
Run Code Online (Sandbox Code Playgroud)

说明

执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详情

System.Net.Sockets.SocketException:尝试以访问权限禁止的方式访问套接字 74.125.206.108:25

我的代码是:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

[SocketException (0x271d): 试图以访问权限禁止的方式访问套接字 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: 无法连接到远程服务器] 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.Smtp .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:发送邮件失败。] 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

小智 14

Windows 更新后,某些端口被 Windows 保留,应用程序无法绑定到这些端口。请检查此命令

netsh 接口 ipv4 显示excludedportrange 协议=tcp


Sun*_*nny 4

请检查您的网络服务器是否已正确配置并且已启动并正在运行。

如果您的应用程序抛出异常,请按照以下步骤操作

  1. 转到“开始”-->“控制面板”

  2. 单击“Windows 防火墙” 在此输入图像描述

  3. 在 Windows 防火墙内,单击“允许程序或功能通过 Windows 防火墙” 在此输入图像描述

  4. 现在在“允许程序”内,单击“更改设置”按钮。单击“更改设置”按钮后,“允许另一个程序...”按钮将启用。 在此输入图像描述

  5. 单击“允许另一个程序...”按钮,将打开一个新对话框。选择您收到套接字异常的程序或应用程序,然后单击“添加” 在此输入图像描述

  6. 单击“确定”,然后重新启动计算机。

  7. 尝试使用管理权限运行您的应用程序(之前有例外)。

我希望这有帮助。

和平,

桑尼·马科德