简体   繁体   中英

Can not programmatically send email using web service of MS Exchange Server 2010 SP2

I got an app (C#) that send email using web service of MS Exchange Server 2010 SP2. It works fine before. But, just 2 weeks ago, I got the problem with it. Sometime the code can send an email but sometime it's not. I got the error message as below (full error message shows at the end).

The request failed. Unable to connect to the remote server

My code is below.

string ewsUrl = "https://xxxx.xxxx.com/ews/Exchange.asmx";
string userName = "xxxx";
string password = "xxxx";
string to = "xxxx@xxxx.com";
string cc = "xxxx@xxxx.com";

ExchangeService service = new ExchangeService();
service.Url = new Uri(ewsUrl);
service.UseDefaultCredentials = true;
service.Credentials = new WebCredentials(userName, password);

EmailMessage message = new EmailMessage(service);
message.Subject = subject;
message.Body = bodyHtml;
message.ToRecipients.Add(to);
message.CcRecipients.Add(cc);

message.SendAndSaveCopy();

I don't know what went wrong. I believe that the URL is correct and it's work sometime and don't another time. I have search on the internet but don't found any solutions for this. Is this something about server configuration? I have talked to my IT guy but it seems that he got no idea about the web service. Please help me. The full error message is below.

The request failed. Unable to connect to the remote server
    at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(HttpWebRequest& request)
    at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
    at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
    at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable`1 items, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode, ServiceErrorHandling errorHandling)
    at Microsoft.Exchange.WebServices.Data.ExchangeService.CreateItem(Item item, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
    at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
    at Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
    at Microsoft.Exchange.WebServices.Data.EmailMessage.Send()
ServicePointManager.ServerCertificateValidationCallback = 
    delegate(object sender1,
             System.Security.Cryptography.X509Certificates.X509Certificate certificate,
             System.Security.Cryptography.X509Certificates.X509Chain chain,
             System.Net.Security.SslPolicyErrors sslPolicyErrors) 
    { return true; };

I think you need to add the above code between message.CcRecipients.Add(cc); and message.SendAndSaveCopy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM