EWS:邮件已添加到已发送邮件但未传递

时间:2015-07-20 06:38:01

标签: c# exchange-server exchangewebservices

我正在使用Microsoft Exchange Web服务 - EWS托管API 2.2从我的Exchange Server电子邮件发送邮件。 这是我正在使用的代码:

ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("UserName", "Password", "domain");
service.Url = new Uri("https://exchangeserver.com/EWS/Exchange.asmx");
EmailMessage email = new EmailMessage(service);
//email.ToRecipients.Add("example@hotmail.com");
email.ToRecipients.Add("example@gmail.com");
email.ToRecipients.Add("username@domain.com");
email.Body = new MessageBody("BODY!");

email.SendAndSaveCopy();


//Here is my Certificate Validation function taken straight from MSDN :

static private bool CertificateValidationCallBack(object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    // If the certificate is a valid, signed certificate, return true.
    if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
    {
        return true;
    }

    // If there are errors in the certificate chain, look at each error to determine the cause.
    if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
    {
        if (chain != null && chain.ChainStatus != null)
        {
            foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
            {
                if ((certificate.Subject == certificate.Issuer) &&                             (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                {
                    // Self-signed certificates with an untrusted root are valid. 
                    continue;
                }
                else
                {
                    if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                    {
                        // If there are any other errors in the certificate chain, the certificate is invalid,
                        // so the method returns false.
                        return false;
                    }
                }
            }
        }

        // When processing reaches this line, the only errors in the certificate chain are 
        // untrusted root errors for self-signed certificates. These certificates are valid
        // for default Exchange server installations, so return true.
        return true;
    }
    else
    {
        // In all other cases, return false.
        return false;
    }
}

如果我将ToEmailAddress设置为登录用户名,则邮件将被发送到收件箱。 如果我发送到任何其他电子邮件地址(尝试过gmail,hotmail,yahoo),邮件将存入已发送的邮件中,但邮件不会发送到预期的电子邮件地址。 但是,如果我登录到Outlook Web App并键入邮件并发送,邮件将添加到已发送邮件以及传送到配方电子邮件地址。

我已检查垃圾邮件/垃圾文件夹等。凭据正确无误,因为我可以在已发送邮件中保存邮件副本。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

事实证明,邮件在传出的Exchange服务器本身被垃圾邮件发送出去了。如果我设置了一个有意义的主题,则会发送邮件。

email.Subject = "Some meaningful subject";