views:

313

answers:

1

I have a C# application which emails out Excel spreadsheet reports via an Exchange 2007 server using SMTP. These arrive fine for Outlook users, but for Thunderbird and Blackberry users the attachments have been renamed as "Part 1.2".

I found this article which describes the problem, but doesn't seem to give me a workaround. I don't have control of the Exchange server so can't make changes there. Is there anything I can do on the C# end? I have tried using short filenames and HTML encoding for the body but neither made a difference.

My mail sending code is simply this:

    public static void SendMail(string recipient, string subject, string body, string attachmentFilename)
    {
        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password);
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(MailConst.Username);

        // setup up the host, increase the timeout to 5 minutes
        smtpClient.Host = MailConst.SmtpServer;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        smtpClient.Timeout = (60 * 5 * 1000);

        message.From = fromAddress;
        message.Subject = subject;
        message.IsBodyHtml = false;
        message.Body = body;
        message.To.Add(recipient);

        if (attachmentFilename != null)
            message.Attachments.Add(new Attachment(attachmentFilename));

        smtpClient.Send(message);
    }

Thanks for any help.

+2  A: 

Explicitly filling in the ContentDisposition fields did the trick.

        if (attachmentFilename != null)
        {
            Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = attachment.ContentDisposition;
            disposition.CreationDate = File.GetCreationTime(attachmentFilename);
            disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
            disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
            disposition.FileName = Path.GetFileName(attachmentFilename);
            disposition.Size = new FileInfo(attachmentFilename).Length;
            disposition.DispositionType = DispositionTypeNames.Attachment;
            message.Attachments.Add(attachment);                
        }
Jon