Reputation: 11
Geeting below error
An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException' occurred in Microsoft.Exchange.WebServices.dll
ExchangeService oews = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
{
Credentials = new WebCredentials("[email protected]","******") //state your Exchange username,Exchange Password and Exchange Domain
};
oews.AutodiscoverUrl("[email protected]"); //User Mailbox whose inbox is to be accessed.
FindFoldersResults foundFolderResults = oews.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));
MEWS.Folder exchangeExchangeAPIArchivedFolder = foundFolderResults.Folders.ToList().Find(
f => f.DisplayName.Equals("SentItem", StringComparison.CurrentCultureIgnoreCase));
I made changed in code here using https://outlook.office365.com/EWS/Exchange.asmx now i am getting this error
An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.ServiceRequestException' occurred in Microsoft.Exchange.WebServices.dll
Additional information: The request failed. The remote server returned an error: (401) Unauthorized.
ExchangeService _service = new ExchangeService();
_service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10))) {
email.Load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.TextBody));
string recipients = "";
foreach (EmailAddress emailAddress in email.CcRecipients)
{
recipients += ";" + emailAddress.Address.ToString();
}
string internetMessageId = email.InternetMessageId;
string fromAddress = email.From.Address;
string recipient = recipients;
string subject = email.Subject;
}
Thanks in advance
Upvotes: 1
Views: 7595
Reputation: 884
This may be an authentication issue.
You could add the following code:
service.PreAuthenticate = true;
service.Credentials = new WebCredentials("YouEmailAdress","Password");
This is complete code:
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
//ICredentials creds = new NetworkCredential("xxxx", "xxxx.com");
service.Credentials = new WebCredentials("YouEmailAdress","Password");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.PreAuthenticate = true;
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, " YouEmailAdress");
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, SetFilter(), view);
foreach (Item item in findResults.Items)
{
if (item.Subject != null)
{
list.Add(item.Subject.ToString());
}
else
{
list.Add("test");
}
list.Add(item.DateTimeSent.ToString());
}
}
private static SearchFilter SetFilter()
{
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
return s;
}
private static 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 you use the acount is not your acount , you need to make sure that we have required permissions for EWS Impersonation as per the article mentioned below:
Configuring Exchange Impersonation (Exchange Web Services)
Using Exchange Impersonation (Exchange Web Services)
Upvotes: 1