通过C#在Outlook中打开发送电子邮件对话框

时间:2012-01-20 17:23:37

标签: c# asp.net email c#-4.0 outlook

我的应用程序需要在Outlook中打开发送电子邮件窗口。

应用程序显示发票列表。当用户点击发票号时,我需要在outlook中打开发送电子邮件窗口并附加PDF语句。然后,用户可以修改该消息并单击“发送”。

我该如何做到这一点?

我尝试了以下内容:

    using Outlook = Microsoft.Office.Interop.Outlook;

该应用程序在开发环境中运行良好,但我得到一个例外:

**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

我没有在应用服务器上安装Outlook。我需要在服务器上进行前景吗?或者例外是出于其他原因?如何摆脱它?

编辑:添加代码

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

        }

    }

3 个答案:

答案 0 :(得分:2)

您必须安装MS Outlook,Office应用程序的COM Interop依赖于Office应用程序可用,Microsoft仍然希望您为您的副本付费,而不是为您提供替换Word,Excel或Outlook的库。

答案 1 :(得分:0)

如果您想使用Microsoft COM对象发送电子邮件,请尝试查看此MS Link 它会带你一步一步的方法。希望你的机器也有COM对象..

Sending Outlook Emails via COM Object

答案 2 :(得分:0)

现在我们已经满足了您的实际要求,事情变得更容易了。

首先,从混合中摆脱Outlook。你不需要它。

当用户点击发票号码时,要求他们提供与电子邮件一起发送的消息。一旦他们在使用standard .net smtp objects键入此内容以创建邮件消息,请附加pdf并通过您的邮件服务器发送。

您不需要在服务器上安装Outlook,也不需要任何类型的互操作。

如果您使用的是Exchange或IMAP服务器,则该电子邮件将显示在用户outlook outlook框中。