Skip to content

Commit

Permalink
Bumped version to 5.6.1 - see changes in release comments
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Dec 29, 2018
1 parent a47bde6 commit 06ff591
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 43 deletions.
12 changes: 8 additions & 4 deletions MailMergeLib/HtmlBodyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
/// Constructor.
Expand All @@ -32,7 +33,7 @@ internal class HtmlBodyBuilder : BodyBuilderBase
/// <param name="dataItem"></param>
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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -151,8 +152,11 @@ public override MimeEntity GetBodyPart()
/// </summary>
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();
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions MailMergeLib/MailMergeLib.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>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.</Description>
<Description>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.</Description>
<Copyright>© 2007-2019 by axuno gGmbH</Copyright>
<AssemblyTitle>MailMergeLib 5</AssemblyTitle>
<Version>5.6.0.0</Version>
<VersionPrefix>5.5.1.0</VersionPrefix>
<AssemblyTitle>MailMergeLib</AssemblyTitle>
<Version>5.6.1.0</Version>
<AssemblyVersion>5.6.1.0</AssemblyVersion>
<FileVersion>5.6.1.0</FileVersion>
<Authors>axuno gGmbH</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>TRACE;DEBUG;RELEASE</DefineConstants>
Expand All @@ -19,16 +20,15 @@
<PackageIconUrl>https://github.com/axuno/MailMergeLib/raw/master/MailMergeLib_64x64.png</PackageIconUrl>
<PackageLicenseUrl>https://github.com/axuno/MailMergeLib/blob/master/MailMergeLib/License.txt</PackageLicenseUrl>
<RepositoryUrl>https://github.com/axuno/MailMergeLib</RepositoryUrl>
<PackageReleaseNotes>* Classes in namespace 'MailMergLib.SmartFormatMail' are obsolete. Use namespace 'SmartFormat' from dependency 'SmartFormat.Net' instead.
<PackageReleaseNotes>* 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</PackageReleaseNotes>
<PackageTags>smtp mime mail email merge template netcore netstandard net45 c#</PackageTags>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<RootNamespace>MailMergeLib</RootNamespace>
<LangVersion>latest</LangVersion>
<AssemblyVersion>5.6.0.0</AssemblyVersion>
<FileVersion>5.6.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
29 changes: 25 additions & 4 deletions MailMergeLib/MailMergeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -649,11 +649,32 @@ public IEnumerable<MimeMessage> GetMimeMessages<T>(IEnumerable<T> dataSource)
AddAddressesToMailMessage(mimeMessage, dataItem);
AddAttributesToMailMessage(mimeMessage, dataItem);

BuildTextMessagePart(dataItem);
BuildAttachmentPartsForMessage(dataItem);

var exceptions = new List<Exception>();

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()))
Expand Down
8 changes: 5 additions & 3 deletions MailMergeLib/MailMergeSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public MailMergeSender()
}

/// <summary>
/// Renews the <see cref="CancellationTokenSource"/> in a way that it renews after
/// the <see cref="CancellationToken"/> has canceled.
/// Renews the <see cref="CancellationTokenSource"/>. The method is called after the "Send" methods have completed.
/// </summary>
/// <remarks>
/// This allows the current instance of a <see cref="MailMergeSender"/> can be reused after a cancellation.
Expand All @@ -40,7 +39,6 @@ public MailMergeSender()
private void RenewCancellationTokenSource()
{
_cancellationTokenSource = new CancellationTokenSource();
_cancellationTokenSource.Token.Register(RenewCancellationTokenSource);
}

/// <summary>
Expand Down Expand Up @@ -195,6 +193,7 @@ void AfterSend(object obj, MailSenderAfterSendEventArgs args)

OnAfterSend -= AfterSend;

RenewCancellationTokenSource();
IsBusy = false;
}
}
Expand Down Expand Up @@ -269,6 +268,7 @@ await Task.Run(async () =>
}
finally
{
RenewCancellationTokenSource();
IsBusy = false;
}
}
Expand Down Expand Up @@ -583,6 +583,7 @@ public void Send<T>(MailMergeMessage mailMergeMessage, IEnumerable<T> dataSource
}
finally
{
RenewCancellationTokenSource();
IsBusy = false;
}
}
Expand Down Expand Up @@ -654,6 +655,7 @@ public void Send(MailMergeMessage mailMergeMessage, object dataItem)
}
finally
{
RenewCancellationTokenSource();
IsBusy = false;
}
}
Expand Down
11 changes: 3 additions & 8 deletions MailMergeLib/MessageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ private string CultureInfoName

/// <summary>
/// Gets or sets the local base directory of HTML linked resources and other attachments.
/// The <see cref="FileBaseDirectory"/> must be a an absolute path, while *file paths are relative* to the <see cref="FileBaseDirectory"/>.
/// The <see cref="FileBaseDirectory"/> must be a an absolute path *when processing the message* (not when setting the value), while *file paths are relative* to the <see cref="FileBaseDirectory"/>.
/// It is useful for retrieval of inline attachments (linked resources of the HTML body).
/// Defaults to <see cref="string.Empty"/>.
/// </summary>
[YAXSerializableField]
public string FileBaseDirectory
Expand All @@ -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;
}
}
Expand Down
64 changes: 64 additions & 0 deletions UnitTests/HtmlBodyBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -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", "<html><head><base href=\"\" /></head><body></body></html>");
var hbb = new HtmlBodyBuilder(mmm, (object)null);
Assert.Throws<UriFormatException>(() => 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", "<html><head><base href=\"\" /></head><body></body></html>");
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", "<html><head><script>var x='x';</script><script>var y='y';</script></head><body>some body</body></html>");
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", "<html><head><title>abc</title></head><body></body></html>");
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", "<html><head></head><body></body></html>");
var hbb = new HtmlBodyBuilder(mmm, (object)null);
var html = hbb.GetBodyPart();
Assert.IsTrue(!html.ToString().Contains(subjectToSet));
}
}
}
106 changes: 95 additions & 11 deletions UnitTests/Message_Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<html><body></body></html>");
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", "<html><body></body></html>");
mmm.Config.FileBaseDirectory = path;

HtmlBodyBuilder hbb;
if (expected == null)
{
Assert.Throws<ArgumentException>(() => _msgConfig.FileBaseDirectory = path);
return;
Assert.Throws<UriFormatException>(() => { 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",
"<html><head><base href=\"\" /></head><body></body></html>");
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",
$"<html><head><base href=\"{baseTagHref}\" /></head><body></body></html>");
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());
}
}
}
Loading

0 comments on commit 06ff591

Please sign in to comment.