2012-01-20 11 views
2

私のアプリケーションでは、Outlookでメールの送信ウィンドウを開く必要があります。OutlookでC#でメールを送信ダイアログを開く

アプリケーションは、請求書の一覧を表示します。ユーザーが請求書番号をクリックすると、私はoutlookでメールの送信ウィンドウを開き、PDFステートメントを添付する必要があります。ユーザーはメッセージを変更して[送信]をクリックできます。

どうすればこの問題を解決できますか?

私は次のことを試してみました:

using Outlook = Microsoft.Office.Interop.Outlook; 

アプリケーション開発環境に正常に動作しますが、IAMが例外を取得:

**System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))** 

Detailed: 

    System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) 
    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) 
     at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) 
    at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) 
    at System.Activator.CreateInstance(Type type, Boolean nonPublic) 
    at InvoiceSearchTool.Controllers.emailController.CreateMessageWithAttachment(String invoiceNumber, String recipient, String messageBody) in C:\Projects\KeleInvoice\InvoiceSearchTool\Controllers\emailController.cs:line 38 

私は見通しがアプリケーションサーバーにインストールされていません。私はサーバーの見通しをistallする必要がありますか?例外は他の理由によるものですか?どのようにそれを取り除く?

EDIT:コード

public static void CreateMessageWithAttachment(string invoiceNumber, string recipient, string messageBody) 
    { 
     try 
     { 

      Outlook.Application oApp = new Outlook.Application(); 
      Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem)); 

      Models.DYNAMICS_EXTEntities _db = new Models.DYNAMICS_EXTEntities(); 

      #region set email recipients 
      { 
       ObjectParameter[] parameters = new ObjectParameter[1]; 
       parameters[0] = new ObjectParameter("InvoiceNumber", invoiceNumber); 

       List<Models.EmailAddress> emailList = _db.ExecuteFunction<Models.EmailAddress>("uspGetEmailAddress", parameters).ToList<Models.EmailAddress>(); 
       if (emailList.Count() > 0) 
       { 
        if(!(string.IsNullOrEmpty(emailList[0].Email.ToString().Trim()))) 
        recipient = emailList[0].Email.ToString().Trim(); 
        else 
         recipient = " "; 
       } 
       else 
        recipient = " "; 

       email.Recipients.Add(recipient); 
      } 
      #endregion 

      //email subject     
      email.Subject = "Invoice # " + invoiceNumber; 

      #region set email Text 
      { 
       Models.EmailText emailText = _db.ExecuteFunction<Models.EmailText>("uspEmailText").SingleOrDefault(); 

       messageBody = emailText.EmailTextLine1.ToString().Trim(); 
       email.Body = messageBody; 
      } 
      #endregion 

      #region email attachment 
      { 
       string fileName = invoiceNumber.Trim(); 
       string filePath = HostingEnvironment.MapPath("~/Content/reports/"); 
       filePath = filePath + fileName + ".pdf"; 
       fileName += ".pdf"; 
       int iPosition = (int)email.Body.Length + 1; 
       int iAttachType = (int)Outlook.OlAttachmentType.olByValue; 
       Outlook.Attachment oAttach = email.Attachments.Add(filePath, iAttachType, iPosition, fileName); 
      } 
      #endregion 

      email.Display();     

         } 
     catch (Exception e) 
     { 
      InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable(); 
      exception.MethodName = "email"; 
      exception.Exception = e.ToString(); 
      exception.Date = DateTime.Now; 
      DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities(); 
      db.AddToudtExceptionTables(exception); 
      db.SaveChanges(); 

     } 

    } 
+0

Outlook経由で電子メールを送信する必要があるが、電子メールを送信したマシンにOutlookがインストールされていない場合は、これのポイントは何ですか?少なくとも、Interopが参照するCOMオブジェクトが必要です。 – Olaf

+0

私たちはちょうどbinのdllをコピーして、そのように使うのですか? – 14578446

+0

いいえ、登録する必要があります。 – Olaf

答えて

0

をCOMオブジェクトを持つことになります。

まず、混乱からOutlookを取り除く。あなたはそれを必要としません。

ユーザーが請求書番号をクリックすると、メールに添付されているメッセージを尋ねます。彼らがこれをタイプすると、standard .net smtp objectsはメールメッセージを作成し、pdfを添付してあなたのメールサーバーを送信します。

Outlookはサーバーにインストールする必要はなく、interopの種類も必要ありません。

ExchangeまたはIMAPサーバーを使用している場合、電子メールはユーザーのOutlook送信済みボックスに表示されます。

+0

@ Chris:この場合、どのように送信される添付ファイルのプレビューをユーザーに与えることができますか? – 14578446

+0

@ 14578446:請求書の行に2つのリンクがある可能性があります。最初にインボイスを見て、インボイスを送信する2番目の... – NotMe

2

を追加あなたはMS Outlookがインストールされている必要があり、OfficeアプリケーションのCOM相互運用機能は、Officeに依存しているが利用できるようにするアプリ、Microsoftはまだあなたがあなたのコピーのために支払いをしたいん、ではありませんWord、Excel、Outlookを置き換えるライブラリを提供します。

+0

は、Outlookを使用しているわけではありません。質問がC#で電子メールを送信する方法であれば、SMTP電子メール送信用の.NET Frameworkクラスがあり、それ以外のものは必要ありませんが、MS Outlookを使用するように求められます。 –

+0

@DJKRAZE:パテ? – 14578446

0

Microsoft COMオブジェクトを使用して電子メールを送信する場合は、このMSリンク を見てみると、ステップバイステップアプローチが必要です。うまくいけば、あなたのマシンは、今、私たちはあなたの実際の要件は、物事が少し楽に得る持っていること。..だけでなく

Sending Outlook Emails via COM Object

+0

とにかくOutlookが必要です –

関連する問題