Skip to content

Commit

Permalink
Oauth: Implement autosend for Outlook Web
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jul 6, 2024
1 parent 42cb1f5 commit f7054b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions NAPS2.Lib/ImportExport/Email/Oauth/OauthProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ protected async Task<JObject> PostAuthorized(string url, string body, string con
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();
Expand Down
14 changes: 11 additions & 3 deletions NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebEmailProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ public async Task<bool> SendEmail(EmailMessage emailMessage, ProgressHandler pro
["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;
}
Expand Down
13 changes: 11 additions & 2 deletions NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebOauthProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,20 @@ public string GetEmailAddress()
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
}

0 comments on commit f7054b8

Please sign in to comment.