private void EmailScannedImages() { if (scannedImages.Count == 0) { errorOutput.DisplayError(ConsoleResources.NoPagesToEmail); return; } OutputVerbose(ConsoleResources.Emailing); var message = new EmailMessage { Subject = options.EmailSubject ?? "", BodyText = options.EmailBody, AutoSend = options.EmailAutoSend, SilentSend = options.EmailSilentSend }; message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.To, options.EmailTo)); message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.Cc, options.EmailCc)); message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.Bcc, options.EmailTo)); var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName())); tempFolder.Create(); try { string attachmentName = fileNamePlaceholders.SubstitutePlaceholders(options.EmailFileName, startTime, false); string targetPath = Path.Combine(tempFolder.FullName, attachmentName); if (IsPdfFile(targetPath)) { if (options.OutputPath != null && IsPdfFile(options.OutputPath)) { // The scan has already been exported to PDF, so use that file OutputVerbose(ConsoleResources.AttachingExportedPDF, attachmentName); message.Attachments.Add(new EmailAttachment { FilePath = options.OutputPath, AttachmentName = attachmentName }); } else { // The scan hasn't bee exported to PDF yet, so it needs to be exported to the temp folder OutputVerbose(ConsoleResources.ExportingPDFToAttach); DoExportToPdf(targetPath); // Attach the PDF file AttachFilesInFolder(tempFolder, message); } } else { // Export the images to the temp folder // Don't bother to re-use previously exported images, because the possible different formats and multiple files makes it non-trivial, // and exporting is pretty cheap anyway OutputVerbose(ConsoleResources.ExportingImagesToAttach); DoExportToImageFiles(targetPath); // Attach the image file(s) AttachFilesInFolder(tempFolder, message); } OutputVerbose(ConsoleResources.SendingEmail); if (emailer.SendEmail(message)) { OutputVerbose(ConsoleResources.EmailSent); } else { OutputVerbose(ConsoleResources.EmailNotSent); } } finally { tempFolder.Delete(true); } }
private async Task EmailScannedImages() { if (scanList.Count == 0) { errorOutput.DisplayError(ConsoleResources.NoPagesToEmail); return; } OutputVerbose(ConsoleResources.Emailing); var message = new EmailMessage { Subject = fileNamePlaceholders.SubstitutePlaceholders(options.EmailSubject, startTime, false) ?? "", BodyText = fileNamePlaceholders.SubstitutePlaceholders(options.EmailBody, startTime, false), AutoSend = options.EmailAutoSend, SilentSend = options.EmailSilentSend }; message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.To, options.EmailTo)); message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.Cc, options.EmailCc)); message.Recipients.AddRange(EmailRecipient.FromText(EmailRecipientType.Bcc, options.EmailBcc)); var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName())); tempFolder.Create(); try { string targetPath = Path.Combine(tempFolder.FullName, options.EmailFileName); if (IsPdfFile(targetPath)) { if (options.OutputPath != null && IsPdfFile(options.OutputPath)) { // The scan has already been exported to PDF, so use that file OutputVerbose(ConsoleResources.AttachingExportedPDF); int digits = (int)Math.Floor(Math.Log10(scanList.Count)) + 1; int i = 0; foreach (var path in actualOutputPaths) { string attachmentName = fileNamePlaceholders.SubstitutePlaceholders(options.EmailFileName, startTime, false, i++, scanList.Count > 1 ? digits : 0); message.Attachments.Add(new EmailAttachment { FilePath = path, AttachmentName = attachmentName }); } } else { // The scan hasn't bee exported to PDF yet, so it needs to be exported to the temp folder OutputVerbose(ConsoleResources.ExportingPDFToAttach); if (!await DoExportToPdf(targetPath, true)) { OutputVerbose(ConsoleResources.EmailNotSent); return; } // Attach the PDF file AttachFilesInFolder(tempFolder, message); } } else { // Export the images to the temp folder // Don't bother to re-use previously exported images, because the possible different formats and multiple files makes it non-trivial, // and exporting is pretty cheap anyway OutputVerbose(ConsoleResources.ExportingImagesToAttach); await DoExportToImageFiles(targetPath); // Attach the image file(s) AttachFilesInFolder(tempFolder, message); } OutputVerbose(ConsoleResources.SendingEmail); if (await emailProviderFactory.Default.SendEmail(message, (j, k) => { }, CancellationToken.None)) { OutputVerbose(ConsoleResources.EmailSent); } else { OutputVerbose(ConsoleResources.EmailNotSent); } } finally { tempFolder.Delete(true); } }