简体   繁体   English

c#通过Exchange Server 2010发送电子邮件

[英]c# Send Email through Exchange server 2010

I am trying to write a code that will send email through my exchange server 2010 and i get this error: 我正在尝试编写将通过我的Exchange Server 2010发送电子邮件的代码,但出现此错误:

An unhandled exception of type 'System.Security.Authentication.AuthenticationException' occurred in System.dll System.dll中发生了类型为'System.Security.Authentication.AuthenticationException'的未处理异常

I confirmed sending anonymous email on my exchange server and all my information is correct (ip, port, username and password). 我确认在我的交换服务器上发送了匿名电子邮件,并且我所有的信息都是正确的(ip,端口,用户名和密码)。

But still unable to make sending. 但是仍然无法发送。

Here is the code i am using in c#: 这是我在C#中使用的代码:

            SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.Host = "myip";

        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("username", "password");

        MailMessage mm = new MailMessage("myEmail", "SendToEmail", "test","test" );
        mm.BodyEncoding = UTF8Encoding.UTF8;
        mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

        client.Send(mm);

Thanks for your help! 谢谢你的帮助!

You can send emails using the Exchange Web Services API (EWS). 您可以使用Exchange Web服务API(EWS)发送电子邮件。 EWS is a set of old fashioned ASMX web services hosted on the same server as OWA. EWS是与OWA托管在同一服务器上的一组老式ASMX Web服务。 Microsoft has even published a managed API wrapper for EWS. 微软甚至发布了EWS的托管API包装器。

Here is an example on how to send emails using EWS: http://code.msdn.microsoft.com/Send-Email-with-Exchange-50189e57 这是有关如何使用EWS发送电子邮件的示例: http : //code.msdn.microsoft.com/Send-Email-with-Exchange-50189e57

Usually EWS can be found at http://yourexchangeserver/ews/exchange.asmx but with the managed API you can use autodiscovery to automatically find the address of EWS on your Exchange server. 通常,可以在http://yourexchangeserver/ews/exchange.asmx上找到EWS,但是通过托管API,您可以使用自动发现功能自动在Exchange服务器上查找EWS的地址。

Here is an overview where things like autodiscovery are explained. 这是概述,其中说明了诸如自动发现之类的事情。

Update regarding Exchange 2003: 有关Exchange 2003的更新:

You can access the Exchange 2003 mail store via HTTP using WebDAV. 您可以使用WebDAV通过HTTP访问Exchange 2003邮件存储。 WebDAV is a bit of a pain to use because you may have to use Forms Based Authentication (FBA) if that is what your OWA installation requires (in comparison EWS can use Windows Authentication even if OWA on Exchange 2007/2010 is using FBA). WebDAV的使用有点麻烦,因为如果您的OWA安装需要它,则可能必须使用基于表单的身份验证(FBA)(相比之下,即使Exchange 2007/2010上的OWA使用FBA,EWS也可以使用Windows身份验证)。

I have never tried sending mails using WebDAV for Exchange (although I have used WebDAV for a number of other things) but I found an example on MSDN that you may want to try. 我从未尝试使用WebDAV for Exchange发送邮件(尽管我已经将WebDAV用于许多其他事情),但是我在MSDN上找到了一个您可能想尝试的示例。 It uses Windows Authentication so it will not work if your Exchange 2003 OWA is set up to use FBA. 它使用Windows身份验证,因此,如果将Exchange 2003 OWA设置为使用FBA,它将无法使用。 If you need to use FBA let me know - I may have some sample code somewhere that you can use. 如果您需要使用FBA,请告诉我-我可能在某些地方可以使用一些示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM