问题描述
当我尝试通过 ASP MVC3 项目使用 SmtpClient 类发送电子邮件时,我的服务器上出现了奇怪的情况.这是我正在使用的代码.
I'm getting strange occurance on our servers when I am trying to send an email using SmtpClient class via an ASP MVC3 project. This is the code I am using.
try
{
var client = new SmtpClient("MailServer");
client.UseDefaultCredentials = true;
MailMessage message = new MailMessage("me@mydomain.com", "friend@mydomain.com", "Test Message", "Test Body");
client.Send(message);
}
catch (Exception ex)
{
// Do Nothing
}
我已经部署在三个环境中;在 Windows 7(使用 VS 2010 IIS)上它发送电子邮件正常,在 Windows 2003 IIS6 机器上它发送电子邮件正常,最后在 Windows 2008 R2 II7 服务器上我收到以下错误:
I have deployed on three environments; on Windows 7 (using VS 2010 IIS) it sends the email fine, on the Windows 2003 IIS6 machine it sends the email fine, finally on the Windows 2008 R2 II7 server I get the following error:
Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender using username
任何人都可以就可能导致此问题的原因提出建议.我注意到当我查看 User.Identity.Name
时,返回的是一个空字符串.
Can anybody advise on what may be causing this. I have noticed that when I view User.Identity.Name
, this is returning an empty string.
推荐答案
邮件服务器可能不支持发送匿名邮件,需要指定在邮件服务器上注册的凭据.
It is likely that the mail server does not support sending anonymous emails and will require credentials to be specified which are registered on the mail server.
您可以像这样指定凭据:
You can specify the credentials like so:
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("username", "password");
希望这会有所帮助.
这篇关于.NET SMTP 客户端 - 客户端无权作为此发件人发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!