Skip to content

Commit

Permalink
Merge pull request #61 from CommunityHiQ/ISSUE-60
Browse files Browse the repository at this point in the history
ISSUE-60: Fixed issue with adding multiple attachments to an email
  • Loading branch information
RikuVirtanen authored Jun 6, 2023
2 parents 5b77ca7 + dd5dd0f commit 11cf42b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
34 changes: 34 additions & 0 deletions Frends.Community.Email.Tests/SendExchangeEmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
44 changes: 13 additions & 31 deletions Frends.Community.Email/EmailTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ private static async Task<Output> SendExchangeEmailWithAttachments(Attachment[]
var attachmentList = new MessageAttachmentsCollectionPage();
var allAttachmentFilePaths = new List<string>();

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)
{

Expand Down Expand Up @@ -248,17 +259,6 @@ private static async Task<Output> 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();
Expand All @@ -279,11 +279,12 @@ private static async Task<Output> SendExchangeEmailWithAttachments(Attachment[]
var largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(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,
Expand Down Expand Up @@ -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
}
}
5 changes: 1 addition & 4 deletions Frends.Community.Email/Frends.Community.Email.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IncludeSource>true</IncludeSource>
<PackageTags>Frends</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>4.2.0</Version>
<Version>4.2.1</Version>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
</PropertyGroup>

Expand All @@ -23,12 +23,9 @@
<ItemGroup>
<PackageReference Include="MailKit" Version="[2.5.2]" />
<PackageReference Include="MimeKit" Version="2.9.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.8" />
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="Azure.Identity" Version="1.6.0" />
<PackageReference Include="Microsoft.Graph" Version="4.35.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion Frends.Community.Email/ReadEmailTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.1 | SendEmailToExchangeServer: Fixed issue which prevented adding multiple attachment to email. |

0 comments on commit 11cf42b

Please sign in to comment.