Outlook web working

This commit is contained in:
Ben Olden-Cooligan 2018-08-07 18:15:25 -04:00
parent 330130a1d3
commit dbe64f20cb
7 changed files with 76 additions and 17 deletions

View File

@ -55,6 +55,7 @@ namespace NAPS2.ImportExport.Email.Oauth
accessTokenEncrypted = reader.ReadElementString();
refreshTokenEncrypted = reader.ReadElementString();
Expiry = DateTime.Parse(reader.ReadElementString());
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using MailKit;
using MimeKit;
using NAPS2.Config;
using Newtonsoft.Json.Linq;
namespace NAPS2.ImportExport.Email.Oauth
{
public class OutlookWebEmailProvider : IEmailProvider
{
private readonly IUserConfigManager userConfigManager;
private readonly OutlookWebOauthProvider outlookWebOauthProvider;
public OutlookWebEmailProvider(IUserConfigManager userConfigManager, OutlookWebOauthProvider outlookWebOauthProvider)
{
this.userConfigManager = userConfigManager;
this.outlookWebOauthProvider = outlookWebOauthProvider;
}
public bool SendEmail(EmailMessage emailMessage)
{
var messageObj = new JObject
{
{ "Subject", emailMessage.Subject },
{ "Body", new JObject
{
{ "ContentType", "Text" },
{ "Content", emailMessage.BodyText }
}},
{ "ToRecipients", Recips(emailMessage, EmailRecipientType.To) },
{ "CcRecipients", Recips(emailMessage, EmailRecipientType.Cc) },
{ "BccRecipients", Recips(emailMessage, EmailRecipientType.Bcc) },
{ "Attachments", new JArray(emailMessage.Attachments.Select(attachment => new JObject
{
{ "@odata.type", "#Microsoft.OutlookServices.FileAttachment" },
{ "Name", attachment.AttachmentName },
{ "ContentBytes", Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath)) }
}))}
};
var respUrl = outlookWebOauthProvider.UploadDraft(messageObj.ToString());
// Open the draft in the user's browser
Process.Start(respUrl + "&ispopout=0");
return true;
}
private JToken Recips(EmailMessage message, EmailRecipientType type)
{
return new JArray(message.Recipients.Where(recip => recip.Type == type).Select(recip => new JObject
{
{ "EmailAddress", new JObject
{
{ "Address", recip.Address },
{ "Name", recip.Name }
}}
}));
}
}
}

View File

@ -37,7 +37,7 @@ namespace NAPS2.ImportExport.Email.Oauth
}
}
protected override string Scope => "https://outlook.office.com/mail.readwrite https://outlook.office.com/mail.send offline_access";
protected override string Scope => "https://outlook.office.com/mail.readwrite https://outlook.office.com/mail.send https://outlook.office.com/user.read offline_access";
protected override string CodeEndpoint => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
@ -58,15 +58,14 @@ namespace NAPS2.ImportExport.Email.Oauth
public string GetEmailAddress()
{
return "";
//var resp = GetAuthorized("https://www.googleapis.com/gmail/v1/users/me/profile");
//return resp.Value<string>("emailAddress");
var resp = GetAuthorized("https://outlook.office.com/api/v1.0/me");
return resp.Value<string>("Id");
}
public string UploadDraft(string messageRaw)
{
var resp = PostAuthorized($"https://www.googleapis.com/upload/gmail/v1/users/{User}/drafts?uploadType=multipart", messageRaw, "message/rfc822");
return resp.Value<string>("id");
var resp = PostAuthorized("https://outlook.office.com/api/v1.0/me/messages", messageRaw, "application/json");
return resp.Value<string>("WebLink");
}
#endregion

View File

@ -143,6 +143,7 @@
<Compile Include="ImportExport\Email\EmailProviderType.cs" />
<Compile Include="ImportExport\Email\EmailSetup.cs" />
<Compile Include="ImportExport\Email\IEmailProviderFactory.cs" />
<Compile Include="ImportExport\Email\Oauth\OutlookWebEmailProvider.cs" />
<Compile Include="ImportExport\Email\Oauth\OutlookWebOauthProvider.cs" />
<Compile Include="ImportExport\Email\Oauth\GmailOauthProvider.cs" />
<Compile Include="ImportExport\Email\Oauth\GmailEmailProvider.cs" />

View File

@ -118,15 +118,6 @@ namespace NAPS2.WinForms
throw new NotImplementedException();
}
private EmailSetup GetOrCreateSetup()
{
if (UserConfigManager.Config.EmailSetup == null)
{
UserConfigManager.Config.EmailSetup = new EmailSetup();
}
return UserConfigManager.Config.EmailSetup;
}
private void ShowWidgets()
{
int heightDiff = Height - panel1.Height;

View File

@ -46,9 +46,9 @@ namespace NAPS2.WinForms
private ToolStripButton tsSharpen;
private readonly ScannedImageRenderer scannedImageRenderer;
private readonly KeyboardShortcutManager ksm;
private readonly UserConfigManager userConfigManager;
private readonly IUserConfigManager userConfigManager;
public FViewer(ChangeTracker changeTracker, IOperationFactory operationFactory, WinFormsExportHelper exportHelper, AppConfigManager appConfigManager, ScannedImageRenderer scannedImageRenderer, KeyboardShortcutManager ksm, UserConfigManager userConfigManager)
public FViewer(ChangeTracker changeTracker, IOperationFactory operationFactory, WinFormsExportHelper exportHelper, AppConfigManager appConfigManager, ScannedImageRenderer scannedImageRenderer, KeyboardShortcutManager ksm, IUserConfigManager userConfigManager)
{
this.changeTracker = changeTracker;
this.operationFactory = operationFactory;

View File

@ -27,6 +27,8 @@ namespace NAPS2.DI
{
case EmailProviderType.Gmail:
return kernel.Get<GmailEmailProvider>();
case EmailProviderType.OutlookWeb:
return kernel.Get<OutlookWebEmailProvider>();
default:
return kernel.Get<MapiEmailProvider>();
}