2

Outlook Exchange サーバー経由で電子メールを送信する C# .net 実行可能ファイルを作成しました。手動で実行するとすべて正常に動作しますが、スケジュールされたタスクを使用して実行可能ファイルを呼び出すと、電子メールが送信されません。他のすべては正常に機能しますが、電子メールは送信されません。スケジュールされたタスクを自分のユーザー アカウントとして実行するように設定しました。タスクが実行されているとき、実行可能ファイルが自分のユーザー名で実行されていることがタスク マネージャーで確認できます。これにより、明らかなアクセス許可の問題が除外されます。

デバッグ中に、Exchange が実行されている同じマシン上のネットワーク共有上のファイルにプログラムがテキストを出力するようにしました。このファイルは正常に出力されるため、プログラムがそのマシンに接続できることがわかります。

誰でも助けることができますか?

4

1 に答える 1

1

上記のように、Outlook の実行中のインスタンスを介してメールを送信しようとしていました。コメント ボックスにコードを投稿することはできませんでしたが、@amitapollo は System.Net.Mail 名前空間を使用する手がかりを与えてくれました。一日の終わりに、私はそれを働かせました。これが私のコードです:

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("myExchangeServerIPAddress");
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = new System.Net.NetworkCredential("myDomain\\myUsername", "myPassword");
        smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;            
        smtpClient.EnableSsl = true;

System.Security.Cryptography.X509Certificates.X509Store xStore = new System.Security.Cryptography.X509Certificates.X509Store();
        System.Security.Cryptography.X509Certificates.OpenFlags xFlag = System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly;
        xStore.Open(xFlag);
        System.Security.Cryptography.X509Certificates.X509Certificate2Collection xCertCollection = xStore.Certificates;
        System.Security.Cryptography.X509Certificates.X509Certificate xCert = new System.Security.Cryptography.X509Certificates.X509Certificate();
        foreach (System.Security.Cryptography.X509Certificates.X509Certificate _Cert in xCertCollection)
        {
            if (_Cert.Subject.Contains("myUsername@myDomain.com"))
            {                    
                xCert = _Cert;
            }
        }

smtpClient.ClientCertificates.Add(xCert);

//I was having problems with the remote certificate no being validated so I had to override all security settings with this line of code...

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };
smtpClient.Send("myUsername@myDomain.com", "myUsername@myDomain.com", "mySubject", "myBody");
于 2013-05-17T22:05:34.733 に答える