0

I use a C# code to send some e-mails to different users with mail clients on different platforms: BlackBerry, iPhone, Pc, Mac, etc. Here is the snippet:

Attachment attachment = null;
        if (attachNameFile!=null) 
        {
            attachment = new Attachment(attachNameFile, new System.Net.Mime.ContentType(attachMimeType));
        }

        SmtpClient smtp = new SmtpClient
        {
            Host = this.smtpServer,
            Port = this.smtpPort,
            EnableSsl = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential()
        };

        using (MailMessage message = new MailMessage())
        {
            message.From = fromAddress;
            if (macTo != null) message.To.AddRangeUnique(macTo);
            if (macCc!=null) message.CC.AddRangeUnique(macCc);
            if (macCcn != null) message.Bcc.AddRangeUnique(macCcn);
            message.Subject = subject;
            message.Body = sb.ToString();

            if (replyTo != null)
                message.ReplyTo = new MailAddress(replyTo);
            else
                message.ReplyTo = fromAddress;

            if (attachment!=null)  
            { 
                message.Attachments.Add(attachment);                
            }
            smtp.Send(message);
        }

Some user told me that the message he or she receives doesn't have the attachment. The attachment is a text (UTF8) file. After some analysis, I saw that the attachment is shown in the body of the mail and only some mail clients show it as an attachment. It is not a problem for me, but BlackBerry has some problem with this kind of attachments, because it shows only the body and cut off the attachment. But it works in Google, iMail, Thunderbird, etc. etc.

I analysed the source of the message and I saw the ContentTransferEncoding of the attacchment is 8 bit:

Content-Transfer-Encoding: 8bit
Content-Type: text/plain; name=Attachment.2324333.txt

I think I resolve my problem if I set the ContentTransferEncoding property of the object Attachment in C# to Base64 encoding:

Attachment attachment = null;
        if (attachNameFile!=null) 
        {
            attachment = new Attachment(attachNameFile, new System.Net.Mime.ContentType(attachMimeType));
            attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        }

Do you think it is a good and working approach? Do I have to set other properties?

Thanks to all

robob
  • 1,534
  • 4
  • 20
  • 41

3 Answers3

4

Looking at some of the attachments coming from my BlackBerry, I think you need to add a Content-Disposition header:

Content-Transfer-Encoding: base64
Content-Type: text/plain
Content-Disposition: attachment; filename="myfilename.bin"
Michael Donohue
  • 11,606
  • 5
  • 29
  • 44
2

See: Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

Content Disposition was the solution for my problem.

Community
  • 1
  • 1
robob
  • 1,534
  • 4
  • 20
  • 41
0

Seems to me you're doing everything right, the other mail clients prove it. Try BASE64, there's a good chance BlackBerry will like it, but you can't be sure until you actually see it :).

fejesjoco
  • 11,307
  • 3
  • 31
  • 63
  • 1
    The other client behavior proves they agree with each other, but not necessarily the spec. Via wikipedia: The widely used Mozilla Thunderbird mail client makes its own decisions about which MIME parts should be automatically displayed, ignoring the content-disposition headers in the messages. It also sends out newly composed messages with inline content-disposition for all MIME parts. ... Many mail user agents also send messages with the file name in the name parameter of the content-type header instead of the filename parameter of the content-disposition header. This practice is discouraged. – Michael Donohue Dec 23 '10 at 15:47