我有一个C#应用程序,它使用SMTP通过Exchange 2007服务器通过电子邮件发送Excel电子表格报告 . 这些适用于Outlook用户,但对于Thunderbird和Blackberry用户,附件已重命名为“第1.2部分” .
我发现这个article描述了这个问题,但是没有控制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);
}
谢谢你的帮助 .
8 回答
使用附件发送电子邮件的简单代码 .
来源:http://www.coding-issues.com/2012/11/sending-email-with-attachments-from-c.html
明确填写ContentDisposition字段就可以了 .
BTW ,如果是Gmail,您可能会遇到一些关于ssl secure甚至端口的例外情况!
这是一个带附件的简单邮件发送代码
阅读更多Sending emails with attachment in C#
完成Ranadheer的解决方案,使用 Server.MapPath 找到该文件
试试这个:
我尝试了Ranadheer Reddy(上图)提供的代码,效果很好 . 如果您使用的是具有受限服务器的公司计算机,则可能需要将SMTP端口更改为25并将用户名和密码留空,因为它们将由管理员自动填写 .
最初,我尝试使用来自nugent包管理器的EASendMail,只是意识到它是30天试用版的付费 . 除非你打算买它,否则不要浪费时间 . 我注意到程序使用EASendMail运行得更快,但对我来说,免费快速 .
只需2美分 .
我已经制作了一个简短的代码,我希望与您分享 .
这里的主要代码: