Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-65: Fixed sending of multiple string attachments in Exchange emails #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 116 additions & 28 deletions Frends.Community.Email.Tests/SendExchangeEmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using File = System.IO.File;
Expand All @@ -18,6 +19,7 @@ public class SendExchangeEmailTests
private readonly string _password = Environment.GetEnvironmentVariable("Exchange_User_Password");
private readonly string _applicationID = Environment.GetEnvironmentVariable("Exchange_Application_ID");
private readonly string _tenantID = Environment.GetEnvironmentVariable("Exchange_Tenant_ID");
private readonly string _attachmentDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testAttachments");
private static ExchangeServer _server;

[OneTimeSetUp]
Expand All @@ -37,6 +39,23 @@ public void OneTimeSetup()
AppId = _applicationID,
TenantId = _tenantID
};

if (System.IO.Directory.Exists(_attachmentDirectory))
{
DeleteAttachmentDirectory();
}
}

[SetUp]
public void CreateAttachmentDirectory()
{
System.IO.Directory.CreateDirectory(_attachmentDirectory);
}

[TearDown]
public void DeleteAttachmentDirectory()
{
System.IO.Directory.Delete(_attachmentDirectory, true);
}

[Test]
Expand All @@ -54,8 +73,8 @@ public async Task SendEmailWithPlainTextTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual(email[0].BodyText, message);
await DeleteMessages(subject);
}
Expand All @@ -74,8 +93,8 @@ public async Task SendEmailToMultipleUsingSemicolonTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual(_username + ", " + _username, email[0].To);
await DeleteMessages(subject);
}
Expand All @@ -94,8 +113,8 @@ public async Task SendEmailToMultipleUsingCommaTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual(_username + ", " + _username, email[0].To);
await DeleteMessages(subject);
}
Expand All @@ -115,8 +134,8 @@ public async Task SendEmailWithCCTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual(_username, email[0].Cc);
await DeleteMessages(subject);
}
Expand All @@ -137,8 +156,8 @@ public async Task SendEmailWithHtmlTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.IsTrue(email[0].BodyHtml.Contains(message));
await DeleteMessages(subject);
}
Expand All @@ -157,8 +176,8 @@ public async Task SendEmailNordicLettersTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails with correct subject found in inbox.");
Assert.AreEqual("Tämä testimaili tuo yöllä ålannista.", email[0].BodyText);
Assert.AreEqual(subject, email[0].Subject);
await DeleteMessages(subject);
Expand All @@ -168,7 +187,7 @@ public async Task SendEmailNordicLettersTest()
public async Task SendEmailWithFileAttachmentTest()
{
var subject = "Email test - FileAttachment";
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../attachmentFile.txt");
var filePath = Path.Combine(_attachmentDirectory, "attachmentFile.txt");
File.WriteAllText(filePath, "This is a test attachment file.");
var input = new ExchangeInput
{
Expand All @@ -191,8 +210,8 @@ public async Task SendEmailWithFileAttachmentTest()
var result = await EmailTask.SendEmailToExchangeServer(input, attachmentArray, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
File.Delete(filePath);
Thread.Sleep(2000); // Give the email some time to get through.
await ReadTestEmailWithAttachment(subject);
var email = await ReadTestEmailWithAttachment(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.IsTrue(File.Exists(filePath));
File.Delete(filePath);
await DeleteMessages(subject);
Expand All @@ -202,9 +221,9 @@ public async Task SendEmailWithFileAttachmentTest()
public async Task SendEmailWithMultipleFileAttachmentTest()
{
var subject = "Email test - MultiFileAttachment";
var filePath1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../first.txt");
var filePath1 = Path.Combine(_attachmentDirectory, "first.txt");
File.WriteAllText(filePath1, "This is a test attachment file.");
var filePath2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../second.txt");
var filePath2 = Path.Combine(_attachmentDirectory, "second.txt");
File.WriteAllText(filePath2, "This is a test attachment file.");
var input = new ExchangeInput
{
Expand All @@ -217,7 +236,7 @@ public async Task SendEmailWithMultipleFileAttachmentTest()
var attachment = new Attachment
{
AttachmentType = AttachmentType.FileAttachment,
FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../*.txt"),
FilePath = Path.Combine(_attachmentDirectory, "*.txt"),
ThrowExceptionIfAttachmentNotFound = false,
SendIfNoAttachmentsFound = false
};
Expand All @@ -228,15 +247,16 @@ public async Task SendEmailWithMultipleFileAttachmentTest()
Assert.IsTrue(result.EmailSent);
File.Delete(filePath1);
File.Delete(filePath2);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
await DeleteMessages(subject);
}

[Test]
public async Task SendEmailWithBigFileAttachmentTest()
{
var subject = "Email test - BigFileAttachment";
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../BigAttachmentFile.txt");
var filePath = Path.Combine(_attachmentDirectory, "BigAttachmentFile.txt");

// Write 9MB file.
var stream = new FileStream(filePath, FileMode.CreateNew);
Expand Down Expand Up @@ -265,8 +285,8 @@ public async Task SendEmailWithBigFileAttachmentTest()
var result = await EmailTask.SendEmailToExchangeServer(input, attachmentArray, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
File.Delete(filePath);
Thread.Sleep(2000); // Give the email some time to get through.
await ReadTestEmailWithAttachment(subject);
var email = await ReadTestEmailWithAttachment(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.IsTrue(File.Exists(filePath));
File.Delete(filePath);
await DeleteMessages(subject);
Expand All @@ -275,7 +295,7 @@ public async Task SendEmailWithBigFileAttachmentTest()
[Test]
public async Task SendEmailWithStringAttachmentTest()
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../stringAttachmentFile.txt");
var filePath = Path.Combine(_attachmentDirectory, "stringAttachmentFile.txt");
var subject = "Email test - StringAttachment";
var input = new ExchangeInput
{
Expand All @@ -301,13 +321,65 @@ public async Task SendEmailWithStringAttachmentTest()

var result = await EmailTask.SendEmailToExchangeServer(input, attachmentArray, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
await ReadTestEmailWithAttachment(subject);
var email = await ReadTestEmailWithAttachment(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.IsTrue(File.Exists(filePath));
File.Delete(filePath);
await DeleteMessages(subject);
}

[Test]
public async Task SendEmailWithTwoStringAttachmentsTest()
{
var attachments = new Attachment[]
{
new Attachment
{
AttachmentType = AttachmentType.AttachmentFromString,
StringAttachment = new AttachmentFromString
{
FileContent = "Test content 1.",
FileName = "stringAttachmentFile1.txt"
}
},
new Attachment
{
AttachmentType = AttachmentType.AttachmentFromString,
StringAttachment = new AttachmentFromString
{
FileContent = "Test file content 2.",
FileName = "stringAttachmentFile2.txt"
}
}
};
var subject = "Email test - StringAttachment";

var result = await EmailTask.SendEmailToExchangeServer(
new ExchangeInput
{
To = _username,
Message = "This email has two attachments written from a string.",
IsMessageHtml = false,
Subject = subject
},
attachments,
_server,
new CancellationToken()
);
Assert.IsTrue(result.EmailSent);
var email = await ReadTestEmailWithAttachment(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
foreach (var attachment in attachments)
{
var fileName = attachment.StringAttachment.FileName;
var filePath = Path.Combine(_attachmentDirectory, fileName);
Assert.IsTrue(File.Exists(filePath), $"File {fileName} not found in attachments.");
Assert.AreEqual(attachment.StringAttachment.FileContent, File.ReadAllText(filePath), $"Attachment {fileName} contents do not match with what was sent.");
File.Delete(filePath);
}
await DeleteMessages(subject);
}

[Test]
public async Task SendEmailWithEmptyAttachmentTest()
{
Expand All @@ -325,8 +397,8 @@ public async Task SendEmailWithEmptyAttachmentTest()

var result = await EmailTask.SendEmailToExchangeServer(input, attachment.ToArray(), _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual(email[0].BodyText, message);
await DeleteMessages(subject);
}
Expand All @@ -347,8 +419,8 @@ public async Task SendEmailAsAnotherUserTest()

var result = await EmailTask.SendEmailToExchangeServer(input, null, _server, new CancellationToken());
Assert.IsTrue(result.EmailSent);
Thread.Sleep(2000); // Give the email some time to get through.
var email = await ReadTestEmail(subject);
Assert.IsTrue(email.Count > 0, "No emails matching subject in inbox.");
Assert.AreEqual("frends_exchange_test_user_2@frends.com", email[0].From);
await DeleteMessages(subject);
}
Expand Down Expand Up @@ -389,13 +461,20 @@ private async Task<List<EmailMessageResult>> ReadTestEmail(string subject)
EmailSubjectFilter = subject
};

var result = await ReadEmailTask.ReadEmailFromExchangeServer(settings, options, new CancellationToken());
return result;
for (var attempt = 0; attempt < 5; ++attempt)
{
var result = await ReadEmailTask.ReadEmailFromExchangeServer(settings, options, new CancellationToken());
if (result.Any())
{
return result;
}
await Task.Delay(2000); // Give the email some time to get through.
}
return new List<EmailMessageResult>();
}

private async Task<List<EmailMessageResult>> ReadTestEmailWithAttachment(string subject)
{
var dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../..");
var settings = new ExchangeSettings
{
TenantId = _tenantID,
Expand All @@ -411,12 +490,21 @@ private async Task<List<EmailMessageResult>> ReadTestEmailWithAttachment(string
GetOnlyUnreadEmails = false,
MarkEmailsAsRead = false,
IgnoreAttachments = false,
AttachmentSaveDirectory = dirPath,
EmailSubjectFilter = subject
AttachmentSaveDirectory = _attachmentDirectory,
EmailSubjectFilter = subject,
FileExistsAction = FileExists.Error
};

var result = await ReadEmailTask.ReadEmailFromExchangeServer(settings, options, new CancellationToken());
return result;
for (var attempt = 0; attempt < 5; ++attempt)
{
var result = await ReadEmailTask.ReadEmailFromExchangeServer(settings, options, new CancellationToken());
if (result.Any())
{
return result;
}
await Task.Delay(2000); // Give the email some time to get through.
}
return new List<EmailMessageResult>();
}

#endregion
Expand Down
Loading