|
@@ -17,7 +17,7 @@ namespace InABox.Mail
|
|
|
|
|
|
public class ExchangeMailer : CoreMailer<ExchangeMailFolder, ExchangeMailSummary, ExchangeMailMessage>
|
|
|
{
|
|
|
- private ExchangeService Service;
|
|
|
+ private ExchangeService? Service;
|
|
|
|
|
|
private ExchangeVersion Version = ExchangeVersion.Exchange2007_SP1;
|
|
|
|
|
@@ -85,12 +85,20 @@ namespace InABox.Mail
|
|
|
else
|
|
|
{
|
|
|
Service.Credentials = new WebCredentials(MailboxUserName, MailboxPassword, MailboxDomain);
|
|
|
- Service.Url = new Uri(string.Format("https://{0}:{1}/EWS/Exchange.asmx", MailboxHost, MailboxPort == 0 ? 443 : MailboxPort));
|
|
|
+ if (MailboxHost.IsNullOrWhiteSpace())
|
|
|
+ {
|
|
|
+ Service.AutodiscoverUrl(MailboxUserName, RedirectionUrlValidationCallback);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Service.Url = new Uri(string.Format("https://{0}:{1}/EWS/Exchange.asmx", MailboxHost, MailboxPort == 0 ? 443 : MailboxPort));
|
|
|
+ }
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception err)
|
|
|
{
|
|
|
+ CoreUtils.LogException("", err);
|
|
|
Service = null;
|
|
|
return false;
|
|
|
}
|
|
@@ -100,6 +108,11 @@ namespace InABox.Mail
|
|
|
{
|
|
|
return Service != null;
|
|
|
}
|
|
|
+
|
|
|
+ private ExchangeService GetService()
|
|
|
+ {
|
|
|
+ return Service ?? throw new Exception("Not Connected");
|
|
|
+ }
|
|
|
|
|
|
protected override bool DoSendMessage(ExchangeMailMessage message)
|
|
|
{
|
|
@@ -118,20 +131,24 @@ namespace InABox.Mail
|
|
|
|
|
|
email.Subject = message.Subject;
|
|
|
email.Body = new MessageBody(BodyType.Text, message.Body);
|
|
|
- email.SendAndSaveCopy();
|
|
|
+ email.SendAndSaveCopy().ContinueWith(MessageSent);
|
|
|
result = true;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ private void MessageSent(System.Threading.Tasks.Task task)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
protected override ExchangeMailFolder DoFindFolder(ExchangeMailFolder parent, string name)
|
|
|
{
|
|
|
FindFoldersResults results = null;
|
|
|
if (parent == null)
|
|
|
- results = Service.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue)).Result;
|
|
|
+ results = GetService().FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue)).Result;
|
|
|
else
|
|
|
- results = Service.FindFolders(parent.Folder.Id, new FolderView(int.MaxValue)).Result;
|
|
|
+ results = GetService().FindFolders(parent.Folder.Id, new FolderView(int.MaxValue)).Result;
|
|
|
var result = results.FirstOrDefault(x => x.DisplayName.Equals(name));
|
|
|
return new ExchangeMailFolder(result);
|
|
|
}
|
|
@@ -155,7 +172,7 @@ namespace InABox.Mail
|
|
|
|
|
|
protected override IEnumerable<ExchangeMailMessage> DoGetMessages(ExchangeMailFolder folder)
|
|
|
{
|
|
|
- var parent = folder != null ? folder.Folder : Service.FindFolders(WellKnownFolderName.Inbox, new FolderView(1)).Result.FirstOrDefault();
|
|
|
+ var parent = folder != null ? folder.Folder : GetService().FindFolders(WellKnownFolderName.Inbox, new FolderView(1)).Result.FirstOrDefault();
|
|
|
if (parent != null)
|
|
|
{
|
|
|
FindItemsResults<Item> items = parent.FindItems(new ItemView(int.MaxValue)).Result;
|
|
@@ -168,6 +185,7 @@ namespace InABox.Mail
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
+ CoreUtils.LogException("", e);
|
|
|
}
|
|
|
|
|
|
return results;
|