【发布时间】:2020-03-15 21:56:43
【问题描述】:
我在访问共享邮箱时遇到问题,并出现错误 SMTP 地址没有与之关联的邮箱,但我可以在 Outlook 应用程序中看到该邮箱。 这两个邮箱都是 Office 365 邮箱。我之前可以访问上面提到的共享邮箱,但是通过另一个邮箱,我的代码一切正常。值得一提的是,我在 Blue Prism 中使用了这段代码。对不起,我不是一个合格的程序员,所以代码可能有点乱……有什么想法吗?
public static ExchangeService CreateService(
string user = "",
string pass = ""
)
{
bool success= false;
string[] serviceURLs= new []{
"yyy",
"xxx",
};
string[] knownAutodiscoveryUrls = new [] {
"qqq",
"zzz",
};
var autodiscoveryUrlsList = new List<string>();
autodiscoveryUrlsList.AddRange(knownAutodiscoveryUrls);
foreach (string serviceURL in serviceURLs)
{
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); // only for DotNetCore 2.1 and up
ExchangeService service = new ExchangeService();
service.Url = new Uri(serviceURL);
if (String.IsNullOrEmpty(pass.Trim())) { service.UseDefaultCredentials = false; } else { service.Credentials = new WebCredentials(user, pass); }
var findFolderResults = service.FindFolders(WellKnownFolderName.Root, new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0), new FolderView(10));
success=true;
return service;
//creates service with services URL
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
if (success==false){
try{
var service = new ExchangeService(ExchangeVersion.Exchange2013);
service.UseDefaultCredentials = false;
if (!string.IsNullOrWhiteSpace(user)) {
service.Credentials = new WebCredentials(user, pass);
}
service.AutodiscoverUrl(user,RedirectionUrlValidationCallback);
if (service.Url == null || string.IsNullOrWhiteSpace(service.Url.ToString())) {
const string baseMessage = "Failed to resolve valid ExchangeService object: service.Url property is either null or empty, meaning that no Exchange server was discovered!";
if (autodiscoveryUrlsList.Any()) {
throw new Exception(baseMessage + ". Tried following Autodiscover URL addresses: " + string.Join("; ", autodiscoveryUrlsList));
} else {
throw new Exception(baseMessage + ". Tried just to use classic ExchangeService.AutodiscoverUrl(string mailbox) approach in order to detect the Exchange server, no custom Autodiscover URL addresses were tried.");
}
}
var findFolderResults = service.FindFolders(WellKnownFolderName.Root, new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0), new FolderView(10));
return service;
}
为了从邮箱中获取电子邮件,我使用这个
ExchangeService service = CreateService(user, pass);
if (item.StartsWith("\\"))
//Returns item form specific folder e.g \Inbox without knowing exact Email ID
{
folderId = GetFolderId(item, user,pass,mailbox);
ItemView view = new ItemView(1);
if (ascending) { view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending); } else { view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending); }
FindItemsResults<Item> findResults = service.FindItems(folderId, view);
int itemCount = findResults.Count();
if (itemCount == 0) { noEmail = true; Console.WriteLine("No email"); return; }
else
{
noEmail = false;
}
itemId = findResults.Items[0].Id.UniqueId.ToString();
}
else { itemId = item; }
【问题讨论】:
标签: c# smtp exchange-server exchangewebservices blueprism