Hi I am new to C# development. and I was given the job to create a C# WPF stand alone application, which creates an outlook template and open that in outlook application.
In order to open the outlook, I ma using outlook._mailitem and below is a snapshot of the code
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = EmailUrl;
oMailItem.BCC = "CarerGatewayReferrals#wellways.org";
oMailItem.HTMLBody = HTMLBody;
oMailItem.Subject = SubjectText;
oMailItem.Display(true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ", Please contact NCSP BI Team");
}
Now after building the application, I placed the executable file in our Company Server, so that everyone can access this application. Unfortunately due to the requirments I can't publish this application.
Whenever a user runs the application, and reach to this point,when the application needs to open Outlook, the program catches the exception and prompts the following message
"Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section"
I tried to debug the program, but found no issues at my development machine. And I think the error is occuring when the program is trying to open Outlook. I have no idea how to resolve this issue.
Please some one help.
Related
We have hosted a .net application on our IIS server.
This application tries to read the emails from the current logged in users outlook.
I am using the library using Microsoft.Office.Interop.Outlook; and below is my code.
I am able to view the emails when this code runs from my VS.
The moment I deploy this application on IIS then I am unable to top read any emails.
This is the error which is logged.
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
Am i following the correct approach on accessing the emails or are there any different ways to archive this? please enlighten.
Below is the whole code.
try
{
outlookApplication = new Application();
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
mailItems = inboxFolder.Items;
foreach (object item in inboxFolder.Items)
{
if (item is Microsoft.Office.Interop.Outlook.MailItem)
{
Microsoft.Office.Interop.Outlook.MailItem mailitem = (Microsoft.Office.Interop.Outlook.MailItem)item;
if(mailitem.ReceivedTime.Date ==DateTime.Today)
{
TempEmail objTempEmail = new TempEmail();
objTempEmail.From = mailitem.SenderEmailAddress;
objTempEmail.To = mailitem.To;
objTempEmail.CC = mailitem.CC;
objTempEmail.Subject = mailitem.Subject;
objTempEmail.Body = mailitem.Body;
lTempEmail.Add(objTempEmail);
Marshal.ReleaseComObject(mailitem);
}
}
}
}
catch (System.Exception ex)
{
log.Error(ex.Message + "" + ex.InnerException);
}
finally
{
ReleaseComObject(mailItems);
ReleaseComObject(inboxFolder);
ReleaseComObject(outlookNamespace);
ReleaseComObject(outlookApplication);
}
"Am i following the correct approach"
No. Outlook interop will only work on the local machine. All you will achieve is checking the AppPool's inbox, which won't exist.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
As a workaround you may consider using EWS or Outlook REST API if you deal with Exchange based mailboxes. See EWS Managed API, EWS, and web services in Exchange for more information.
i am try to hook to outlook application from windows Service but getting an exception Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) here is my code.
public void ItemSendEvent()
{
try
{
if (Process.GetProcessesByName(ApplicationConstants.OUTLOOK_PROCESS_NAME).Count() > 0)
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
outlookApplication = Marshal.GetActiveObject(ApplicationConstants.OUTLOOK_APPLICATION_NAME) as Microsoft.Office.Interop.Outlook.Application;
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = outlookApplication.GetNamespace(ApplicationConstants.OUTLOOK_NAME_SPACE);
nameSpace.Logon("", "", Missing.Value, Missing.Value);
nameSpace = null;
outlookApplication.ItemSend += outlookApplication_ItemSend;
}
log.Info("Outlook Item Send event registered successfully.");
}
catch (System.Exception ex)
{
log.Error("Exception occurred while registering Outlook Item Send event. " + ex.Message);
}
}
but the same code when i launch it through Windows Form Application its working Fine. i gone through some site's and they were saying that outlook object is not in ROT Table. what will be the solution.
Outlook, or any other Office app, cannot run in a Windows service even if your service is running as an interactive user. Only Extended MAPI (C++ or Delphi only) or an Extended MAPI wrapper like Redemption (I am its author - its RDO family objects) can be used in a service.
In your particular case, it looks like you are trying to trap the Application.ItemSend event. There is absolutely no reason to create a Windows service for that. Create a COM addin - it will be loaded by Outlook and run as long as Outlook itself is running in the same process in the same security context.
Two common issues could cause this.
The first would be that you are running Visual Studio in Administrator mode and you are starting your program from within VS, and the Office application is not. To fix that, you need to run your Office application with elevated privileges, in Administrator mode, as well.
The second could be caused by the application not being fully started/loaded when you call Marshal.GetActiveObject(...).
Old, but still significant thread.
I collided with this error when tried to access Outlook data using the MS example.
Treating the error in a Try / Catch block and offering the option of newing Outlook solves the problem:
const int ERROR_HRESULT_0x800401E3_MK_E_UNAVAILABLE= -2147221021;
Outlook.Application application = null;
// Check whether there is an Outlook process running.
if (Process.GetProcessesByName("OUTLOOK").Any())
{
try
{
// If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
}
catch (Exception ex)
{
//This is the branch where you can get correctly the current Outlook instance
if (ex.HResult == ERROR_HRESULT_0x800401E3_MK_E_UNAVAILABLE)
{
application = new Outlook.Application();
}
}
}
else
{
application = new Outlook.Application();
}
Although the newing trick functions, no other Outlook instance is created, as Outlook behaves like a Singleton.
I only tested it with Office 365 64 bits installed.
you don't need to have your application as a service to get it on the background ...
if your winform work well, just put your winform in background running on the systray for instance
Trying to add an event to Outlook calendar through my event registration app using asp.net/C#. Getting call was rejected by callee error when trying to initialize (line 1). How do I overcome this issue?
Error:
"Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80010001 Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))."
Outlook.Application outlookapp = new Outlook.Application();
Outlook.AppointmentItem appt = outlookapp.CreateItem(Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
appt.Subject = er.Event.Name;
appt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
appt.Location = er.Event.LocationName;
appt.Start = er.Event.StartTime;
appt.End = er.Event.EndTime;
appt.Recipients.ResolveAll();
appt.Display(false);
appt.Save();
Firstly, you cannot use Outlook from a service (such as IIS).
Secondly, even if your code worked, you'd end up creating an appointment and displaying (!) it locally on the server machine, where there isn't anybody to see it.
Create an iCal file and provide a link to the user - the ics file will be opened on the client machine using Outlook and the user will be able to save it.
Server-side Automation of Office is not supported
Developers can use Automation in Microsoft Office to build custom solutions that use the capabilities and the features that are built into the Office product. Although such programmatic development can be implemented on a client system with relative ease, a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET, DCOM, or a Windows NT service.
See : https://support.microsoft.com/en-ca/kb/257757
I am using this link for open outlook new mail window.
How to open Outlook new mail window c#
But It is working fine On Local machine but when I deployed it on server it shows below error.
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)).
Microoft office outlook is install on local machine not on server.It is required to install and configure outlook on server.
Plz help.
Thanks.
1. Using Outlook
To send an email using outlook, we need to add a reference to the dynamic link library for Outlook which is called Microsoft.Office.Interop.Outlook.dll
For the same follow the below steps:
Go to your solution explorer
Click on add a reference
Click on .Net Tab
Go through the DLL and select Microsoft.Office.Interop.Outlook.dll
correctly.
when you have selected the correct reference you select the “OK” button and this reference will be added to your project under references.
using Outlook = Microsoft.Office.Interop.Outlook;
//method to send email to outlook
public void sendEMailThroughOUTLOOK()
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
//Add an attachment.
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//now attached the file
Outlook.Attachment oAttach = oMsg.Attachments.Add(#"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
//Subject line
oMsg.Subject = "Your Subject will go here.";
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("jawed.ace#gmail.com");
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
}//end of catch
}//end of Email Method
For more information Open outlook
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
Read more about that in the Considerations for server-side Automation of Office article.
Consider using standard .Net classes or any other components designed for the server-side execution. In case of Exchange Server account you can use EWS (Exchange Web Services), see EWS Managed API, EWS, and web services in Exchange .
If you run that code on the server, who will see the newly created message? Even if there is a user logged in locally to the server, IIS runs without a desktop session.
If you want the message to be displayed on the client, that is where your code needs to run. Why not use a mailto url? It will work in any browser and the default email client will be opened. If you need something more sophisticated than that, you need to write your code in JavaScript and create an instance of the Outlook.Application object using new ActiveXObject(). You can only do that in IE and your site must be trusted to do that.
I'm building an application that opens existing mail messages in Outlook. The user may or may not already be running Outlook. All works well if Outlook is not running, but if it's already running I get a COM error (80080005). The internet seems to indicate that this can happen if the existing Outlook process is running with a higher permission level than the app that tries to bind to it.
Is there some other way for me to ask Outlook to open a message, or do I just need to make sure I match permission levels?
Thanks,
-Patrick
EDIT Adding code to original question, as Stack Overflow does not permit meaningful formatting in comments.
I was originally doing the following:
var outlook = new Outlook.Application();
That line works in all cases except the case where I've launched Outlook prior to launching my application. In that case, I get the aforementioned 80080005 error code.
I've changed this to be a bit more COM-explicit:
Application outlook;
try
{
outlook = (Application)Marshal.GetActiveObject("Outlook.Application");
}
catch (COMException ex)
{
if (ex.ErrorCode == -2147221021)
outlook = new ApplicationClass();
else
throw;
}
However, that code still does not quite work -- if Outlook is running, I trap an exception whose ErrorCode is 0x800401E3 (MK_E_UNAVAILABLE). But when I attempt to create the new ApplicationClass object, I still get the same 80080005 error code.
I've also tried putting the following into the catch block instead of the new ApplicationClass() line, but there's no difference in behavior:
outlook = (Application) Activator.CreateInstance(
Type.GetTypeFromProgID("Outlook.Application"));
Turns out that the cause of the problem was the debugger -- I was launching Word from Visual Studio's debugger. When launching Word via normal pathways, the 80080005 code goes away.
-Patrick
Without seeing your code I'm guessing, but it sounds like you are calling CreateObject(). You need to call GetObject() if Outlook is already running.
First, use GetObject to see whether Outlook is already running (You need to catch
the error).