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

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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.<br/>
<br/>
Exception: <br/>
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)).
<br/>
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)<br/>
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)<br/>
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)<br/>
at System.Activator.CreateInstance(Type type, Boolean nonPublic)<br/>
at InvoiceSearchTool.Controllers.emailController.CreateMessageWithAttachment(String invoiceNumber, String recipient, String messageBody) in C:ProjectsInvoiceInvoiceSearchToolControllersemailController.cs:line 38<br/>
<br/>
Got Outlook installed on app server.
Problem: Code Still not working on client machine.
Code:
<div style="background-color:white; color:black
<pre> <span style="color:blue public <span style="color:blue static <span style="color:blue void CreateMessageWithAttachment(<span style="color:blue string invoiceNumber, <span style="color:blue string recipient = <span style="color:#a31515 " ", <span style="color:blue string messageBody = <span style="color:#a31515 " ")
{
<span style="color:blue try
{
Outlook.Application oApp = <span style="color:blue new Outlook.Application();
Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));

Models.DYNAMICS_EXTEntities _db = <span style="color:blue new Models.DYNAMICS_EXTEntities();

<span style="color:blue #region set email recipients
{
ObjectParameter[] parameters = <span style="color:blue new ObjectParameter[1];
parameters[0] = <span style="color:blue new ObjectParameter(<span style="color:#a31515 "InvoiceNumber", invoiceNumber);

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

email.Recipients.Add(recipient);
}
<span style="color:blue #endregion

<span style="color:green //email subject
email.Subject = <span style="color:#a31515 "Invoice # " + invoiceNumber;

<span style="color:blue #region set email Text
{
Models.EmailText emailText = _db.ExecuteFunction<Models.EmailText>(<span style="color:#a31515 "uspEmailText").SingleOrDefault();

messageBody = emailText.EmailText.ToString().Trim();
email.Body = messageBody;
}
<span style="color:blue #endregion

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

email.Display();

}
<span style="color:blue catch (Exception e)
{
InvoiceSearchTool.Models.udtExceptionTable exception = <span style="color:blue new udtExceptionTable();
exception.MethodName = <span style="color:#a31515 "email";
exception.Exception = e.ToString();
exception.Date = DateTime.Now;
DYNAMICS_EXTEntities db = <span style="color:blue new DYNAMICS_EXTEntities();
db.AddToudtExceptionTables(exception);
db.SaveChanges();

}

}
[/code]


View the full article
 
Back
Top