当 Windows 窗体应用程序使用相同的 SMTP 服务器时,为什么 MailMessage 会从 Windows 窗体应用程序发送到任何地址,而不是从 ASP.NET 应用程序发送到任何地址?
当我从 ASP.NET Web 应用程序通过 SMTP 服务器发送电子邮件时,遇到了一个神秘的(对我来说)错误。我收到著名的错误“无法中继 [email protected]”。对我来说很神秘的是,当我将发送电子邮件的完全相同的代码剪切并粘贴到常用的 .NET Windows 窗体应用程序中,并使用此应用程序发送电子邮件时,一切都运行良好。这让我认为问题可能在于 ASP.NET 应用程序作为网络服务运行,而 Windows 窗体应用程序在域用户帐户上运行,但事实证明我有另一个 ASP.NET 应用程序通过同一帐户发送电子邮件SMTP 服务器在同一 IIS 的网络服务器下运行,并且此应用程序不会遇到此问题。
我进一步尝试通过 SMTP 服务器手动发送电子邮件,方法是在端口 25 上 telnet SMTP 服务器并手动运行 SMTP 协议,一切正常。 SMTP 服务器未配置任何类型的身份验证或 SSL。
另一个神秘的事实是,ASP.NET 应用程序可以将电子邮件从同一域内的地址发送到同一域内的电子邮件地址,但不能发送到域外的任何地址。但使用完全相同的代码的 Windows 窗体应用程序可以将电子邮件从任何地址发送到域内和域外的任何地址。
总结一下:
- ASP.NET 应用程序可以发送 来自以下地址的电子邮件 域到域内的地址, 但不包括地址之外的地址 领域。
- 正在运行的 Windows 窗体应用程序 在同一台计算机上使用相同的代码 可以从任何地址发送电子邮件至 任何地址。
- 另一个 ASP.NET 应用程序 相同的IIS运行在相同的 帐户(NETWORK SERVICE)可以发送 使用同一 SMTP 服务器发送电子邮件 从任何地址到任何地址。
- 没有配置身份验证 在 SMTP 服务器上。
- ASP.NET 应用程序和 Windows 窗体应用程序利用 System.Net.Mail.SmtpClient 类 发送一个 系统.Net.Mail.MailMessage。
发送电子邮件消息的代码是:
private void button1_Click(object sender, EventArgs e)
{
MailMessage mesasge = new MailMessage(txtFrom.Text, txtTo.Text, "Test mail", txtBody.Text);
SmtpClient client = new SmtpClient();
if (!(string.IsNullOrEmpty(txtUserName.Text))) //Is false since txtUserName.Text is empty
client.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
client.EnableSsl = false;
client.Host = txtServer.Text;
client.Port = 25;
try
{
client.Send(mesasge);
}
catch (Exception ex)
{
txtResponse.Text = ex.Message;
}
}
据我了解,这应该是配置问题而不是编码问题。有谁知道问题可能是什么,以及如何解决?谢谢!
I'm experiencing a mysterious (to me) error when sending e-mails through a SMTP-server from an ASP.NET web application. I get the famous error "unable to relay for [email protected]". What's mysterious to me is that when I cut and paste the exact same code that sends the e-mail into an usual .NET Windows Forms application, send the e-mail with this application, it all works just fine. This made me think that perhaps the problem is that the ASP.NET application runs as NETWORK SERVICE while the Windows Forms application runs on a domain user account, but it turns out that I have another ASP.NET application sending e-mail through the same SMTP-server running under NETWORK SERVER at the same IIS, and this application does not experience this problem.
I've further tried to send e-mails through the SMTP-server manually by telnet the SMTP-server on port 25 and running the SMTP-protocol manually, and it all works fine. The SMTP-server is not configured with any kind of authentication or SSL.
Another mysterious fact is that the ASP.NET application can send e-mails from an address within the same domain to an e-mail address within the same domain, but not to any address outside of the domain. But the Windows Forms application, that uses the exact same code, can send e-mails from any address to any address both within AND outside of the domain.
So to summarize:
- The ASP.NET application can send
e-mails from addresses within the
domain to addresses within the domain,
but not to addresses outside of the
domain. - A Windows Forms application running
the same code on the same computer
can send e-mails from ANY address to
ANY address. - Another ASP.NET application on the
same IIS running under the same
account (NETWORK SERVICE) can send
e-mails using the same SMTP-server
from ANY address to ANY address. - There is no authentication configured
on the SMTP-Server. - Both the ASP.NET application and the
Windows Forms application utilizes
the System.Net.Mail.SmtpClient class
to send a
System.Net.Mail.MailMessage.
The code that sends the e-mail massage is:
private void button1_Click(object sender, EventArgs e)
{
MailMessage mesasge = new MailMessage(txtFrom.Text, txtTo.Text, "Test mail", txtBody.Text);
SmtpClient client = new SmtpClient();
if (!(string.IsNullOrEmpty(txtUserName.Text))) //Is false since txtUserName.Text is empty
client.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
client.EnableSsl = false;
client.Host = txtServer.Text;
client.Port = 25;
try
{
client.Send(mesasge);
}
catch (Exception ex)
{
txtResponse.Text = ex.Message;
}
}
As far as I can understand, this should be a matter of configuration rather than coding issues. Do anyone have an idea what the problem might be, and perhaps how this could be solved? Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来绝对像是身份验证问题,或者您不小心使用了 2 个不同的 SMTP 服务器。
我建议您在两个应用程序中启用日志记录并查看日志。当您查看日志时,您应该能够看到差异,并希望将其追溯到您的代码。
要启用 System.Net.Mail 中的日志记录,您需要向 .config 文件中添加一些条目。
以下是包含更多信息的链接:
http://systemnetmail.com/faq/4.10.aspx
This most definitely sounds like an authentication issue or, you are accidently using 2 different SMTP servers.
I recommend you enable logging in both applications, and view the log. When you view the log, you should be able to see the differences, and hopefully trace it back to your code.
To enalbe logging in System.Net.Mail, you need to add some entries to your .config file.
Here is a link with more info:
http://systemnetmail.com/faq/4.10.aspx
web.config 中是否有 impersonate = true ?
这样做:
据我所知,需要将 web.config 设置为允许模拟。
尝试将其添加到您的 web.config (如果您没有它)
Do you have impersonate = true in the web.config?
Doing this:
As far as I know requires that the web.config be set to allow impersonate.
Try adding this to you web.config (if you dont have it there)