首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#通过exchange 2K7使用smtp发送邮件

c#通过exchange 2K7使用smtp发送邮件
EN

Stack Overflow用户
提问于 2011-11-14 22:49:27
回答 2查看 2.2K关注 0票数 1

以下代码在通过gmail的smtp而不是通过exchange 2K7发送邮件时起作用。在同一台机器上,我有outlook express,它可以成功地通过交换发送。我在代码中使用了与outlook express中相同的配置,但不断收到错误:

代码语言:javascript
复制
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.7.1 Client was not authenticated.
Stack:    at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.MailCommand.Send ...

Ssl设置为true。

以下是da代码:

代码语言:javascript
复制
//create new MailMessage
mailmessage = new MailMessage(definition.From, definition.To);
mailmessage.ReplyToList.Add(definition.From);
mailmessage.IsBodyHtml = true;
mailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailmessage.Subject = definition.Subject;
mailmessage.Body = definition.Body;

mailmessage = MessageBody.CompleteMessage(mailmessage, definition);

//send MailMessage
//get smtpclient
SmtpClient smtp = null;

if (smtp == null)
{
//create, init
smtp = new SmtpClient(definition.Server, definition.Port)
{
    Credentials = new NetworkCredential(definition.Uid, definition.Pwd),
    EnableSsl = definition.Ssl,
    DeliveryMethod = SmtpDeliveryMethod.Network
};

}
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Send(mailmessage);

更新:当我取出回调行并尝试发送时,我得到了这个错误:

代码语言:javascript
复制
Exception: The remote certificate is invalid according to the validation procedure.. Stack:    at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.TlsStream.CallProcessAuthentication(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Mail.SmtpConnection.Flush()
   at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
   at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
EN

回答 2

Stack Overflow用户

发布于 2011-11-15 16:02:24

找到了一个很好的解决方案。不要试图在exchange 2K7及更高版本中使用smtp。请改用EWS (Exchange Web服务)。

具体步骤如下:

EWS从http://www.microsoft.com/download/en/details.aspx?id=13480

  • Install it

  • 下载

  • 将对Microsoft.Exchange.WebServices.dll的引用添加到您的项目中。您可以在C:\Program Files (X86)\ Services\1.1

  • Use中找到它,下面的示例代码作为参考。

使用Microsoft.Exchange.WebServices.Data;

使用System.Security.Cryptography.X509Certificates;

..。

代码语言:javascript
复制
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

service.Url = new Uri(@"https://"+[Your exchange computer name like: abc.domain.com]+"/EWS/Exchange.asmx"); 

//if have the ip you can get the computer name using the nslookup utility in command line. ->nslookup 192.168.0.90 

ServicePointManager.ServerCertificateValidationCallback =
                    delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                    {
                        return true;
                    };

service.Credentials = new WebCredentials([User name: either email or domain account-but without the domain\], "password");

EmailMessage mailmessage = new EmailMessage(service);

mailmessage.From="me@something.com";

mailmessage.ToRecipients.Add("someone@something.com");

mailmessage.Subject = "Hello";

mailmessage.Body = "World";

mailmessage.Body.BodyType = BodyType.HTML; //or text

mailmessage.Send();

希望能对你有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2011-11-14 23:46:36

即兴发挥,看起来你做的每件事都是正确的。我的两个建议都是徒劳的,但你可能想仔细看看:

由于您得到的是:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated. Stack: at System.Net.Mail.MailCommand ...,我将不得不假设Exchange服务器在解释它正在接收的数据包时遇到了问题。

1)这可能是由于SSL加密造成的。我会仔细检查您是否需要SSL。您可以在没有SSL的情况下发送用户名和密码(尽管不建议这样做)。这将是一个糟糕的服务器设置,但这可能超出您的控制范围。

2)您还在做证书验证回调。这是一个额外的SMTP验证层,可能与SSL数据包冲突。你能不用那条线发邮件吗?

我希望这些能将你推向一个真正的答案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8123309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档