无法使用 smtpclient 通过 localhost iis 站点发送邮件
我有一个在本地 iis 网站上运行的 asp.net mvc 应用程序,该应用程序无法使用 System.Net.Mail 中的 SmtpClient 发送邮件。 我正在尝试向我的 hotmail 地址发送邮件,但出现套接字异常。 知道可能是什么问题吗?
using(var message = new MailMessage())
{
message.From = new MailAddress(AdminEmail);
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = false;
var mailClient = new SmtpClient();
mailClient.Send(message);
}
这是例外:
{System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)}
我认为默认应用程序池无权访问 intepub\wwwroot。 以下是我在尝试测试连接时遇到的错误:
服务器配置为使用内置帐户的直通身份验证来访问指定的物理路径。 但是,IIS 管理器无法验证内置帐户是否具有访问权限。 确保应用程序池标识具有对物理路径的读取访问权限。 如果此服务器已加入域,并且应用程序池标识为 NetworkService 或 LocalSystem,请验证 \$ 是否具有对物理路径的读取访问权限。 然后再次测试这些设置。
I have an asp.net mvc app running on a local iis website that is failing to send mail using SmtpClient from System.Net.Mail. I am trying to send a mail to my hotmail address and I get a socket exception. Any idea what the problem might be?
using(var message = new MailMessage())
{
message.From = new MailAddress(AdminEmail);
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = false;
var mailClient = new SmtpClient();
mailClient.Send(message);
}
Here is the exception:
{System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)}
I think the default app pool does not have access to intepub\wwwroot. Here is the error that I get when trying to test the connection:
The server is configured to use pass-through authentication with a built-in account to access the specified physical path. However, IIS Manager cannot verify whether the built-in account has access. Make sure that the application pool identity has Read access to the physical path. If this server is joined to a domain, and the application pool identity is NetworkService or LocalSystem, verify that \$ has Read access to the physical path. Then test these settings again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您对我上面的评论的回答,Joe,您的本地计算机上没有启用 SMTP。 Vista 不附带 SMTP。
因此,您要么必须安装将在 Vista 上运行的第 3 方 SMTP 应用程序,要么使用其他应用程序进行发送,在这种情况下,您的 Hotmail 帐户可能允许您通过它发送邮件。 我不使用 Hotmail,所以不知道它是否会使用,但它应该是类似于 smtp.hotmail.com 和您的凭据。 我的主帐户是 gmail 帐户,因此我可以通过 smtp.gmail.com 使用它,当然还有我的凭据。
Based on your answers to my comments above Joe, you don't have SMTP enabled on your local machine. Vista does not come with SMTP.
As such, you'll either have to install a 3rd party SMTP app that will run on Vista, or use another app to send via, in this case, your Hotmail account may allow you to send outgoing via it. I don't use Hotmail so don't know if it will or not but it should be something like smtp.hotmail.com and your credentials. My primary account is a gmail account, so I can use it via smtp.gmail.com and of course, my credentials.
对于本地计算机上的开发,您还可以使用放置文件夹:
http://www.codersbarn.com/post/2008/11/30/ASPNET-Contact-Form.aspx
For development on your local machine, you can also use a drop folder:
http://www.codersbarn.com/post/2008/11/30/ASPNET-Contact-Form.aspx
与其尝试在本地设置 SMTP,为什么不将 SMTP 连接配置为直接通过 hotmail 发送,只需将配置元素添加到 web.config 即可启用所有功能。
编辑
我认为这篇文章我前段时间写的可能对你有帮助。
Rather than trying to setup SMTP locally, why don't you just configure the SMTP connection to send directly via hotmail, just add the configuration elements to the web.config to enable everything.
Edit
I think this article that I wrote a while back might help you out.
尝试显式设置 SmtpClient 的主机。
例如:
SmtpClient mailClient = new SmtpClient();
mailClient.Host = "127.0.0.1";
mailClient.Send(消息);
Try explicitly setting the host of the SmtpClient.
Ex:
SmtpClient mailClient = new SmtpClient();
mailClient.Host = "127.0.0.1";
mailClient.Send(message);