2017-04-20 8 views
0

私はこのサイトで提供されている多くのソリューションを電子メールに添付してみましたが、何をしようとしても、私はいつも「申し訳ありません。私がoutlookのmailitemにファイルを添付しようとしている行でメッセージを再試行します。電子メールにファイルを添付するC#

try 
     { 
      App = new Microsoft.Office.Interop.Outlook.Application(); 

      MailItem mailItem = App.CreateItem(OlItemType.olMailItem); 

      mailItem.Subject = Subject; 
      mailItem.To = To; 
      mailItem.CC = CC; 
      mailItem.BCC = BCC; 
      mailItem.Body = Body; 

      // make sure a filename was passed 
      if (string.IsNullOrEmpty(FileAtachment) == false) 
      { 
       // need to check to see if file exists before we attach ! 
       if (!File.Exists(FileAtachment)) 
        MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       else 
       { 
        System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(FileAtachment); 
        mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); 
       } 
      } 
      mailItem.Display();  // display the email 
     } 
     catch (System.Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
     } 

これを動作させる方法を知っている人はいますか?私は問題なく電子メールを送信できますが、添付ファイルを追加しようとすると機能しません:(

+0

あなたは、Outlookのメールアイテムに 'System.Net.Mail.Attachment'を追加しているが、それは、Outlookの添付ファイルを期待します –

答えて

1

Attachments.Addメソッドはファイル(ファイル名を含む完全なファイルシステムパスで表されます)またはOutlookアイテムそれは、添付ファイルを構成ではなく、添付ファイルオブジェクト:

 App = new Microsoft.Office.Interop.Outlook.Application(); 

     MailItem mailItem = App.CreateItem(OlItemType.olMailItem); 

     mailItem.Subject = Subject; 
     mailItem.To = To; 
     mailItem.CC = CC; 
     mailItem.BCC = BCC; 
     mailItem.Body = Body; 

     // make sure a filename was passed 
     if (string.IsNullOrEmpty(FileAtachment) == false) 
     { 
      // need to check to see if file exists before we attach ! 
      if (!File.Exists(FileAtachment)) 
       MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      else 
      { 
       Attachment attachment = mailItem.Attachments.Add("D:\\text.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); 
      } 
     } 
     mailItem.Display();  // display the email 
関連する問題