none
open Outlook at client machine via web app in c#.net RRS feed

  • Question

  • Requirment: To build a web page, which lists the invoice numbers. When user clicks on invoice number, the invoice statement corresponding to it is generated and attched to an email composed in outlook with all fields(To, cc, from, Subject, message body and attachment) set.

    Problem: the application i built is working fine on development environment but I am receiving an exception on application server.

    Exception:
    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\Invoice\InvoiceSearchTool\Controllers\emailController.cs:line 38

    Got Outlook installed on app server.

    Problem: Code Still not working on client machine.

    Code:

     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.EmailText.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();
    
                }
    
            }
    

     

     

     

     

    • Moved by Cindy Meister MVP Monday, January 23, 2012 7:27 AM Outlook question; specialists may be able to suggest alternative approaches (From:Visual Studio Tools for Office)
    Friday, January 20, 2012 9:28 PM

Answers

  • Hi DZ,

     

    Thanks for posting in the MSDN Forum.

     

    As far as I know that Microsoft doesn’t recommend use Office PIA on server-side. Please take a look at http://support.microsoft.com/kb/257757. You will get more information for Server-side automation of Office.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by DZ craze Tuesday, January 24, 2012 3:05 PM
    Monday, January 23, 2012 7:15 AM
    Moderator

All replies

  • Hi DZ,

     

    Thanks for posting in the MSDN Forum.

     

    As far as I know that Microsoft doesn’t recommend use Office PIA on server-side. Please take a look at http://support.microsoft.com/kb/257757. You will get more information for Server-side automation of Office.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by DZ craze Tuesday, January 24, 2012 3:05 PM
    Monday, January 23, 2012 7:15 AM
    Moderator
  • As always, do not use outlook from under IIS, windows service accounts. If you need to send an email from server consider using MailMessage and SmtpClient classes in .net
    Monday, January 23, 2012 12:10 PM