邮件附件及其内容传输编码设置(BlackBerry 问题)
我使用 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# 中对象 Attachment 的 ContentTransferEncoding 属性到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看来自我的黑莓手机的一些附件,我认为您需要添加 Content-Disposition 标题:
Looking at some of the attachments coming from my BlackBerry, I think you need to add a Content-Disposition header:
请参阅:发送电子邮件对于来自 C# 的附件,附件作为 Thunderbird 中的第 1.2 部分到达
内容处置是我的问题的解决方案。
See: Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird
Content Disposition was the solution for my problem.
在我看来,你做的一切都是正确的,其他邮件客户端证明了这一点。尝试 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 :).