From 3e8ad12eee8ea72da1a4f9441dcc88a14840c88f Mon Sep 17 00:00:00 2001 From: svenskapojkarna Date: Tue, 6 Jun 2023 10:56:03 +0300 Subject: [PATCH 1/2] ISSUE-60: Fixed issue with adding multiple attachments to an email --- .../SendExchangeEmailTests.cs | 34 ++++++++++++++ Frends.Community.Email/EmailTask.cs | 44 ++++++------------- .../Frends.Community.Email.csproj | 5 +-- Frends.Community.Email/ReadEmailTask.cs | 1 - README.md | 1 + 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/Frends.Community.Email.Tests/SendExchangeEmailTests.cs b/Frends.Community.Email.Tests/SendExchangeEmailTests.cs index 861108a..6e21b7a 100644 --- a/Frends.Community.Email.Tests/SendExchangeEmailTests.cs +++ b/Frends.Community.Email.Tests/SendExchangeEmailTests.cs @@ -198,6 +198,40 @@ public async Task SendEmailWithFileAttachmentTest() await DeleteMessages(subject); } + [Test] + public async Task SendEmailWithMultipleFileAttachmentTest() + { + var subject = "Email test - MultiFileAttachment"; + var filePath1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../first.txt"); + File.WriteAllText(filePath1, "This is a test attachment file."); + var filePath2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../second.txt"); + File.WriteAllText(filePath2, "This is a test attachment file."); + var input = new ExchangeInput + { + To = _username, + Message = "This email has multiple file attachments.", + IsMessageHtml = false, + Subject = subject + }; + + var attachment = new Attachment + { + AttachmentType = AttachmentType.FileAttachment, + FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../*.txt"), + ThrowExceptionIfAttachmentNotFound = false, + SendIfNoAttachmentsFound = false + }; + + var attachmentArray = new Attachment[] { attachment }; + + var result = await EmailTask.SendEmailToExchangeServer(input, attachmentArray, _server, new CancellationToken()); + Assert.IsTrue(result.EmailSent); + File.Delete(filePath1); + File.Delete(filePath2); + Thread.Sleep(2000); // Give the email some time to get through. + await DeleteMessages(subject); + } + [Test] public async Task SendEmailWithBigFileAttachmentTest() { diff --git a/Frends.Community.Email/EmailTask.cs b/Frends.Community.Email/EmailTask.cs index 8d451fe..0ea548c 100644 --- a/Frends.Community.Email/EmailTask.cs +++ b/Frends.Community.Email/EmailTask.cs @@ -213,6 +213,17 @@ private static async Task SendExchangeEmailWithAttachments(Attachment[] var attachmentList = new MessageAttachmentsCollectionPage(); var allAttachmentFilePaths = new List(); + var message = new Message + { + Subject = subject, + Body = messageBody, + ToRecipients = to, + CcRecipients = cc, + BccRecipients = bcc + }; + + var msgResult = await graphClient.Me.Messages.Request().AddAsync(message, cancellationToken); + foreach (var attachment in attachments) { @@ -248,17 +259,6 @@ private static async Task SendExchangeEmailWithAttachments(Attachment[] }; } - var message = new Message - { - Subject = subject, - Body = messageBody, - ToRecipients = to, - CcRecipients = cc, - BccRecipients = bcc - }; - - var msgResult = await graphClient.Me.Messages.Request().AddAsync(message, cancellationToken); - foreach (var filePath in allAttachmentFilePaths) { cancellationToken.ThrowIfCancellationRequested(); @@ -279,11 +279,12 @@ private static async Task SendExchangeEmailWithAttachments(Attachment[] var largeFileUploadTask = new LargeFileUploadTask(uploadSession, stream); await largeFileUploadTask.UploadAsync(); } - await graphClient.Me.Messages[msgResult.Id].Send().Request().PostAsync(cancellationToken); + if (attachment.AttachmentType == AttachmentType.AttachmentFromString) CleanUpTempWorkDir(tempFilePath); } } + await graphClient.Me.Messages[msgResult.Id].Send().Request().PostAsync(cancellationToken); return new Output { EmailSent = true, @@ -375,25 +376,6 @@ private static string InitializeTemporaryWorkPath() return tempWorkDir; } - private static Encoding GetEncoding(string encoding) - { - switch (encoding.ToLower()) - { - case "utf-8": - return Encoding.UTF8; - case "ascii": - return Encoding.ASCII; - case "utf-7": - return Encoding.UTF7; - case "unicode": - return Encoding.Unicode; - case "utf-32": - return Encoding.UTF32; - default: - return Encoding.Default; - } - } - #endregion } } diff --git a/Frends.Community.Email/Frends.Community.Email.csproj b/Frends.Community.Email/Frends.Community.Email.csproj index 82c720f..2099087 100644 --- a/Frends.Community.Email/Frends.Community.Email.csproj +++ b/Frends.Community.Email/Frends.Community.Email.csproj @@ -10,7 +10,7 @@ true Frends true - 4.2.0 + 4.2.1 false @@ -23,12 +23,9 @@ - - - diff --git a/Frends.Community.Email/ReadEmailTask.cs b/Frends.Community.Email/ReadEmailTask.cs index 7ce08d1..a12f10c 100644 --- a/Frends.Community.Email/ReadEmailTask.cs +++ b/Frends.Community.Email/ReadEmailTask.cs @@ -13,7 +13,6 @@ using Directory = System.IO.Directory; using Path = System.IO.Path; using File = System.IO.File; -using Newtonsoft.Json.Linq; namespace Frends.Community.Email { diff --git a/README.md b/README.md index 2f9fd94..ed88bde 100644 --- a/README.md +++ b/README.md @@ -320,3 +320,4 @@ NOTE: Be sure to merge the latest from "upstream" before making a pull request! | 4.1.2 | ReadEmailFromExchangeServer: Fixed issue where FileAttachment is not recognized correctly when attachment is fetched. | | 4.1.3 | ReadEmailFromExchangeServer: Ignore ItemAttachments to prevent failing of the Task if attachment is ItemAttachment instead of FileAttachment. | | 4.2.0 | SendEmail: Added feature to add custom headers to email sending. | +| 4.2.0 | SendEmailToExchangeServer: Fixed issue which prevented adding multiple attachment to email. | From dd5dd0fd066a7a5acdc5f521bd40d74f26ebf90f Mon Sep 17 00:00:00 2001 From: svenskapojkarna Date: Tue, 6 Jun 2023 11:41:12 +0300 Subject: [PATCH 2/2] ISSUE-60: Fixed version number in changelog. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed88bde..1ba3160 100644 --- a/README.md +++ b/README.md @@ -320,4 +320,4 @@ NOTE: Be sure to merge the latest from "upstream" before making a pull request! | 4.1.2 | ReadEmailFromExchangeServer: Fixed issue where FileAttachment is not recognized correctly when attachment is fetched. | | 4.1.3 | ReadEmailFromExchangeServer: Ignore ItemAttachments to prevent failing of the Task if attachment is ItemAttachment instead of FileAttachment. | | 4.2.0 | SendEmail: Added feature to add custom headers to email sending. | -| 4.2.0 | SendEmailToExchangeServer: Fixed issue which prevented adding multiple attachment to email. | +| 4.2.1 | SendEmailToExchangeServer: Fixed issue which prevented adding multiple attachment to email. |