//Set NameSpcae for Outlook "Mapi" is by Default Name
NameSpace outlookNs = null;
DailyLog("Start Outlook Application Mapi", "");
outlookNs = app.GetNamespace("MAPI");
DailyLog("End Outlook Application Start Mapi", "");
DailyLog("Start Outlook Application Logon", "");
outlookNs.Logon(null, null, false, false);
DailyLog("End Outlook Application Logon", "");
Console.WriteLine("Set By Default Name MAPI");
//Add Pst File in OutLook
Console.WriteLine("Adding PST File From Path : " + pstFilePath);
DailyLog("Set PST File Path :- " + pstFilePath, "");
**outlookNs.AddStore(pstFilePath);**
DailyLog("Set PST File Path :- " + pstFilePath, "");
Console.WriteLine("SET PST File To Successfully.");
DailyLog("SET PST File To Successfully.","");
if (Convert.ToInt16(ConfigurationManager.AppSettings["isSendRecieved"]) == 1)
{
outlookNs.SendAndReceive(true);
}
i get this error message "The Outlook data file (.pst) failed to load for this session"
<add key="MailPort" value="25"/>
<add key="MailServer" value="smtp1.projectstoday.com"/>
also used the port and server
what should i add the port number and Mail server for Office365
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.
If you deal with Exchange accounts you may consider using the EWS or Graph API (in case of Office365) instead, see Explore the EWS Managed API, EWS, and web services in Exchange for more information.
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.
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've been successfully sending out appointment invites with outlook through c# in an asp.net application. I'm using the following code:
//send out the outlook notification
Outlook.Application outlookApp = new Outlook.Application();
Outlook.AppointmentItem newMeeting = outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
if (newMeeting != null)
{
newMeeting.MeetingStatus = Microsoft.Office.Interop.Outlook.OlMeetingStatus.olMeeting;
newMeeting.Location = "TBD";
newMeeting.Subject = "New SEB Consult";
newMeeting.Body = "A new meeting has been scheduled. If you're a member of the team, please accept";
newMeeting.Start = meetingDate;
newMeeting.Duration = 60;
Outlook.Recipient recipient = newMeeting.Recipients.Add("Smith John");
recipient.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
((Outlook._AppointmentItem)newMeeting).Send();
}
This works, but my problem is that it's sending them from my email which I'm logged into with outlook on the same computer. I'd like to send them from a different email, so that they appear more like system notifications coming from my application rather than a personal email. I do have the username and password for the account, but the application is eventually going to be run on a remote server, so I can't just log into outlook with another email. Nothing I've been able to find changes the sender. Does anyone have any more information on how to change these credentials, or where it looks for the credentials?
You can't use OLE if you would like to control the emails. OLE is just to control the local outlook instance which is tied to the running account.
You must use the exchange API instead. With it you can create an appointment like described in this MSDN article: How to: Create appointments and meetings by using EWS in Exchange 2013
Appointment appointment = new Appointment(service);
// Set the properties on the appointment object to create the appointment.
appointment.Subject = "Tennis lesson";
appointment.Body = "Focus on backhand this week.";
appointment.Start = DateTime.Now.AddDays(2);
appointment.End = appointment.Start.AddHours(1);
appointment.Location = "Tennis club";
appointment.ReminderDueBy = DateTime.Now;
// Save the appointment to your calendar.
appointment.Save(SendInvitationsMode.SendToNone);
// Verify that the appointment was created by using the appointment's item ID.
Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
Console.WriteLine("\nAppointment created: " + item.Subject + "\n");
The library is open source and available at github.
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.
You may consider using the EWS Managed API, EWS, and web services in Exchange instead.
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 have created outlook addin in C# to storing mail subjects in SQLite database using redemption(background). Can we access redemption method/Class of outlook add in into the windows service.
Outlook Object Model cannot be used from a service.
Do not use Outlook Object Model in a service. Secondly, you are assuming that you only have ContactItem objects in the folder, your code will break if there is a distribution list there.
The RDO family of Redemption objects can be used in a service
You are right, you shouldn't use the Outlook object model from a windows service. The Considerations for server-side Automation of Office article states the following:
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.
Redemption is a wrapper around a low-level API (Extended MAPI). But add-ins is a feature of Office applications. Extended MAPI doesn't know wnything about them. So, any wrappers (Redemption in your case) around Extended MAPI don't provide access to add-ins.
Consider using any other communication methods with your add-in, for example - .NET Remoting (WCF). You may consider your managed add-in as a regular .Net application.
using Microsoft.Office.Interop.Outlook;
using OutLook = Microsoft.Office.Interop.Outlook;
and the code will be
object missing = System.Reflection.Missing.Value;
try
{
OutLook.MAPIFolder fldContacts = null;;
OutLook._Application outlookObj = new OutLook.Application();
string folderName = "Default";
fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
//LOOPIN G THROUGH CONTACTS IN THAT FOLDER.
foreach (Microsoft.Office.Interop.Outlook._ContactItem contactItem in fldContacts.Items)
{
StringBuilder strb = new StringBuilder();
strb.AppendLine((contactItem.FirstName == null) ? string.Empty : contactItem.FirstName);
strb.AppendLine((contactItem.LastName == null) ? string.Empty : contactItem.LastName);
strb.AppendLine(contactItem.Email1Address);
strb.AppendLine(contactItem.Business2TelephoneNumber);
strb.AppendLine(contactItem.BusinessAddress);
//write to text file
StreamWriter sw = null;
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + strb.ToString());
sw.Flush();
sw.Close();
}
}
catch (System.Exception ex)
{
throw new ApplicationException(ex.Message);
}