2011-11-14 7 views
1

次のコードは、Gmailのsmtp経由で送信するが、交換2K7では送信しないときに機能します。同じマシン上に 私はOutlook Expressを持っています。私は、Outlook Expressのように私のコードで同じ設定を使用するが、エラーを取得しておくのです :c#交換2K7でsmtpでメールを送信

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に設定されています。

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

UPDATE:ここ

はダコードである私は、コールバックラインを取り出して送信しようとすると、私はこのエラーを取得:カフオフ

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

を、それはあなたがしているように見えますすべてを正しく行う。私が持っている2つの提案は、暗闇の中で刺すようなものですが、近くで見たいものがあります: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)証明書検証コールバックも実行しています。これは、SSLパケットと競合するかもしれないSMTP検証の余分な層です。あなたはその行なしでメールを送ることができますか?

これらが真の答えの方向にあなたを押し進めてくれることを願っています。

+0

はSSLを試していません。SSLを要求するように設定されています。 #2を試みます。 – Orson

+0

はその行を取り出しました。投稿のUPDATEを参照してください。 – Orson

3

が良い解決策を見つけました。交換2K7以上でsmtpを使用しようとしないでください。代わりにEWS(Exchange Web Services)を使用してください。

手順は次のとおりです。

  1. ダウンロードはEWSは、それが
  2. プロジェクトにMicrosoft.Exchange.WebServices.dllへの参照を追加インストールhttp://www.microsoft.com/download/en/details.aspx?id=13480
  3. からAPIを管理していました。 C:¥Program Files(x86)¥Microsoft¥Exchange¥Web Services¥1.1
  4. 以下のサンプルコードを参考にしてください。

    Using Microsoft.Exchange.WebServices.Data;

    using 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(); 

は、それはあなたを助け願っています。

関連する問題