我想在没有
的情况下运行以下代码NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD").
仅使用Exchange主机(服务器名称)和端口
我得到错误对于此代码:邮箱不可用。服务器响应为:5.7.1客户端无权作为此发件人发送
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient("ExchangeServerName",25);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("bala@OfficeName.com", "From Me");
MailAddress toAddress = new MailAddress("bala@OfficeName.com", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.UseDefaultCredentials = true;
System.Net.NetworkCredential nc = CredentialCache.DefaultNetworkCredentials;
smtpClient.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential("ExchangeServerName", 25, "Basic");
smtpClient.Send(message);
lblText.Text ="Email sent.";
}
catch (Exception ex)
{
lblText.Text = "Coudn't send the message!\n " + ex.Message;
}
}
答案 0 :(得分:4)
我做到了。有关我的代码的更多详细信息,请使用this link。
以下代码与
一起运行良好服务器:Windows Server 2003,Windows Server 2008,Windows Server 2008 R2
IIS:6.0,7.0
.Net Frame Wotk:2.0,3.5,4.0
string sMessage;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//you can provide invalid from address. but to address Should be valil
MailAddress fromAddress = new MailAddress("bala@technospine.com", "BALA");
smtpClient.Host = "Exchange Server Name";
smtpClient.Port = 25;
//smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
message.From = fromAddress;
message.To.Add(bala@technospine.com); //Recipent email
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;
//smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(message);
sMessage = "Email sent.";
}
catch (Exception ex)
{
sMessage = "Coudn't send the message!\n " + ex.Message;
}
lblMailStatus.Text = sMessage;
答案 1 :(得分:1)
您正在尝试使用Exchange发送邮件。为此,发件人(或发送进程)必须对其登录的帐户具有权限,以代表您指定为发件人的用户进行发送。这与通过Exchange的SMTP邮件传输代理(MTA)不同,以便让Exchange接收和路由电子邮件。因此,您知道应该使用SMTP执行此操作,因此您正走在正确的轨道上,但您只是尝试使用错误的API来实现此目的。您想要查看CDOSYS以通过SMTP MTA发送它,而无需进行用户身份验证。在System.Web.Mail.MailMessage上搜索更具体的示例 - 有很多内容。如果Exchange服务器似乎不接受/传递以这种方式传递给它的SMTP消息,您可能只需要稍微打开其配置。在这种情况下,Exchange服务器可能配置了对通过其SMTP MTA接收的邮件路由的严格安全性,并且只需要将要发送这些邮件的计算机的IP地址配置为允许邮件转发。
答案 2 :(得分:0)
尝试NetworkCredential nc = new Net.NetworkCredential(“USERNAME”,“PASSWORD”,“DOMAIN”)