diff --git a/NAPS2.Core/ImportExport/Email/MimeEmailProvider.cs b/NAPS2.Core/ImportExport/Email/MimeEmailProvider.cs index 1b0b19dd9..3f5e15f47 100644 --- a/NAPS2.Core/ImportExport/Email/MimeEmailProvider.cs +++ b/NAPS2.Core/ImportExport/Email/MimeEmailProvider.cs @@ -12,7 +12,8 @@ namespace NAPS2.ImportExport.Email { var builder = new BodyBuilder { - TextBody = string.IsNullOrWhiteSpace(emailMessage.BodyText) ? "Test body" : emailMessage.BodyText + // Ensure there is some content (a newline is fine) to work around the buggy new gmail UI + TextBody = string.IsNullOrWhiteSpace(emailMessage.BodyText) ? "\n" : emailMessage.BodyText }; foreach (var attachment in emailMessage.Attachments) { @@ -20,11 +21,10 @@ namespace NAPS2.ImportExport.Email } var message = new MimeMessage(); - emailMessage.Recipients.Add(new EmailRecipient { Address = "someone@example.com", Name = "Someone", Type = EmailRecipientType.To }); CopyRecips(emailMessage.Recipients, EmailRecipientType.To, message.To); CopyRecips(emailMessage.Recipients, EmailRecipientType.Cc, message.Cc); CopyRecips(emailMessage.Recipients, EmailRecipientType.Bcc, message.Bcc); - message.Subject = string.IsNullOrWhiteSpace(emailMessage.Subject) ? "Scan" : emailMessage.Subject; + message.Subject = emailMessage.Subject ?? ""; message.Body = builder.ToMessageBody(); SendMimeMessage(message); diff --git a/NAPS2.Core/ImportExport/Email/Oauth/GmailEmailProvider.cs b/NAPS2.Core/ImportExport/Email/Oauth/GmailEmailProvider.cs index b135bdc0c..3edfc246b 100644 --- a/NAPS2.Core/ImportExport/Email/Oauth/GmailEmailProvider.cs +++ b/NAPS2.Core/ImportExport/Email/Oauth/GmailEmailProvider.cs @@ -23,6 +23,9 @@ namespace NAPS2.ImportExport.Email.Oauth var messageId = gmailOauthProvider.UploadDraft(message.ToString()); var userEmail = userConfigManager.Config.EmailSetup?.GmailUser; // Open the draft in the user's browser + // Note: As of this writing, the direct url is bugged in the new gmail UI, and there is no workaround + // https://issuetracker.google.com/issues/113127519 + // At least it directs to the drafts folder Process.Start($"https://mail.google.com/mail/?authuser={userEmail}#drafts/{messageId}"); } } diff --git a/NAPS2.Core/WinForms/FEmailProvider.cs b/NAPS2.Core/WinForms/FEmailProvider.cs index add4233e8..469b94ecf 100644 --- a/NAPS2.Core/WinForms/FEmailProvider.cs +++ b/NAPS2.Core/WinForms/FEmailProvider.cs @@ -68,13 +68,13 @@ namespace NAPS2.WinForms }); } - providerWidgets.Add(new EmailProviderWidget - { - ProviderType = EmailProviderType.CustomSmtp, - ProviderIcon = Icons.email_setting, - ProviderName = EmailProviderType.CustomSmtp.Description(), - ClickAction = ChooseCustomSmtp - }); + //providerWidgets.Add(new EmailProviderWidget + //{ + // ProviderType = EmailProviderType.CustomSmtp, + // ProviderIcon = Icons.email_setting, + // ProviderName = EmailProviderType.CustomSmtp.Description(), + // ClickAction = ChooseCustomSmtp + //}); // Put the configured provider at the top var defaultWidget = GetDefaultWidget(); @@ -110,11 +110,6 @@ namespace NAPS2.WinForms } } - private void ChooseCustomSmtp() - { - throw new NotImplementedException(); - } - private void ShowWidgets() { int heightDiff = Height - panel1.Height; diff --git a/NAPS2.Core/WinForms/WinFormsExportHelper.cs b/NAPS2.Core/WinForms/WinFormsExportHelper.cs index 220b4fd54..301dec9a4 100644 --- a/NAPS2.Core/WinForms/WinFormsExportHelper.cs +++ b/NAPS2.Core/WinForms/WinFormsExportHelper.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; +using System.Windows.Forms; +using NAPS2.Config; using NAPS2.ImportExport; using NAPS2.ImportExport.Email; using NAPS2.ImportExport.Images; @@ -28,8 +30,9 @@ namespace NAPS2.WinForms private readonly OcrManager ocrManager; private readonly IEmailProviderFactory emailProviderFactory; private readonly IOperationProgress operationProgress; + private readonly IUserConfigManager userConfigManager; - public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, FileNamePlaceholders fileNamePlaceholders, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, IEmailProviderFactory emailProviderFactory, IOperationProgress operationProgress) + public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, FileNamePlaceholders fileNamePlaceholders, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, IEmailProviderFactory emailProviderFactory, IOperationProgress operationProgress, IUserConfigManager userConfigManager) { this.pdfSettingsContainer = pdfSettingsContainer; this.imageSettingsContainer = imageSettingsContainer; @@ -42,6 +45,7 @@ namespace NAPS2.WinForms this.ocrManager = ocrManager; this.emailProviderFactory = emailProviderFactory; this.operationProgress = operationProgress; + this.userConfigManager = userConfigManager; } public async Task SavePDF(List images, ISaveNotify notify) @@ -123,54 +127,66 @@ namespace NAPS2.WinForms public async Task EmailPDF(List images) { - if (images.Any()) + if (!images.Any()) { - var emailSettings = emailSettingsContainer.EmailSettings; - var invalidChars = new HashSet(Path.GetInvalidFileNameChars()); - var attachmentName = new string(emailSettings.AttachmentName.Where(x => !invalidChars.Contains(x)).ToArray()); - if (string.IsNullOrEmpty(attachmentName)) - { - attachmentName = "Scan.pdf"; - } - if (!attachmentName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)) - { - attachmentName += ".pdf"; - } - attachmentName = fileNamePlaceholders.SubstitutePlaceholders(attachmentName, DateTime.Now, false); + return false; + } - var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName())); - tempFolder.Create(); - try + if (userConfigManager.Config.EmailSetup == null) + { + // First run; prompt for a + var form = formFactory.Create(); + if (form.ShowDialog() != DialogResult.OK) { - string targetPath = Path.Combine(tempFolder.FullName, attachmentName); - if (!await ExportPDF(targetPath, images, true)) + return false; + } + } + + var emailSettings = emailSettingsContainer.EmailSettings; + var invalidChars = new HashSet(Path.GetInvalidFileNameChars()); + var attachmentName = new string(emailSettings.AttachmentName.Where(x => !invalidChars.Contains(x)).ToArray()); + if (string.IsNullOrEmpty(attachmentName)) + { + attachmentName = "Scan.pdf"; + } + if (!attachmentName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)) + { + attachmentName += ".pdf"; + } + attachmentName = fileNamePlaceholders.SubstitutePlaceholders(attachmentName, DateTime.Now, false); + + var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName())); + tempFolder.Create(); + try + { + string targetPath = Path.Combine(tempFolder.FullName, attachmentName); + if (!await ExportPDF(targetPath, images, true)) + { + // Cancel or error + return false; + } + var message = new EmailMessage + { + Attachments = { - // Cancel or error - return false; - } - var message = new EmailMessage - { - Attachments = + new EmailAttachment { - new EmailAttachment - { - FilePath = targetPath, - AttachmentName = attachmentName - } + FilePath = targetPath, + AttachmentName = attachmentName } - }; - - if (emailProviderFactory.Default.SendEmail(message)) - { - changeTracker.HasUnsavedChanges = false; - return true; } - } - finally + }; + + if (emailProviderFactory.Default.SendEmail(message)) { - tempFolder.Delete(true); + changeTracker.HasUnsavedChanges = false; + return true; } } + finally + { + tempFolder.Delete(true); + } return false; } }