邮件附件及其内容传输编码设置(BlackBerry 问题)

发布于 2024-10-09 01:28:29 字数 2147 浏览 3 评论 0原文

我使用 C# 代码通过不同平台上的邮件客户端向不同用户发送一些电子邮件:BlackBerry、iPhone、PC、Mac 等。以下是代码片段:

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);
        }

某些用户告诉我,他或她收到的消息不有附件。附件是文本 (UTF8) 文件。 经过一番分析,我发现附件显示在邮件正文中,并且只有某些邮件客户端将其显示为附件。这对我来说不是问题,但黑莓对这种附件有一些问题,因为它只显示正文并切断附件。但它适用于 Google、iMail、Thunderbird 等。

我分析了邮件的来源,发现附件的 ContentTransferEncoding 是 8 位:

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

我想如果我设置C# 中对象 AttachmentContentTransferEncoding 属性到 Base64 编码:

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

您认为这是一种良好且有效的方法吗?我还需要设置其他属性吗?

感谢大家

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠佳期 2024-10-16 01:28:29

查看来自我的黑莓手机的一些附件,我认为您需要添加 Content-Disposition 标题:

Content-Transfer-Encoding: base64
Content-Type: text/plain
Content-Disposition: attachment; filename="myfilename.bin"

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"
自此以后,行同陌路 2024-10-16 01:28:29

在我看来,你做的一切都是正确的,其他邮件客户端证明了这一点。尝试 BASE64,BlackBerry 很有可能会喜欢它,但在您实际看到它之前您无法确定:)。

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 :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文