diff --git a/MailMergeLib/HtmlBodyBuilder.cs b/MailMergeLib/HtmlBodyBuilder.cs
index 2bf63f7..7d8d2e0 100644
--- a/MailMergeLib/HtmlBodyBuilder.cs
+++ b/MailMergeLib/HtmlBodyBuilder.cs
@@ -24,6 +24,7 @@ internal class HtmlBodyBuilder : BodyBuilderBase
private readonly IHtmlDocument _htmlDocument;
private Uri _docBaseUri;
private readonly object _dataItem;
+ private readonly string _defaultDocBaseUri = new Uri(string.Concat(UriScheme.File, UriScheme.SchemeDelimiter)).ToString();
///
/// Constructor.
@@ -32,7 +33,7 @@ internal class HtmlBodyBuilder : BodyBuilderBase
///
public HtmlBodyBuilder(MailMergeMessage mailMergeMessage, object dataItem)
{
- _docBaseUri = new Uri(string.Concat(UriScheme.File, UriScheme.SchemeDelimiter));
+ DocBaseUri = mailMergeMessage.Config.FileBaseDirectory;
_mailMergeMessage = mailMergeMessage;
_dataItem = dataItem;
BinaryTransferEncoding = mailMergeMessage.Config.BinaryTransferEncoding;
@@ -78,7 +79,7 @@ public override MimeEntity GetBodyPart()
var baseDir = baseEle?.Href == null ? null : new Uri(baseEle.Href);
// only replace the base url if it was not set programmatically
- if (_docBaseUri == null)
+ if (baseDir != null && _docBaseUri == new Uri(_defaultDocBaseUri))
{
_docBaseUri = baseDir;
}
@@ -151,8 +152,11 @@ public override MimeEntity GetBodyPart()
///
public string DocBaseUri
{
- set { _docBaseUri = new Uri(string.IsNullOrEmpty(value) ? string.Concat(UriScheme.File, UriScheme.SchemeDelimiter) : value); }
- get { return _docBaseUri.AbsolutePath; }
+ set
+ {
+ _docBaseUri = new Uri(string.IsNullOrEmpty(value) ? string.Concat(UriScheme.File, UriScheme.SchemeDelimiter) : value);
+ }
+ get => _docBaseUri.ToString();
}
///
diff --git a/MailMergeLib/MailMergeLib.csproj b/MailMergeLib/MailMergeLib.csproj
index e87ea43..d568fa4 100644
--- a/MailMergeLib/MailMergeLib.csproj
+++ b/MailMergeLib/MailMergeLib.csproj
@@ -1,11 +1,12 @@
- MailMergeLib is an SMTP mail client library which provides comfortable mail merge capabilities for text, inline images and attachments, as well as good throughput and fault tolerance for sending mail messages.
+ MailMergeLib is a mail client library which provides comfortable mail merge capabilities for text, inline images and attachments, as well as good throughput and fault tolerance for sending mail messages.
© 2007-2019 by axuno gGmbH
- MailMergeLib 5
- 5.6.0.0
- 5.5.1.0
+ MailMergeLib
+ 5.6.1.0
+ 5.6.1.0
+ 5.6.1.0
axuno gGmbH
netstandard1.6;netstandard2.0;net45
TRACE;DEBUG;RELEASE
@@ -19,7 +20,8 @@
https://github.com/axuno/MailMergeLib/raw/master/MailMergeLib_64x64.png
https://github.com/axuno/MailMergeLib/blob/master/MailMergeLib/License.txt
https://github.com/axuno/MailMergeLib
- * Classes in namespace 'MailMergLib.SmartFormatMail' are obsolete. Use namespace 'SmartFormat' from dependency 'SmartFormat.Net' instead.
+ * Reverted back to v5.5.0 behavior: MessageConfig.FileBaseDirectory must be a full path only before the MailMergeMessage is processed (not already, when the property is set).
+* Classes in namespace 'MailMergLib.SmartFormatMail' are obsolete. Use namespace 'SmartFormat' from dependency 'SmartFormat.Net' instead.
* This is the last version which supports netstandard1.6
* Updated versions of dependencies
smtp mime mail email merge template netcore netstandard net45 c#
@@ -27,8 +29,6 @@
True
MailMergeLib
latest
- 5.6.0.0
- 5.6.0.0
diff --git a/MailMergeLib/MailMergeMessage.cs b/MailMergeLib/MailMergeMessage.cs
index d67c872..0825fdd 100644
--- a/MailMergeLib/MailMergeMessage.cs
+++ b/MailMergeLib/MailMergeMessage.cs
@@ -349,7 +349,7 @@ internal string SearchAndReplaceVars(string text, object dataItem)
{
return SmartFormatter.Format(Config?.CultureInfo, text, dataItem);
}
- catch (SmartFormat.Core.Parsing.ParsingErrors ex)
+ catch (SmartFormat.Core.Parsing.ParsingErrors)
{
return text;
}
@@ -649,11 +649,32 @@ public IEnumerable GetMimeMessages(IEnumerable dataSource)
AddAddressesToMailMessage(mimeMessage, dataItem);
AddAttributesToMailMessage(mimeMessage, dataItem);
- BuildTextMessagePart(dataItem);
- BuildAttachmentPartsForMessage(dataItem);
-
var exceptions = new List();
+ if (Config.FileBaseDirectory != string.Empty && !Tools.IsFullPath(Config.FileBaseDirectory))
+ {
+ exceptions.Add(new DirectoryNotFoundException(
+ $"'{nameof(Config)}.{nameof(Config.FileBaseDirectory)}' is not a full path."));
+ }
+
+ try
+ {
+ BuildTextMessagePart(dataItem);
+ }
+ catch (Exception e)
+ {
+ exceptions.Add(e);
+ }
+
+ try
+ {
+ BuildAttachmentPartsForMessage(dataItem);
+ }
+ catch (Exception e)
+ {
+ exceptions.Add(e);
+ }
+
if (mimeMessage.To.Count == 0 && mimeMessage.Cc.Count == 0 && mimeMessage.Bcc.Count == 0)
exceptions.Add(new AddressException("No recipients.", _badMailAddr, null));
if (string.IsNullOrWhiteSpace(mimeMessage.From.ToString()))
diff --git a/MailMergeLib/MailMergeSender.cs b/MailMergeLib/MailMergeSender.cs
index 53e7a7d..1e5b9a2 100644
--- a/MailMergeLib/MailMergeSender.cs
+++ b/MailMergeLib/MailMergeSender.cs
@@ -29,8 +29,7 @@ public MailMergeSender()
}
///
- /// Renews the in a way that it renews after
- /// the has canceled.
+ /// Renews the . The method is called after the "Send" methods have completed.
///
///
/// This allows the current instance of a can be reused after a cancellation.
@@ -40,7 +39,6 @@ public MailMergeSender()
private void RenewCancellationTokenSource()
{
_cancellationTokenSource = new CancellationTokenSource();
- _cancellationTokenSource.Token.Register(RenewCancellationTokenSource);
}
///
@@ -195,6 +193,7 @@ void AfterSend(object obj, MailSenderAfterSendEventArgs args)
OnAfterSend -= AfterSend;
+ RenewCancellationTokenSource();
IsBusy = false;
}
}
@@ -269,6 +268,7 @@ await Task.Run(async () =>
}
finally
{
+ RenewCancellationTokenSource();
IsBusy = false;
}
}
@@ -583,6 +583,7 @@ public void Send(MailMergeMessage mailMergeMessage, IEnumerable dataSource
}
finally
{
+ RenewCancellationTokenSource();
IsBusy = false;
}
}
@@ -654,6 +655,7 @@ public void Send(MailMergeMessage mailMergeMessage, object dataItem)
}
finally
{
+ RenewCancellationTokenSource();
IsBusy = false;
}
}
diff --git a/MailMergeLib/MessageConfig.cs b/MailMergeLib/MessageConfig.cs
index 6177c4b..855d902 100644
--- a/MailMergeLib/MessageConfig.cs
+++ b/MailMergeLib/MessageConfig.cs
@@ -68,8 +68,9 @@ private string CultureInfoName
///
/// Gets or sets the local base directory of HTML linked resources and other attachments.
- /// The must be a an absolute path, while *file paths are relative* to the .
+ /// The must be a an absolute path *when processing the message* (not when setting the value), while *file paths are relative* to the .
/// It is useful for retrieval of inline attachments (linked resources of the HTML body).
+ /// Defaults to .
///
[YAXSerializableField]
public string FileBaseDirectory
@@ -79,15 +80,9 @@ public string FileBaseDirectory
{
if (string.IsNullOrWhiteSpace(value))
{
- _fileBaseDirectory = Path.GetTempPath();
+ _fileBaseDirectory = string.Empty;
return;
}
-
- if (!Tools.IsFullPath(value))
- {
- throw new ArgumentException($"{value} is not a full path for property {nameof(FileBaseDirectory)}.");
- }
-
_fileBaseDirectory = value;
}
}
diff --git a/UnitTests/HtmlBodyBuilderTest.cs b/UnitTests/HtmlBodyBuilderTest.cs
new file mode 100644
index 0000000..61ccd7e
--- /dev/null
+++ b/UnitTests/HtmlBodyBuilderTest.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MailMergeLib;
+using NUnit.Framework;
+
+namespace UnitTests
+{
+ [TestFixture]
+ public class HtmlBodyBuilderTest
+ {
+ [TestCase("Temp")]
+ [TestCase("..\\..\\temp")]
+ public void SetHtmlBuilderDocBaseUri_UriFormatException(string baseUri)
+ {
+ var mmm = new MailMergeMessage("subject", "plain text", "");
+ var hbb = new HtmlBodyBuilder(mmm, (object)null);
+ Assert.Throws(() => hbb.DocBaseUri = baseUri);
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("C:\\Temp")]
+ [TestCase("\\\\some\\unc\\path")]
+ public void SetHtmlBuilderDocBaseUri_NoException(string baseUri)
+ {
+ var mmm = new MailMergeMessage("subject", "plain text", "");
+ var hbb = new HtmlBodyBuilder(mmm, (object)null);
+ Assert.DoesNotThrow(() => hbb.DocBaseUri = baseUri);
+ }
+
+ [Test]
+ public void ScriptTagRemoved()
+ {
+ var mmm = new MailMergeMessage("subject_to_set", "plain text", "some body");
+ var hbb = new HtmlBodyBuilder(mmm, (object)null);
+ var html = hbb.GetBodyPart();
+ Assert.IsTrue(html.ToString().Contains("some body"));
+ Assert.IsTrue(!html.ToString().Contains("script"));
+ }
+
+ [Test]
+ public void ExistingTitleTagSetWithSubject()
+ {
+ var subjectToSet = "subject_to_set";
+ var mmm = new MailMergeMessage(subjectToSet, "plain text", "abc");
+ var hbb = new HtmlBodyBuilder(mmm, (object)null);
+ var html = hbb.GetBodyPart();
+ Assert.IsTrue(html.ToString().Contains(subjectToSet));
+ }
+
+ [Test]
+ public void NonExistingTitleTagSetWithSubject()
+ {
+ var subjectToSet = "subject_to_set";
+ var mmm = new MailMergeMessage(subjectToSet, "plain text", "");
+ var hbb = new HtmlBodyBuilder(mmm, (object)null);
+ var html = hbb.GetBodyPart();
+ Assert.IsTrue(!html.ToString().Contains(subjectToSet));
+ }
+ }
+}
diff --git a/UnitTests/Message_Config.cs b/UnitTests/Message_Config.cs
index 23d1aa3..30aa6d6 100644
--- a/UnitTests/Message_Config.cs
+++ b/UnitTests/Message_Config.cs
@@ -9,25 +9,109 @@ namespace UnitTests
public class Message_Config
{
private MessageConfig _msgConfig = new MessageConfig();
- private string _tempPath = Path.GetTempPath();
- [TestCase(" \t", "?")]
- [TestCase(" ", "?")]
- [TestCase("", "?")]
- [TestCase(null, "?")]
- [TestCase("\\noFullPath", null)]
+ [TestCase(" \t", "")]
+ [TestCase(" ", "")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ [TestCase("noFullPath", "noFullPath")]
[TestCase("C:\\some\\path\\to\\folder", "C:\\some\\path\\to\\folder")]
public void SetFileBaseDirectory(string path, string expected)
{
- if (expected == "?") expected = Path.GetTempPath();
+ _msgConfig.FileBaseDirectory = path;
+ Assert.AreEqual(expected, _msgConfig.FileBaseDirectory);
+ }
+
+ [TestCase(" \t", false)]
+ [TestCase(" ", false)]
+ [TestCase("", false)]
+ [TestCase(null, false)]
+ [TestCase("C:\\some\\path\\to\\folder", false)]
+ [TestCase("\\\\some\\unc\\path", false)]
+ [TestCase("noFullPath", true)]
+ [TestCase("..\\..\\relativePath", true)]
+ public void FileBaseDirectory_must_be_full_path_when_processing_the_message(string path, bool shouldThrow)
+ {
+ var mmm = new MailMergeMessage("subject", "plain text", "");
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "to@example.org"));
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.org"));
+ mmm.Config.FileBaseDirectory = path;
+
+ if (shouldThrow)
+ {
+ try
+ {
+ mmm.GetMimeMessage(null);
+ }
+ catch (Exception e)
+ {
+ Assert.IsTrue(e is MailMergeMessage.MailMergeMessageException);
+ Assert.IsTrue(e.InnerException != null);
+ }
+ }
+ else
+ {
+ Assert.DoesNotThrow(() => mmm.GetMimeMessage(null));
+ }
+ }
+
+ [TestCase(" \t", "file:///")]
+ [TestCase(" ", "file:///")]
+ [TestCase("", "file:///")]
+ [TestCase("C:\\some\\path\\to\\folder", "file:///C:/some/path/to/folder")]
+ [TestCase("\\\\some\\unc\\path", "file://some/unc/path")]
+ [TestCase("noFullPath", null)]
+ [TestCase("..\\..\\relativePath", null)]
+ public void HtmlBodyBuilderDocBaseUri_vs_MessageConfig_FileBaseDirectory(string path, string expected)
+ {
+ var mmm = new MailMergeMessage("subject", "plain text", "");
+ mmm.Config.FileBaseDirectory = path;
+
+ HtmlBodyBuilder hbb;
if (expected == null)
{
- Assert.Throws(() => _msgConfig.FileBaseDirectory = path);
- return;
+ Assert.Throws(() => { hbb = new HtmlBodyBuilder(mmm, (object) null); });
}
+ else
+ {
+ hbb = new HtmlBodyBuilder(mmm, (object) null);
+ Assert.AreEqual(expected, hbb.DocBaseUri);
+ }
+ }
- _msgConfig.FileBaseDirectory = path;
- Assert.AreEqual(expected, _msgConfig.FileBaseDirectory);
+ [Test]
+ public void MessageConfig_FileBaseDirectory_cannot_be_changed_by_Html_Base_Tag()
+ {
+ var mmm = new MailMergeMessage("subject", "plain text",
+ "");
+ mmm.Config.FileBaseDirectory = Path.GetTempPath();
+
+ var hbb = new HtmlBodyBuilder(mmm, (object) null);
+ Assert.AreEqual(new Uri(mmm.Config.FileBaseDirectory), hbb.DocBaseUri);
+ }
+
+ [Test]
+ public void Empty_MessageConfig_FileBaseDirectory_is_changed_by_Html_Base_Tag()
+ {
+ var baseTagHref = "file:///C:/Temp/";
+ var mmm = new MailMergeMessage("subject", "plain text",
+ $"");
+ mmm.Config.FileBaseDirectory = string.Empty;
+
+ var hbb = new HtmlBodyBuilder(mmm, (object) null);
+ hbb.GetBodyPart();
+ Assert.AreEqual(baseTagHref, hbb.DocBaseUri);
+ }
+
+ [Test]
+ public void HashCode()
+ {
+ var mc1 = new MessageConfig();
+ var mc2 = new MessageConfig();
+
+ Assert.AreEqual(mc1.GetHashCode(), mc2.GetHashCode());
+ Assert.AreEqual(mc1.GetHashCode(), mc1.GetHashCode());
+ Assert.AreEqual(mc2.GetHashCode(), mc2.GetHashCode());
}
}
}
diff --git a/UnitTests/Message_Html.cs b/UnitTests/Message_Html.cs
index 07d7fd9..546c6d8 100644
--- a/UnitTests/Message_Html.cs
+++ b/UnitTests/Message_Html.cs
@@ -9,6 +9,7 @@
using MailMergeLib;
using MimeKit;
using NUnit.Framework;
+using SmartFormat.Core.Settings;
namespace UnitTests
{
@@ -24,6 +25,38 @@ public string ToPlainText(string html)
}
}
+ [Test]
+ public void MimeMessageSize()
+ {
+ var mmm = new MailMergeMessage("subject", "plain text", "some body");
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "to@example.org"));
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.org"));
+ var mimeMessage = mmm.GetMimeMessage(null);
+
+ var size = MailMergeLib.Tools.CalcMessageSize(mimeMessage);
+ Assert.IsTrue(size > 0);
+
+ Assert.IsTrue(MailMergeLib.Tools.CalcMessageSize(null) == 0);
+ }
+
+ [Test]
+ public void EmptyContent()
+ {
+ var mmm = new MailMergeMessage();
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "to@example.org"));
+ mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "from@example.org"));
+
+ try
+ {
+ mmm.GetMimeMessage(null);
+ }
+ catch (Exception e)
+ {
+ Assert.IsTrue(e is MailMergeMessage.MailMergeMessageException);
+ Assert.IsTrue(e.InnerException is MailMergeMessage.EmtpyContentException);
+ }
+ }
+
[Test]
public void CreateTextMessageWithFileAttachments()
{
@@ -262,5 +295,43 @@ public void ConvertHtmlToPlainText()
mmm.ConvertHtmlToPlainText(new DummyHtmlConverter());
Assert.AreEqual(DummyHtmlConverter.ConstantText, mmm.PlainText);
}
+
+ [TestCase("{Name} {SenderAddr}", "John test@specimen.com")]
+ [TestCase("{Name {SenderAddr}", "{Name {SenderAddr}")] // parsing error
+ [TestCase("{NotExisting}", "{NotExisting}")] // formatting error
+ [TestCase(null, null)]
+ public void SearchAndReplace(string text, string expected)
+ {
+ var dataItem = new
+ {
+ Name = "John",
+ SenderAddr = "test@specimen.com",
+ };
+
+ var mmm = new MailMergeMessage();
+ mmm.Config.SmartFormatterConfig.FormatErrorAction = ErrorAction.ThrowError;
+ mmm.Config.SmartFormatterConfig.ParseErrorAction = ErrorAction.ThrowError;
+ var result = mmm.SearchAndReplaceVars(text, dataItem);
+ Assert.AreEqual(expected, result);
+ }
+
+ [TestCase("{Name} {SenderAddr}", "John test@specimen.com")]
+ [TestCase("{Name {SenderAddr}", "{Name {SenderAddr}")] // parsing error
+ [TestCase("{NotExisting}", "{NotExisting}")] // formatting error
+ [TestCase(null, null)]
+ public void SearchAndReplaceFilename(string text, string expected)
+ {
+ var dataItem = new
+ {
+ Name = "John",
+ SenderAddr = "test@specimen.com",
+ };
+
+ var mmm = new MailMergeMessage();
+ mmm.Config.SmartFormatterConfig.FormatErrorAction = ErrorAction.ThrowError;
+ mmm.Config.SmartFormatterConfig.ParseErrorAction = ErrorAction.ThrowError;
+ var result = mmm.SearchAndReplaceVarsInFilename(text, dataItem);
+ Assert.AreEqual(expected, result);
+ }
}
}
diff --git a/UnitTests/Properties/AssemblyInfo.cs b/UnitTests/Properties/AssemblyInfo.cs
index 79f1f68..ed3a2b2 100644
--- a/UnitTests/Properties/AssemblyInfo.cs
+++ b/UnitTests/Properties/AssemblyInfo.cs
@@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyInformationalVersion ("5.6.0.0")]
-[assembly: AssemblyFileVersion ("5.6.0.0")]
-[assembly: AssemblyVersion ("5.6.0.0")]
+[assembly: AssemblyInformationalVersion ("5.6.1.0")]
+[assembly: AssemblyFileVersion ("5.6.1.0")]
+[assembly: AssemblyVersion ("5.6.1.0")]
diff --git a/UnitTests/Sender_Config.cs b/UnitTests/Sender_Config.cs
new file mode 100644
index 0000000..fe3e92e
--- /dev/null
+++ b/UnitTests/Sender_Config.cs
@@ -0,0 +1,31 @@
+using MailMergeLib;
+using NUnit.Framework;
+
+namespace UnitTests
+{
+ [TestFixture]
+ public class Sender_Config
+ {
+ [Test]
+ public void Equality()
+ {
+ var sc1 = new SenderConfig();
+ var sc2 = new SenderConfig();
+
+ Assert.IsTrue(sc1.Equals(sc2));
+ Assert.IsFalse(sc1.Equals(null));
+ Assert.IsFalse(sc1.Equals(new object()));
+ }
+
+ [Test]
+ public void NotEqual()
+ {
+ var sc1 = new SenderConfig();
+ var sc2 = new SenderConfig {MaxNumOfSmtpClients = 99999 };
+
+ Assert.IsFalse(sc1.Equals(sc2));
+ Assert.IsFalse(sc1.Equals(null));
+ Assert.IsFalse(sc1.Equals(new object()));
+ }
+ }
+}
diff --git a/UnitTests/Sender_EventsAndSend.cs b/UnitTests/Sender_EventsAndSend.cs
index 463888e..bd6f295 100644
--- a/UnitTests/Sender_EventsAndSend.cs
+++ b/UnitTests/Sender_EventsAndSend.cs
@@ -137,14 +137,14 @@ public async Task CancelSendOperation()
var tasks = new [] {
await Task.Factory.StartNew(async () => await mms.SendAsync(mmm, anyData)),
Task.Factory.StartNew(() => mms.SendCancel()),
- Task.Factory.StartNew(() => mms.SendCancel()) // a second cancel operation will
+ Task.Factory.StartNew(() => mms.SendCancel()) // a second cancel operation will just return
};
-
+
Assert.Throws(() =>
{
Task.WaitAll(tasks);
});
-
+
Assert.AreEqual(0, _server.ReceivedEmailCount);
}
diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj
index b7c927b..d2e3f9c 100644
--- a/UnitTests/UnitTests.csproj
+++ b/UnitTests/UnitTests.csproj
@@ -82,6 +82,8 @@
+
+