用附件从C#发送电子邮件,附件到达Thunderbird的第1.2部分

我有一个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); } 

简单的代码发送电子邮件与附件。

来源: http : //www.coding-issues.com/2012/11/sending-email-with-attachments-from-c.html

 using System.Net; using System.Net.Mail; public void email_send() { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your mail@gmail.com"); mail.To.Add("to_mail@gmail.com"); mail.Subject = "Test Mail - 1"; mail.Body = "mail with attachment"; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment("c:/textfile.txt"); mail.Attachments.Add(attachment); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); } 

这是一个简单的邮件发送代码附件

 try { SmtpClient mailServer = new SmtpClient("smtp.gmail.com", 587); mailServer.EnableSsl = true; mailServer.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword"); string from = "myemail@gmail.com"; string to = "reciever@gmail.com"; MailMessage msg = new MailMessage(from, to); msg.Subject = "Enter the subject here"; msg.Body = "The message goes here."; msg.Attachments.Add(new Attachment("D:\\myfile.txt")); mailServer.Send(msg); } catch (Exception ex) { Console.WriteLine("Unable to send email. Error : " + ex); } 

阅读更多在C#中使用附件发送电子邮件

完成Ranadheer的解决scheme,使用Server.MapPath来定位文件

 System.Net.Mail.Attachment attachment; attachment = New System.Net.Mail.Attachment(Server.MapPath("~/App_Data/hello.pdf")); mail.Attachments.Add(attachment); 
 private void btnSent_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress(txtAcc.Text); mail.To.Add(txtToAdd.Text); mail.Subject = txtSub.Text; mail.Body = txtContent.Text; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(txtAttachment.Text); mail.Attachments.Add(attachment); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential(txtAcc.Text, txtPassword.Text); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); MessageBox.Show("mail send"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void button1_Click(object sender, EventArgs e) { MailMessage mail = new MailMessage(); openFileDialog1.ShowDialog(); System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(openFileDialog1.FileName); mail.Attachments.Add(attachment); txtAttachment.Text =Convert.ToString (openFileDialog1.FileName); } 

尝试这个:

 private void btnAtt_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); Attachment myFile = new Attachment(openFileDialog1.FileName); MyMsg.Attachments.Add(myFile); } 

你好兄弟,我做了一个简短的代码,我想与你分享

这里的主要代码

  public void Send(string from, string password, string to, string Message, string subject, string host, int port, string file) { MailMessage email = new MailMessage(); email.From = new MailAddress(from); email.To.Add(to); email.Subject = subject; email.Body = Message; SmtpClient smtp = new SmtpClient(host, port); smtp.UseDefaultCredentials = false; NetworkCredential nc = new NetworkCredential(from, password); smtp.Credentials = nc; smtp.EnableSsl = true; email.IsBodyHtml = true; email.Priority = MailPriority.Normal; email.BodyEncoding = Encoding.UTF8; if (file.Length > 0) { Attachment attachment; attachment = new Attachment(file); email.Attachments.Add(attachment); } // smtp.Send(email); smtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallBack); string userstate = "sending ..."; smtp.SendAsync(email, userstate); } private static void SendCompletedCallBack(object sender,AsyncCompletedEventArgs e) { string result = ""; if (e.Cancelled) { MessageBox.Show(string.Format("{0} send canceled.", e.UserState),"Message",MessageBoxButtons.OK,MessageBoxIcon.Information); } else if (e.Error != null) { MessageBox.Show(string.Format("{0} {1}", e.UserState, e.Error), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("your message is sended", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } 

在你的button做这样的东西
你可以添加filterJPG或PDF和更多..这只是一个例子

 using (OpenFileDialog attachement = new OpenFileDialog() { Filter = "Exel Client|*.png", ValidateNames = true }) { if (attachement.ShowDialog() == DialogResult.OK) { Send("yourmail@gmail.com", "gmail_password", "tomail@gmail.com", "just smile ", "mail with attachement", "smtp.gmail.com", 587, attachement.FileName); } } 

如果有帮助的话给予反馈表示感谢