Reputation: 21
I have been having issues accessing the shared mailbox with error The SMTP Address has no mailbox associated with it, though I can see the mailbox in the Outlook app. The both mailboxes are Office 365 mailboxes. I was having access before to the shared mailboxes mentioned above but through another mailbox and everything worked fine with my code. The thing worth mentioning is that I use this code inside Blue Prism. I am sorry but I am not a proper programmer so the code may be a little bit messy...any thoughts?
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;
}
For getting email from the mailbox I use this
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; }
Upvotes: 2
Views: 7721
Reputation: 26
I think your function CreateService() is missing following statement : service.Credentials = new NetworkCredential(userEmailAddress, userPassword);
in foreach loop put following code : ExchangeService service = new ExchangeService() service.Credentials = new NetworkCredential(userEmailAddress, userPassword);
and give proper email address and password. Let me know if it works
Upvotes: 1