Reputation: 1759
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
Upvotes: 0
Views: 12179
Reputation: 1759
See: Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird
Content Disposition was the solution for my problem.
Upvotes: 2
Reputation: 11876
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"
Upvotes: 4
Reputation: 11903
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 :).
Upvotes: 0