2011-11-14 22 views
1

通過Gmail SMTP而不是通過交換2K7發送時,下面的代碼工作與發送SMTP郵件。 在同一臺機器上,我有Outlook Express通過交換成功發送。 我用我的代碼相同的配置在Outlook Express中,但不斷收到錯誤:C#通過交換2K7

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代碼:

//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); 

更新:當我拿出回調線並嘗試發送我得到這個錯誤:

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) 
+0

只是猜測:交換服務器正在使用自簽名證書。您需要在運行代碼的機器上安裝該證書。 – ConsultUtah

+0

但是,它是否有意義,Outlook Express可以在沒有安裝證書的同一臺計算機上工作?我認爲它也使用SMTP。 – Orson

回答

1

即興,它看起來像你一切正確。我有兩個建議是黑暗中刺,但你可能想要看得更近:

既然你得到︰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數據包相沖突。你可以發送沒有該行的郵件嗎?

我希望這些可以把你的方向,以真正的答案。

+0

嘗試沒有SSL ..沒有快樂。其設立要求SSL。將嘗試#2。 – Orson

+0

拿出那條線。請參閱帖子中的UPDATE。 – Orson

3

找到了一個很好的解決方案。不要嘗試使用交換2K7及以上的smtp。改爲使用EWS(Exchange Web服務)。

的步驟是:

  1. 下載EWS託管API從http://www.microsoft.com/download/en/details.aspx?id=13480
  2. 安裝它
  3. 添加到Microsoft.Exchange.WebServices.dll引用到您的項目。你會用C發現:\程序文件(x86)\微軟\交換\ Web服務\ 1.1
  4. 使用下面的示例代碼作爲參考。使用Microsoft.Exchange.WebServices.Data

    ;使用System.Security.Cryptography.X509Certificates

    ;

...

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="[email protected]"; 

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

mailmessage.Subject = "Hello"; 

mailmessage.Body = "World"; 

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

mailmessage.Send(); 

希望它可以幫助你。