2015-12-24 53 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

なぜあなたはそれが必要なすべてのクラスがmailaddressのように実施している、あなたがはるかに簡単な方法で物事を達成することができ、 をSystem.net.mailに外観を与える、および添付ファイルの目的の可能性 – csharpcoder

+1

用アタッチメントクラスべきではありません重複:[C#MailTo Attachment?](http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment) –

答えて

2

MailMessage.Attachmentsプロパティを持つSystem.Net.Mailを使用できます。

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を使用しています:あなたはこのように試すことができ

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

OR

:ような何か。

Source

あなたは電子メールクライアントの自由 のmailtoプロトコルを実装し、添付ファイルの句の 解析を含めることであるので、それはおそらく、文書を添付しません。 がPCにインストールされているメールクライアントがわからない可能性があります。Outlookは確かに はmailtoを使用している添付ファイルをサポートしていません。

+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-attachment#answer-1195153 ただそれにリンクしています –