在获取的时候会报401错误。不知道是什么问题。求各位高手帮忙解答
5 个解决方案
#1
public static void SaveMailForExchange()
{
// 实例化
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
// 指定用户名,密码,和域名
service.Credentials = new WebCredentials(@"用户名", "密码", "域名");
// 指定邮箱账号
//service.AutodiscoverUrl("邮箱账号");
////给出Exchange Server的URL
service.Url = new Uri("服务地址");
//创建过滤器, 条件为邮件未读.
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
//查找Inbox,加入过滤器条件,结果10条
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(10));
foreach (Item item in findResults.Items)
{
Microsoft.Exchange.WebServices.Data.EmailMessage email = Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(service, item.Id);
if (!email.IsRead)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
sw.WriteLine("===========================MailBody===========================");
sw.WriteLine(email.Body);
sw.WriteLine("===============================================================");
sw.Close();
//标记为已读
email.IsRead = true;
//将对邮件的改动提交到服务器
email.Update(ConflictResolutionMode.AlwaysOverwrite);
}
}
}
#2
登陆凭据有问题啊,大部分情况是Credentials 写错
service.Credentials = new WebCredentials(user, pwd, domain);
其中user不要带上@domain
service.Credentials = new WebCredentials(user, pwd, domain);
其中user不要带上@domain
#3
关于Credentials 里面的参数。几种情况都试过了。都是报401和405的错误
#4
如果是405的话应该就是这个有问题
service.Url = new Uri("服务地址");
如果是webservice的话,服务地址应该是以asmx结尾的,不是wsdl
service.Url = new Uri("服务地址");
如果是webservice的话,服务地址应该是以asmx结尾的,不是wsdl
#5
非常感谢。问题已经解决。谢谢您
#1
public static void SaveMailForExchange()
{
// 实例化
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
// 指定用户名,密码,和域名
service.Credentials = new WebCredentials(@"用户名", "密码", "域名");
// 指定邮箱账号
//service.AutodiscoverUrl("邮箱账号");
////给出Exchange Server的URL
service.Url = new Uri("服务地址");
//创建过滤器, 条件为邮件未读.
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
//查找Inbox,加入过滤器条件,结果10条
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(10));
foreach (Item item in findResults.Items)
{
Microsoft.Exchange.WebServices.Data.EmailMessage email = Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(service, item.Id);
if (!email.IsRead)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
sw.WriteLine("===========================MailBody===========================");
sw.WriteLine(email.Body);
sw.WriteLine("===============================================================");
sw.Close();
//标记为已读
email.IsRead = true;
//将对邮件的改动提交到服务器
email.Update(ConflictResolutionMode.AlwaysOverwrite);
}
}
}
#2
登陆凭据有问题啊,大部分情况是Credentials 写错
service.Credentials = new WebCredentials(user, pwd, domain);
其中user不要带上@domain
service.Credentials = new WebCredentials(user, pwd, domain);
其中user不要带上@domain
#3
关于Credentials 里面的参数。几种情况都试过了。都是报401和405的错误
#4
如果是405的话应该就是这个有问题
service.Url = new Uri("服务地址");
如果是webservice的话,服务地址应该是以asmx结尾的,不是wsdl
service.Url = new Uri("服务地址");
如果是webservice的话,服务地址应该是以asmx结尾的,不是wsdl
#5
非常感谢。问题已经解决。谢谢您