我有一个C#应用程序,可以使用SMTP通过Exchange 2007服务器通过电子邮件发送Excel电子表格报告。这些对于Outlook用户来说可以正常使用,但是对于Thunderbird和Blackberry用户,附件已重命名为“ 1.2部分”。
我找到了描述问题的文章,但似乎没有给我解决方法。我无法控制Exchange服务器,因此无法在其中进行更改。在C#端我有什么可以做的吗?我曾尝试使用短文件名和HTML编码作为正文,但是两者都没有改变。
我的邮件发送代码就是这样的:
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字段可以达到目的。
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安全甚至端口有一些例外!
smtpClient.EnableSsl = true; smtpClient.Port = 587;