从 C# 发送带有附件的电子邮件,附件在 Thunderbird 中作为 Part 1.2 到达 [英] Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

查看:26
本文介绍了从 C# 发送带有附件的电子邮件,附件在 Thunderbird 中作为 Part 1.2 到达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 应用程序,它使用 SMTP 通过 Exchange 2007 服务器通过电子邮件发送 Excel 电子表格报告.这些对于 Outlook 用户来说很好,但对于 Thunderbird 和 Blackberry 用户,附件已重命名为第 1.2 部分".

I have a C# application which emails out Excel spreadsheet reports via an Exchange 2007 server using SMTP. These arrive fine for Outlook users, but for Thunderbird and Blackberry users the attachments have been renamed as "Part 1.2".

我发现这篇 文章 描述了这个问题,但没有t似乎给了我一个解决方法.我无法控制 Exchange 服务器,因此无法在那里进行更改.在 C# 端有什么我可以做的吗?我曾尝试对正文使用短文件名和 HTML 编码,但都没有区别.

I found this article which describes the problem, but doesn't seem to give me a workaround. I don't have control of the Exchange server so can't make changes there. Is there anything I can do on the C# end? I have tried using short filenames and HTML encoding for the body but neither made a difference.

我的邮件发送代码是这样的:

My mail sending code is simply this:

public static void SendMail(string recipient, string subject, string body, string attachmentFilename)
{
    SmtpClient smtpClient = new SmtpClient();
    NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password);
    MailMessage message = new MailMessage();
    MailAddress fromAddress = new MailAddress(MailConst.Username);

    // setup up the host, increase the timeout to 5 minutes
    smtpClient.Host = MailConst.SmtpServer;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = basicCredential;
    smtpClient.Timeout = (60 * 5 * 1000);

    message.From = fromAddress;
    message.Subject = subject;
    message.IsBodyHtml = false;
    message.Body = body;
    message.To.Add(recipient);

    if (attachmentFilename != null)
        message.Attachments.Add(new Attachment(attachmentFilename));

    smtpClient.Send(message);
}

感谢您的帮助.

推荐答案

明确填写 ContentDisposition 字段即可解决问题.

Explicitly filling in the ContentDisposition fields did the trick.

if (attachmentFilename != null)
{
    Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
    ContentDisposition disposition = attachment.ContentDisposition;
    disposition.CreationDate = File.GetCreationTime(attachmentFilename);
    disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
    disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
    disposition.FileName = Path.GetFileName(attachmentFilename);
    disposition.Size = new FileInfo(attachmentFilename).Length;
    disposition.DispositionType = DispositionTypeNames.Attachment;
    message.Attachments.Add(attachment);                
}

顺便说一句,对于 Gmail,您可能对 ssl 安全甚至端口有一些例外!

BTW, in case of Gmail, you may have some exceptions about ssl secure or even port!

smtpClient.EnableSsl = true;
smtpClient.Port = 587;

这篇关于从 C# 发送带有附件的电子邮件,附件在 Thunderbird 中作为 Part 1.2 到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆