Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.5 SMTP Client Dot Stuffing Issue when Delivering to Pickup Directory

Hoping someone will be able to help out with this, I've been looking round and I can't seem to find an answer anywhere.

I'm creating a mail message using that will be delivered to a specified pickup directory, this code has been used numerous times in the past without issue. Now though, when I inspect the resulting file and more specifically a URL within the eml file, I can see that in the middle there is a double .. From what I've been reading, I understand that this is part of the SMTP protocol to dot stuff if the first character of a line in the message begins with .. This file is later going to be picked up by another service that will ultimately carry out the sending of the email.

I've been able to narrow it down to the exact line when I call client.Send(). If I inspect the message body before the send, the URL is correctly formed. Inspecting the message body after I have called it, there is the .. present in the URL.

My question, or questions I suppose, are as follows:

  1. Has anyone else come across an issue with dot stuffing when using SmtpDeliveryMethod.SpecifiedPickupDirectory?
  2. Whose job is it to correctly handle this? The .NET SMTP or the secondary service that picks this message up at a later date and sends it on to the final destination?
  3. Any advice on how to resolve this?

I have previously tried the approach described here, but it fails with numerous exceptions.

I'm mainly looking for a way to save this eml file to a location on disk that can later be picked back up and sent, my knowledge on C# is still fairly limited so there may be something simple I am just over looking, so any advice or guidance would be hugely appreciated!

I have created a small sample piece of code to try and recreate the issue, this isn't the exact content I'm using, but it does show that after sending via the client.Send() method, there are 2 '..' at the start of the string.

using (var client = new SmtpClient())
{
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\temp";
var message = new MailMessage();
message.To.Add(new MailAddress("[email protected]"));
message.From = new MailAddress("[email protected]");
message.Subject = "Smtp Dot Stuffing Test";
message.Body = ".A.B.C..... .0.1.2.3.4.5.6.7.8.9";
client.Send(message);
}
like image 515
Jak Hammond Avatar asked Oct 20 '22 23:10

Jak Hammond


1 Answers

The SmtpClient should not be byte-stuffing when saving to a file. That is only needed when actually "uploading" the message stream to an SMTP server in order to prevent prematurely ending the DATA command (which terminates with the line: ".\r\n").

If SmtpClient is byte stuffing when saving to a file, that's pretty broken behavior.

Since this is impossible to work around if you are using System.Net.Mail (and I assume you are), I might recommend using MimeKit to create and save your message to a file instead.

Then, if you need to re-parse the message and send it via SMTP, you could use MailKit to do that.

like image 85
jstedfast Avatar answered Oct 22 '22 12:10

jstedfast