向 Exchange 2007 进行身份验证

发布于 2024-10-27 05:56:40 字数 1629 浏览 1 评论 0原文

我有一个简单的邮件实用程序,应该通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-11-03 05:56:40

我测试过,你的代码工作正常。请检查您使用的帐户是否在 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.

止于盛夏 2024-11-03 05:56:40

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 示例修改的示例。

    using Microsoft.Exchange.WebServices.Data;


        protected void EmailEws()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

            service.Credentials = new WebCredentials("user", "password", "domain(local)");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
// EWS service url.
            service.Url = new Uri("https://example.com/EWS/Exchange.asmx");

            EmailMessage email = new EmailMessage(service);

            email.ToRecipients.Add("[email protected]");

            email.Subject = "HelloWorld";          
            email.Body = new MessageBody("<p>This is the first email I've sent by using the EWS Managed API!</p>");
            // set Body before BodyType or an error is raised!                                                        
            email.Body.BodyType = BodyType.HTML;

            email.SendAndSaveCopy();
        }

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.

    using Microsoft.Exchange.WebServices.Data;


        protected void EmailEws()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

            service.Credentials = new WebCredentials("user", "password", "domain(local)");

            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
// EWS service url.
            service.Url = new Uri("https://example.com/EWS/Exchange.asmx");

            EmailMessage email = new EmailMessage(service);

            email.ToRecipients.Add("[email protected]");

            email.Subject = "HelloWorld";          
            email.Body = new MessageBody("<p>This is the first email I've sent by using the EWS Managed API!</p>");
            // set Body before BodyType or an error is raised!                                                        
            email.Body.BodyType = BodyType.HTML;

            email.SendAndSaveCopy();
        }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文