3

We've had an issue with sending emails that contain attachments with long names and accents in them.

The size or type don't seem to affect the outcome (I tried with txt & pdf, 300kb and 3833kb in size)

After some searching around, I found this post http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55 that show how to get around the problem.

If my name is really long (see attachement2 variable name), it will work. But in the case of a slight shorter name (see attachment1 variable name), the name comes out as -=_iso-8859-1_Q_Example,_Example_and_other_

I think it has to do with the following line somehow in SplitEncodedAttachmentName encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length);

Here's a watch of the vars attach1 - "=?ISO-8859-1?Q?Example=2c_Example_and_other_Repr=e9senta.txt?="

attach2 - "=?ISO-8859-1?Q?Example=2c_Example_and_other_Repr=e9sentant_Example_Examp?==?ISO-8859-1?Q?le_Example_Example_Example_Example2.txt?="

I can't seem to get the attachment1 to be sent properly..

(I did answer this question in the past - MailMessage Attachment filename with accents but I had a bug, refactored and came across this issue..)

Here is the smallest code example I could come up that causes the error.

class Program
{
    static void Main(string[] args)
    {
        Attachment attachment = new Attachment(@"c:\client\temp\Example,_Example_and_other_Représenta.pdf"); //3488kb
        Attachment attachment2 = new Attachment(@"c:\client\temp\Example,_Example_and_other_Représentant_Example_Example_Example_Example_Example_Example.pdf"); //3488kb

        Console.WriteLine(attachment.Name);

        MailMessage mm = new MailMessage();
         mm.From = new MailAddress("toemail");
         mm.To.Add("toemail");
        mm.Subject = "Yo";
        mm.Body = "hello";
        mm.Attachments.Add(CreateAttachment(attachment, attachment.Name)); // =_iso-8859-1_Q_Example,_Example_and_other_
        mm.Attachments.Add(CreateAttachment(attachment2, attachment2.Name));

        SmtpClient smtp = new SmtpClient("SmptServer");
        smtp.Send(mm);
    }

    /// <summary>
    /// This method fixes the name of the attachment to allow accents
    /// </summary>
    /// <remarks>Taken from http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55</remarks>
    public static Attachment CreateAttachment(Attachment attachmentFile, string displayName)
    {
        Attachment attachment = attachmentFile;
        attachment.TransferEncoding = TransferEncoding.Base64;

        string tranferEncodingMarker = "Q";
        string encodingMarker = "ISO-8859-1";
        int maxChunkLength = 76;

        attachment.NameEncoding = Encoding.GetEncoding(encodingMarker);

        string encodingtoken = String.Format("=?{0}?{1}?", encodingMarker, tranferEncodingMarker);
        string softbreak = "?=";
        string encodedAttachmentName = encodingtoken;
        encodedAttachmentName = HttpUtility.UrlEncode(displayName, Encoding.Default).Replace("+", " ").Replace("%", "=");

        encodedAttachmentName = SplitEncodedAttachmentName(encodingtoken, softbreak, maxChunkLength, encodedAttachmentName);
        attachment.Name = encodedAttachmentName;

        return attachment;
    }


    private static IEnumerable<string> SplitByLength(string stringToSplit, int length)
    {
        while (stringToSplit.Length > length)
        {
            yield return stringToSplit.Substring(0, length);
            stringToSplit = stringToSplit.Substring(length);
        }

        if (stringToSplit.Length > 0)
        {
            yield return stringToSplit;
        }
    }

    private static string SplitEncodedAttachmentName(string encodingtoken, string softbreak, int maxChunkLength, string encoded)
    {
        int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2);
        var parts = SplitByLength(encoded, splitLength);

        string encodedAttachmentName = encodingtoken;

        foreach (var part in parts)
        {
            encodedAttachmentName += part + softbreak + encodingtoken;
        }

        encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length);

        return encodedAttachmentName;
    }
}
Community
  • 1
  • 1
Lareau
  • 1,982
  • 1
  • 26
  • 47

1 Answers1

4

It turns out there's a bug report - https://connect.microsoft.com/VisualStudio/feedback/details/696372/filename-encoding-error-when-encoding-utf-8-and-encoded-name-exceeds-the-length-of-a-single-mime-header-line#details

and a kb article to fix the initial issue of long names with special characters. http://support.microsoft.com/kb/2402064

After installing the kb,

I changed

mm.Attachments.Add(CreateAttachment(attachment, attachment.Name)); mm.Attachments.Add(CreateAttachment(attachment2, attachment2.Name));

back to what is should be mm.Attachments.Add(attachment); mm.Attachments.Add(attachment2);

and everything worked..

Lareau
  • 1,982
  • 1
  • 26
  • 47