5

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("alice@a.com"));
message.From = new MailAddress("bob@b.com");
message.Subject = "Smtp Dot Stuffing Test";
message.Body = ".A.B.C..... .0.1.2.3.4.5.6.7.8.9";
client.Send(message);
}
Jak Hammond
  • 1,460
  • 3
  • 11
  • 24

2 Answers2

3

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.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Yeah I'm using System.Net.Mail so sounds like it's something broken in there then. Least I wasn't doing anything obviously wrong, I'll give your suggestion a go and see how I get on. Thanks for the advice – Jak Hammond Oct 26 '14 at 01:13
  • Just gave this a go and once I called MimeKits 'WriteTo()' method and checked the resulting eml file it had all formatted fine. Thanks for the tip about Mimekit, pretty useful library! – Jak Hammond Oct 27 '14 at 16:49
0

Actually the "accepted" answer might have been helpful for the original question - because it suggested an alternative lib, but the analysis/explanation was not accurate.

The mail client IS supposed to do dot-stuffing when passing the message body to the SMTP server.

The "pickup" directory is NOT a place where you "save" messages, it's place where HAND OVER messages to be "picked up" by the SMTP server. It's an alternative to using a port 25 connection to hand over the same message to the SMTP server.

A relaying SMTP server is supposed to "unstuff" on the receiving side and "restuff" on the sending side. Consequently, anything handed to the SMTP server MUST already be stuffed for the entire "dot-stuffing" concept to work!