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