向 Exchange 2007 进行身份验证
我有一个简单的邮件实用程序,应该通过 Exchange 2007 服务器(安装在 Windows Server 2008 R2 64 位上)发送电子邮件,但它不起作用,在命令行中给出以下错误消息:“邮箱不可用”服务器响应为:5.7.1 无法中继”。 我被告知我需要向服务器进行身份验证,但显然我做得不正确。有什么建议吗? 我的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace SendMail
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtpClient = new SmtpClient("x.x.x.x", 25);
NetworkCredential basicCredential = new NetworkCredential("username", "password", "domain");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("[email protected]");
smtpClient.Host = "x.x.x.x";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "test message";
message.Body = "test message";
message.To.Add("[email protected]");
try
{
smtpClient.Send(message);
Console.WriteLine("Message sent successfully");
}
catch (Exception ex)
{
//Error, could not send the message
Console.WriteLine(ex.Message);
}
}
}
}
I've got a simple mail utility that is supposed to send an email through an Exchange 2007 server (which is installed on Windows Server 2008 R2 64bit) and it won't work, giving the following error message at the commandline: "Mailbox unavailable. The server response was: 5.7.1 Unable to relay".
I've been told I need to authenticate to the server but evidently I'm not doing it correctly. Any suggestions?
My code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace SendMail
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtpClient = new SmtpClient("x.x.x.x", 25);
NetworkCredential basicCredential = new NetworkCredential("username", "password", "domain");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("[email protected]");
smtpClient.Host = "x.x.x.x";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "test message";
message.Body = "test message";
message.To.Add("[email protected]");
try
{
smtpClient.Send(message);
Console.WriteLine("Message sent successfully");
}
catch (Exception ex)
{
//Error, could not send the message
Console.WriteLine(ex.Message);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我测试过,你的代码工作正常。请检查您使用的帐户是否在 Exchange 中正确配置,或尝试使用其他帐户。
I tested and your code is working fine. Please check that the account you using is properly configured in Exchange, or try using another account.
System.Net.Mail.SmtpClient 将无法轻松地通过 Exchange 进行身份验证,除非您创建一个连接器以允许在 Exchange 服务器上进行开放中继。您可以将其限制为仅发送服务器的 IP。然而,对于通过 Exchange 发送邮件,您会发现以下 EWS dll 是更好的通用解决方案。它与您现在的代码一样简单,但更适合 Exchange。
您可以尝试使用以下 dll 的 Microsoft Exchange Web 服务。
https://www.microsoft.com/en-us/download /details.aspx?id=42951
该对象非常简单,与使用 System.Net.Mail 相当。
这是我根据 MSDN 示例修改的示例。
EWS 网址可能有点令人困惑。它还取决于您是否有权访问 Exchange 服务器。这是一些帮助。
https://msdn.microsoft.com/ en-us/library/office/dn509511(v=exchg.150).aspx
如果您无权访问 Exchange 服务器,这会很有帮助。
将最后一部分更改为 Exchange.asmx,而不是 Services.wsdl
The System.Net.Mail.SmtpClient will not easily authenticate with Exchange unless you make a connector to allow open relay on the Exchange server. You could limit it to only the ip of the sending server. However for sending mail through Exchange you will find the following EWS dll to be the better general solution. It's just as straightforward as your code is now but works better for Exchange.
You can try Microsoft Exchange Web Services using the following dll.
https://www.microsoft.com/en-us/download/details.aspx?id=42951
The object is pretty straight forward and comparable to using System.Net.Mail.
Here is an example I modified from the MSDN example.
The EWS url can be a bit confusing. It also depends if you have access to the Exchange server. Here is some help.
https://msdn.microsoft.com/en-us/library/office/dn509511(v=exchg.150).aspx
Helpful if you don't have access to the Exchange server.
http://nuanceimaging.custhelp.com/app/answers/detail/a_id/13098/~/determining-the-exchange-web-services-(ews)-url-for-the-sharescan-exchange
Change last part to Exchange.asmx, not Services.wsdl