Reputation: 121
I am using Microsoft Exchange Web Services - EWS Managed API 2.2 to send mails from my Exchange Server email. Here's the code I am using :
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("[email protected]");
email.ToRecipients.Add("[email protected]");
email.ToRecipients.Add("[email protected]");
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;
}
}
If I set the ToEmailAddress as the login username itself, the mail is being sent to inbox. If I send to any other email address(tried gmail,hotmail,yahoo), the mail is deposited in the sent mail, but it is not delivered to the email address intended. But if I login to Outlook Web App and type a mail and send, the mail is added to Sent Items as well as delivered to the recipent email address.
I have checked Spam/Junk folder etc. The credentials are correct because I am able to save the copy of mail in Sent Items.
What is it that I am doing wrong?
Upvotes: 0
Views: 1530
Reputation: 121
It turns out the mail was being spammed out at the outgoing exchange server itself. If I set a meaningful Subject, the mail was being sent.
email.Subject = "Some meaningful subject";
Upvotes: 0