C# 套接字、smtp和web异常

C# 套接字、smtp和web异常,c#,email,C#,Email,我从我的应用程序发送电子邮件时出错。我与我的托管公司进行了检查,获得了正确的邮件服务器设置,但仍然收到错误消息。有什么建议吗?堆栈跟踪和下面的代码 [SocketException (0x274d): No connection could be made because the target machine actively refused it 68.178.232.62:25] System.Net.Sockets.Socket.DoConnect(EndPoint endP

我从我的应用程序发送电子邮件时出错。我与我的托管公司进行了检查,获得了正确的邮件服务器设置,但仍然收到错误消息。有什么建议吗?堆栈跟踪和下面的代码

    [SocketException (0x274d): No connection could be made because the target machine actively refused it 68.178.232.62:25]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +251
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +279

[WebException: Unable to connect to the remote server]
   System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +6136880
   System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +314
   System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21
   System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +322
   System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +146
   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +222
   System.Net.Mail.SmtpClient.GetConnection() +50
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1496

[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1829
   Lostpass.EmailUser(User user) +713
   Lostpass.uxUserInfoSubmit_Click(Object sender, EventArgs e) +446
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   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) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

    <mailSettings>
    <smtp deliveryMethod="Network" from="support@<domain>.com">
        <network host="relay-hosting.secureserver.net" port="25" defaultCredentials="true"/>
    </smtp>
</mailSettings>

    void EmailUser(User user)
{
    user.ChangePasswordID = Guid.NewGuid();
    user.Save();
    MailMessage email = new MailMessage();
    email.To.Add(new MailAddress(uxEmail.Text));
    email.IsBodyHtml = true;
    email.From = new MailAddress(Settings.LostPasswordEmailFrom);
    email.Subject = Settings.LostPasswordSubject;
    //email.Subject = "your new password";
    email.Body = EmailTemplateService.HtmlMessageBody(EmailTemplates.MembershipPasswordRecovery, new { Body = Settings.LostPasswordText, BeginRequired = "", EndRequired = "", UserName = user.Name, GUID = user.ChangePasswordID.ToString() });
    //email.Body = "your new password is: password";
    SmtpClient client = new SmtpClient();
    //try
    //{
    client.Send(email);
//}
    //catch
    //{ Exception ex; }

    uxSuccessPH.Visible = true;
    uxQuestionPanel.Visible = false;
    uxUserInfoPanel.Visible = false;
    uxUserNameLabelSuccess.Text = uxEmail.Text;
}
[SocketException(0x274d):无法建立连接,因为目标计算机主动拒绝它68.178.232.62:25]
System.Net.Sockets.Socket.DoConnect(端点端点快照,SocketAddress SocketAddress)+251
System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败、套接字s4、套接字s6、套接字和套接字、IP地址和地址、ConnectSocketState状态、IAsyncResult asyncResult、Int32超时、异常和异常)+279
[WebException:无法连接到远程服务器]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream、对象所有者、布尔异步、IPAddress和address、Socket和abortSocket、Socket和abortSocket6、Int32超时)+6136880
System.Net.PooledStream.Activate(对象所有者对象、布尔异步、Int32超时、GeneralSyncDeleteGate异步回调)+314
System.Net.PooledStream.Activate(对象拥有对象,GeneralAsyncDeleteGate异步回调)+21
System.Net.ConnectionPool.GetConnection(对象拥有对象,GeneralAsyncDeleteGate异步回调,Int32 creationTimeout)+322
System.Net.Mail.SmtpConnection.GetConnection(服务点服务点)+146
System.Net.Mail.SmtpTransport.GetConnection(服务点服务点)+222
System.Net.Mail.SmtpClient.GetConnection()+50
System.Net.Mail.SmtpClient.Send(邮件消息)+1496
[SmtpException:发送邮件失败。]
System.Net.Mail.SmtpClient.Send(MailMessage)+1829
Lostpass.EmailUser(用户用户)+713
Lostpass.uxUserInfoSubmit_单击(对象发送者,事件参数)+446
System.Web.UI.WebControls.Button.OnClick(EventArgs e)+118
System.Web.UI.WebControl.Button.RaisePostBackEvent(String eventArgument)+112
System.Web.UI.WebControl.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)+36
System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+5563
无效EmailUser(用户)
{
user.ChangePasswordID=Guid.NewGuid();
user.Save();
MailMessage email=新的MailMessage();
email.To.Add(新邮件地址(uxEmail.Text));
email.IsBodyHtml=true;
email.From=新邮件地址(Settings.LostPasswordEmailFrom);
email.Subject=Settings.LostPasswordSubject;
//email.Subject=“您的新密码”;
email.Body=EmailTemplateService.HtmlMessageBody(EmailTemplates.MembershipPasswordRecovery,new{Body=Settings.LostPasswordText,BeginRequired=“”,EndRequired=“”,UserName=user.Name,GUID=user.ChangePasswordID.ToString());
//email.Body=“您的新密码是:password”;
SmtpClient=新的SmtpClient();
//试一试
//{
发送(电子邮件);
//}
//抓住
//{Exception ex;}
uxSuccessPH.Visible=真;
uxQuestionPanel.Visible=假;
uxUserInfoPanel.Visible=false;
uxUserNameLabelSuccess.Text=uxEmail.Text;
}

您确定
defaultCredentials
正确吗?我怀疑这会发送您的Windows登录凭据,这可能不是您的ISP所需要的。我认为您需要在
SmtpClient
实例上设置属性。比如:

// set your SMTP server login credentials
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential("username", "password");
client.Credentials = credentials;

您是否看到此线程:?检查是否启用了代理。这可能是因为代理。如果没有代理,请确保选择正确的smtp端口并在代码中启用ssl。谢谢,Jaroslaw。我的机器声称没有telnet。我的ISP让我向他们发送跟踪路由。我还要求他们确保远程机器的中继没有被禁用。@Bravo11:我上面描述的中继问题是否与您提到的代理问题类似?我没有太多与代理打交道的知识或经验。我还与我的ISP就正确的SMTP端口进行了双重检查。是的,如果您在代理服务器后面,这是一个经常出现的问题,但您知道许多错误消息是相似的,但实际问题是不同的,因此很难说出实际原因,除非我亲自调试。希望你能理解。谢谢你,吉姆。由于我仍然收到“拒绝”错误,我将首先尝试解决上面提到的套接字和Web异常,然后添加更多有关SMTP问题的内容。以下是我的进一步发现:我可以在ISP的远程服务器上发送运行项目的电子邮件,但不能在本地计算机上发送(错误消息来自本地运行项目).奇怪,因为根据我的经验,大多数项目更喜欢本地计算机而不是远程计算机。我关闭了Windows防火墙和卡巴斯基防火墙,但这些操作都不起作用。根据这些新信息,有什么进一步的建议吗?@muybn:一些服务提供商阻止了端口25。我知道mind(可能仍然)阻止了,因此我无法在家中执行SMTP。您可以尝试使用telnet连接到SMTP服务器。如果您甚至无法获得连接,则您知道问题很可能不是由您的程序引起的。谢谢,Jim。我尝试过telnet几次,以获取其他建议,但发现一篇文章指出它在Win7中没有被d激活efault及其安装方法:程序和功能/打开或关闭Windows功能/Telnet客户端(适用于遇到相同障碍的用户)。是否还有其他更适合使用的端口?我在命令提示符下使用了以下命令(以管理员身份运行)所有都失败了。它们中是否有一个看起来像正确的格式?telnet relay-hosting.secureserver.net telnet 68.178.232.62:25 telnet 68.178.232。62@muybn:重点是查看是否可以连接到计算机上的任何SMTP服务器