2010-05-13 22 views
80

我有一個C#應用程序,它使用SMTP通過Exchange 2007服務器通過電子郵件發送Excel電子表格報告。這些適用於Outlook用戶,但對於Thunderbird和Blackberry用戶,附件已更名爲「第1.2部分」。發送帶有C#附件的電子郵件,附件以Thunderbird的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); 
} 

感謝您的幫助。

+0

你試圖定義/更改'附件.Name'屬性? – Alex 2010-05-13 10:45:24

+0

不,我沒有 - 「獲取或設置MIME內容類型名稱的值」,你有什麼值嘗試的建議嗎?謝謝。 – Jon 2010-05-13 11:09:08

+0

當收到附件的電子郵件時,名稱會顯示爲附件的名稱。所以你可以嘗試任何價值。 – Alex 2010-05-13 11:24:10

回答

71

顯式地填充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);     
} 
+0

爲什麼不使用'FileInfo'對象來獲取'CreationTime','LastWriteTime'和'LastAccessTime'屬性?無論如何,你正在創建一個獲得'Length'屬性。 – Krumia 2015-12-17 12:31:12

63

簡單的代碼發送電子郵件與附件。

源: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 [email protected]"); 
    mail.To.Add("[email protected]"); 
    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 [email protected]", "your password"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 

} 
+10

你應該使用語句來包裝MailMessage和SmtpClient,以確保它們被正確處置 – Andrew 2013-01-14 09:07:54

+1

@Andrew - 我該怎麼做? – Steam 2013-12-30 19:09:13

+0

我試過這段代碼,我得到了這篇文章中顯示的錯誤 - http://stackoverflow.com/questions/20845469/finding-the-exact-cause-for-the-exception-system-net-sockets-socketexception – Steam 2013-12-30 19:09:30

4

完成Ranadheer的解決方案,使用使用Server.Mappath來定位文件

System.Net.Mail.Attachment attachment; 
attachment = New System.Net.Mail.Attachment(Server.MapPath("~/App_Data/hello.pdf")); 
mail.Attachments.Add(attachment); 
+0

'Server.MapPath'從哪裏來,它應該在什麼時候使用? – Kimmax 2016-07-05 15:03:49

0

嘗試這種情況:

private void btnAtt_Click(object sender, EventArgs e) { 

    openFileDialog1.ShowDialog(); 
    Attachment myFile = new Attachment(openFileDialog1.FileName); 

    MyMsg.Attachments.Add(myFile); 


} 
6

這裏是一個簡單的郵件發送代碼附件

try 
{ 
    SmtpClient mailServer = new SmtpClient("smtp.gmail.com", 587); 
    mailServer.EnableSsl = true; 

    mailServer.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); 

    string from = "[email protected]"; 
    string to = "[email protected]"; 
    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); 
} 

更多Sending emails with attachment in C#

1
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); 
} 
0

你好兄弟我做了簡短的代碼,這樣做,我想與大家

這裏主要的代碼共享

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); 
} 

} 

在你的按鈕做這樣的東西
您可以添加過濾JPG或PDF以及更多......這只是一個例子

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

} 
} 

投票,如果它有幫助的給予反饋感謝

相關問題