2015-12-24 62 views
8
string email ="[email protected]"; 
attachment = path + "/" + filename; 
Application.OpenURL ("mailto:" + 
         email+" 
         ?subject=EmailSubject&body=EmailBody"+"&attachment="+attachment); 

在上面的代碼中,attachment不起作用。在C#中使用mailto:鏈接添加附件是否有其他替代方法?如何將附件添加到C#中的mailto?

+1

爲什麼不應該你給看看System.net.mail,就可以實現的事情在一個更簡單的方法, 它實現像mailaddress所需的所有類,以及用於連接目的 – csharpcoder

+1

可能的附件類重複:[C#MailTo with Attachment?](http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment) –

回答

2

您可以使用具有MailMessage.Attachments屬性的System.Net.Mail。喜歡的東西:

message.Attachments.Add(new Attachment(yourAttachmentPath)); 

OR

你可以嘗試這樣的:

using SendFileTo; 

namespace TestSendTo 
{ 
    public partial class Form1 : Form 
    { 
     private void btnSend_Click(object sender, EventArgs e) 
     { 
      MAPI mapi = new MAPI(); 

      mapi.AddAttachment("c:\\temp\\file1.txt"); 
      mapi.AddAttachment("c:\\temp\\file2.txt"); 
      mapi.AddRecipientTo("[email protected]"); 
      mapi.AddRecipientTo("[email protected]"); 
      mapi.SendMailPopup("testing", "body text"); 

      // Or if you want try and do a direct send without displaying the 
      // mail dialog mapi.SendMailDirect("testing", "body text"); 
     } 
    } 
} 

上面的代碼使用MAPI32.DLL。

Source

它可能不會附加文件,因爲你是在電子郵件客戶端的自由 實施mailto協議,包括 解析爲附件條款。您可能不知道PC上安裝了哪個郵件客戶端 ,因此它可能並不總是有效 - Outlook肯定 不支持使用郵件附件到

+1

「Outlook當然不支持使用mailto的附件。」 它將爲一些版本,但你的工作權利,你不知道安裝什麼郵件客戶端,所以我建議使用C#System.Net.Mail.Attachment。 –

+1

@MaxSassen: - 這是發佈答案後的原因,我添加了使用'System.Net.Mail'的選項 –

2

mailto:不正式支持附件。我聽說過的Outlook 2003將與該語法工作:

<a href='mailto:[email protected]?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '> 

你的問題已經有了答案: c-sharp-mailto-with-attachment

-1

一種更好的方式來處理,這是用來發送郵件的服務器上System.Net .Mail.Attachment。

public static void CreateMessageWithAttachment(string server) 
{ 
    // Specify the file to be attached and sent. 
    // This example assumes that a file named Data.xls exists in the 
    // current working directory. 
    string file = "data.xls"; 
    // Create a message and set up the recipients. 
    MailMessage message = new MailMessage(
     "[email protected]", 
     "[email protected]", 
     "Quarterly data report.", 
     "See the attached spreadsheet."); 

    // Create the file attachment for this e-mail message. 
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 
    // Add time stamp information for the file. 
    ContentDisposition disposition = data.ContentDisposition; 
    disposition.CreationDate = System.IO.File.GetCreationTime(file); 
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 
    // Add the file attachment to this e-mail message. 
    message.Attachments.Add(data); 

    //Send the message. 
    SmtpClient client = new SmtpClient(server); 
    // Add credentials if the SMTP server requires them. 
    client.Credentials = CredentialCache.DefaultNetworkCredentials; 

    try 
    { 
     client.Send(message); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString());    
    } 
    data.Dispose(); 
} 
+2

您剛剛複製該帖子:http://stackoverflow.com/questions/1195111/c-sharp-mailto -with-附件#答案-1195153 只是鏈接到它 –

相關問題