Oauth: Implement autosend for Outlook Web

This commit is contained in:
Ben Olden-Cooligan 2024-07-06 12:55:37 -07:00
parent 42cb1f5340
commit f7054b8230
3 changed files with 28 additions and 5 deletions

View File

@ -169,6 +169,12 @@ public abstract class OauthProvider
return JObject.Parse(response);
}
protected async Task PostAuthorizedNoResponse(string url)
{
using var client = AuthorizedClient();
await client.UploadStringTaskAsync(url, "POST", "");
}
private WebClient AuthorizedClient()
{
var client = new WebClient();

View File

@ -31,10 +31,18 @@ internal class OutlookWebEmailProvider : IEmailProvider
["contentBytes"] = Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath))
}))
};
var respUrl = await _outlookWebOauthProvider.UploadDraft(messageObj.ToString(), progress);
var draft = await _outlookWebOauthProvider.UploadDraft(messageObj.ToString(), progress);
// Open the draft in the user's browser
ProcessHelper.OpenUrl(respUrl + "&ispopout=0");
if (emailMessage.AutoSend)
{
await _outlookWebOauthProvider.SendDraft(draft.MessageId);
}
else
{
// Open the draft in the user's browser
ProcessHelper.OpenUrl(draft.WebLink + "&ispopout=0");
}
return true;
}

View File

@ -67,11 +67,20 @@ public class OutlookWebOauthProvider : OauthProvider
return resp.Value<string>("mail") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response");
}
public async Task<string> UploadDraft(string messageRaw, ProgressHandler progress = default)
public async Task<DraftInfo> UploadDraft(string messageRaw, ProgressHandler progress = default)
{
var resp = await PostAuthorized("https://graph.microsoft.com/v1.0/me/messages", messageRaw, "application/json", progress);
return resp.Value<string>("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response");
var webLink = resp.Value<string>("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response");
var messageId = resp.Value<string>("id") ?? throw new InvalidOperationException("Could not get ID from Outlook messages response");
return new DraftInfo(webLink, messageId);
}
public async Task SendDraft(string draftMessageId)
{
await PostAuthorizedNoResponse($"https://graph.microsoft.com/v1.0/me/messages/{draftMessageId}/send");
}
public record DraftInfo(string WebLink, string MessageId);
#endregion
}