Skip to content

IIS SMTP Server

Norbert Bietsch edited this page Apr 23, 2018 · 3 revisions

IIS SMTP Server has two options for sending mail messages:

  • SMTP protocal
  • IIS Pickup Directory, where files in EML format are stored

While SMTP protocol is fine to be used with MailMergeLib, IIS Pickup Directory has the following issue:

IIS SMTP Server expects that - if a line in an EML file starts with a dot - this dot must be doubled:

Start of line Effect
.some content the single dot will be 'eaten up' while IIS SMTP Server sends the file
..some content the two dots will become one dot when sending the file

MailMergeLib will produce EML files compliant to the rfc1521 standard, i.e. without "dot stuffing".

System.Net.Mail.SmtpClient and IIS SMTP Server are a "couple" in this respect. System.Net.Mail.SmtpClient procudes the EML files with the dot stuffing, that IIS SMTP Servers expects, when SmtpDeliveryMethod.SpecifiedPickupDirectory is chosen.

Solutions

  • only use ContentEncoding.Base64 in MailMergeLib (no dots ever at the beginning of line)
  • use IIS SMTP Server with SMTP protocol (recommended)
  • replace single dots with double dots before transfering EML files to the pickup directory

Reproduce yourself

private static string GetHtmlPart()
{
	var images = new StringBuilder();
	for (var i = 1; i <= 1000; i++)
	{
        // this is the image file posted in this issue earlier
		images.AppendFormat($"<img src=\"https://user-images.githubusercontent.com/10255664/38836490-9f04a8b4-41ce-11e8-9ca3-bf1ca352a40d.gif\" alt=\"{i}\" width=\"50\" height=\"50\" />");
		if (i % 10 == 0) images.Append("<br/>");
	}

	var htmlWithImg =
		"<html>" +
		"<head><title>Send email with images</title></head>" +
		"<body>" + images +
		"</body>" +
		"</html>";

	return htmlWithImg;
}

System.Net.Mail Behavior:

var htmlWithImg = GetHtmlPart();
var mailMsg = new System.Net.Mail.MailMessage("liza@doe.com", "joe@doe.com", "Image Test", "plain text");
var htmlStream = new MemoryStream();
var htmlSw = new StreamWriter(htmlStream);
htmlSw.Write(htmlWithImg);
htmlStream.Position = 0;
mailMsg.AlternateViews.Add(new AlternateView(htmlStream){ContentType = new System.Net.Mime.ContentType("text/html"), TransferEncoding = TransferEncoding.QuotedPrintable });
var smtpClient = new System.Net.Mail.SmtpClient
{
	DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory,
	PickupDirectoryLocation = @"Path-to-output-directory"
};
smtpClient.Send(mailMsg);

When you open this EML file with Thunderbird or Outlook, you will notice that some of the images are not displayed, because dot stuffing breaks the image URLs.

MailMergeLib Behavior

var htmlWithImg = GetHtmlPart();
var mmm = new MailMergeMessage("Image Test", "plain text", htmlWithImg);
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "joe@doe.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "liza@doe.com"));
mmm.Config.TextTransferEncoding = ContentEncoding.QuotedPrintable;

var mms = new MailMergeSender();
mms.Config.SmtpClientConfig[0].MessageOutput = MessageOutput.Directory;
mms.Config.SmtpClientConfig[0].MailOutputDirectory = @"Path-to-output-directory";
mms.Send(mmm, (object) null);

When opening this EML file with Thunderbird or Outlook, it will show all images intact. There is no dot stuffing.