I am trying to write a windows service which check my outlook inbox every minute.
I have the following code setup.
protected override void OnStart(string[] args)
{
timeDelay = new System.Timers.Timer(30000);
timeDelay = new System.Timers.Timer();
timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);
timeDelay.Enabled = true;
}
public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)
{
ReadMyEmail();
}
private void ReadMyEmail()
{
string content;
Application outlookApplication = null;
NameSpace outlookNamespace = null;
MAPIFolder inboxFolder = null;
Items mailItems = null;
try
{
outlookApplication = new Application(); // m getting the error here...
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
mailItems = inboxFolder.Items;
foreach (MailItem item in mailItems)
{
if (item.UnRead)
......
...... all the code for reading emails.
}
}
}
I am getting the following error while i am trying to debug the application.
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
What am I doing wrong here.. I am just trying to read emails using a service.
This should not have been written as a Windows Service. For a "scheduled" application, you could have written this as a normal console application and setup the schedule to run it using Windows Scheduler. You can also set it up to run as you instead of as the system.
I am having following problem. I have a window that opens and allows me to select some files.
Then I can right-click on that window and choose to attach paths of selected files to a new mail dialog.
The workflow is like this:
I open my windows and select couple of files
Right-click, choose to add selected files paths to MailItem
Logic will check if there is an ActiveInspector
3.1. If there is one, I get its CurrentItem as MailItem (so, new mail dialog exists and does not need to be created)
3.2. If there is none, I call CreateItem(Microsoft.Office.Interop.OLItemType.olMailItem) to create
new mail dialog and then I call MailItem.Display(false) to display
the mail item dialog
Next I loop through list of selected files paths and add them to the new mail dialog. This works great.
PROBLEM If I open my window for the 2nd time to select more files and add their paths to the same mail dialog I opened earlier, they are not added.
Here is the code:
public void AddFilePaths(List<string> paths)
{
if (paths.Count > 0)
{
var inspector = MyAddIn.Application.ActiveInspector();
MailItem mi = null;
bool newMailItem = false;
if (inspector != null)
{
// If new mail dialog is already open, just get it.
// This is called on my 2nd attempt to add paths to new mail.
// This MailItem is the same one created on 1st call in below
// else block. I confirmed that by adding some dummy email
// Body in below else block, then checking for it here on
// 2nd call. I think this proves that correct
// Inspector/MailItem is returned here.
mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
}
else
{
// create new mail dialog and display it
// this is called on my 1st call to add paths to new mail
mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mi.Body = "Dummy email body";
newMailItem = true;
}
if (newMailItem)
{
mi.Display();
inspector = MyAddIn.Application.ActiveInspector();
}
if (inspector != null)
{
foreach (var path in paths)
{
AddPathToActiveInspector(path);
}
}
}
}
The code above calls this method to add path to the current ActiveInspector WordEditor:
public void AddPathToActiveInspector(string path)
{
var inspector = MyAddIn.Application.ActiveInspector();
dynamic we = inspector.WordEditor;
dynamic word = we.Application;
const string nl = "\n";
// I have noticed that if I am debugging, this line will throw error
// "COMException was unhandled by user code", "An exception of type
// System.Runtime.Interop.Services.COMException occurred in
// System.Dynamic.dll but was not handled by user code:
// Message: This command is not available
// InnerException: null
// I have also seen following error on 2nd attempt: "The TypeText
// method or property is not available because the document is
// locked for editing."
word.Selection.TypeText(nl);
string address = path;
string subAddress = "";
string screenTip = "";
string displayText = path;
word.ActiveDocument.Hyperlinks.Add(word.Selection.Range, ref address, ref subAddress, ref screenTip, ref displayText);
word.Selection.TypeText(" ");
}
I would simply create a new email message each time you are adding your paths to avoid the possibility of adding paths to a wrong email message which could happen if you have opened multiple emails.
Add a reference to Microsoft.Office.Interop.Word to your project
Add using Microsoft.Office.Interop.Word on top of your class file
Here is the code:
public void AddFilePaths(List<string> paths)
{
if (paths.Count > 0)
{
MailItem mi = ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mi.Display();
if (mi!= null)
{
foreach (var path in paths)
{
AddPathsToNewEmailMessage(path);
}
}
}
}
The code above calls this method to add the path to the new email message WordEditor:
public void AddPathsToNewEmailMessage(string path)
{
object link = url;
object result = "url";
object missing = Type.Missing;
string nl = "\n";
var inspector = ThisAddIn.Application.ActiveInspector();
MailItem currMessage = inspector.CurrentItem;
Word.Document doc = currMessage.GetInspector.WordEditor;
Word.Selection sel = doc.Windows[1].Selection;
doc.Hyperlinks.Add(sel.Range, ref result, ref missing, ref missing, ref link, ref missing);
sel.EndKey(Word.WdUnits.wdLine);
sel.InsertAfter(nl);
sel.MoveDown(Word.WdUnits.wdLine);
}
I develop an outlook add-in using Visual studio 2013 and Add-in express v.7.7.4087.
I have to deal with multiple email accounts (stores). Please see following snapshot and code
private void timerSendFromDraftsFolder_Tick(object sender, EventArgs e)
{
Outlook.Stores stores = null; // CC and OL accounts,
Outlook.Store store = null;
Outlook.MAPIFolder rootFolder = null;
Outlook.Folders rootFolderFolders = null;
Outlook.MAPIFolder draftsFolder = null;
Outlook.Items items = null;
Outlook.MailItem mailItem = null;
bool itemSent = true;
bool allMailItemsSent = true;
try
{
if (Helper.IsOnline())
{
Debug.DebugMessage(3, "AddinModule : timerSendFromSaleswingsFolder_Tick : Fired");
string version = OutlookApp.Version;
if (String.Compare(version, "13") > 0)
{
stores = Globals.ObjNS.Stores;
for (int i = 1; i <= stores.Count; i++)
{
try
{
store = stores[i];
string storeName = store.DisplayName;
if (store.ExchangeStoreType != Outlook.OlExchangeStoreType.olExchangePublicFolder)
{
rootFolder = store.GetRootFolder();
rootFolderFolders = rootFolder.Folders;
if (rootFolderFolders != null)
{
try
{
draftsFolder = rootFolderFolders["drafts"]; // not working for "xxxxxxx#outlook.com" type email accounts
}
catch (Exception )
{
Debug.DebugMessage(3, "AddinModule : timerSendFromSaleswingsFolder_Tick : Excep");
draftsFolder = rootFolderFolders["Drafts (This computer only)"];
}
}
I need to access the drafts folder of each mail account, but the email account of “xxxxxxx#outlook.com“ shows drafts folder as "Drafts (This computer only)" instead of "drafts".
I works fine for me. But I don’t like to introduce this to the production version. Becaues I think this will not work for non-English environments.
Can you please suggest me a solution for that
In redemption (http://www.dimastr.com/redemption/home.htm), is there a solution for that?
P.S
I have used this code in some of my projects
oFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);
But it gives the drafts folder of primary mail account. In my code there is no such method for the “store” object here.
Use the GetDefaultFolder method of the Store class instead. It allows to get a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument.
This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.
The Redemption library provides the GetDefaultFolder method of the RDOStore class.
GetSharedDefaultFolder is the way to go - call Namespace.CreateRecipient / Recipient.Resolve / Namespace.GetSharedDefaultFolder.
I am in the process of writing an app that will use the Outlook API to copy Calendar Items from a shared calendar to my personal calendar. Here is what I have so far...
using Outlook = Microsoft.Office.Interop.Outlook;
public Outlook.Items GetPublicEntries(string calendar)
{
Microsoft.Office.Interop.Outlook.Items CalendarFolderItems = null;
Outlook.Application oApp;
oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
//oNS.Logon(Missing.Value, Missing.Value, true, true);
Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient(calendar);
Outlook.MAPIFolder usersCalendarFolder = (Outlook.MAPIFolder)oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);
CalendarFolderItems = usersCalendarFolder.Items;
return CalendarFolderItems;
}
static void Main(string[] args)
{
String[] Cals = { "Appointments","Deadlines","Hearings"};
foreach (string cal in Cals)
{
CalendarItems calobj = new CalendarItems();
Outlook.Items calobjs = calobj.GetPublicEntries(cal);
foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in calobjs)
{
try
{
Console.WriteLine(item.Subject + " -> " + item.Start.ToLongDateString() + " - " + item.GlobalAppointmentID);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Console.ReadKey();
}
}
I am able to return a list of items from the three calendars, but now I need to copy them to my personal calendar and that is where I am stuck. Anyone know how to go about doing this?
Thanks!
Tony
Outlook will not let you copy directly to a specified folder (unlike AppointmentItem.Move() which takes a MAPIFolder object as a parameter) - use AppointmentItem.Copy, followed by AppointmentItem.Move:
AppointmentItem copiedItem = Item.Copy();
AppointmentItem newItem = copiedItem.Move(YourDestinationFolder);
newItem.Save();
Beware that Outlook will wipe out the global appointment id when you call Copy() - this means message updates, even if they come directly to your Inbox, will not find the appointment.
You can avoid the intermediary step if you use Redemption (I am its author) - its RDOAppointmentItem (derived from the RDOMail object) lets you pass the target folder when calling CopyTo().
We're currently developing a plug-in for a piece of software in C#. This plug-in extracts the contents of a PST file and stores all items in it as text files (with the exception of attachments, which are stored as their type in the same folder as the email).
It has been working without issue until we tested it on Windows 7 w/ Outlook 2K7. After running the same previous job on a machine with Outlook 2000 on it, we noticed that there were over 12,000 files missing. These files turned out to be attachments (mostly URLs)
We found that the issue is that Outlook 2K7 blocks attachments with specific extensions. If you open the email in Outlook itself, you see a blue bar at the top stating "Outlook blocked access to the following potentially unsafe attachments" and all the attachments in the emails.
Is there a way to programmatically get these attachments without Outlook blocking them?
The code we use to save the attachments is:
private void saveAttachment(ref object oEmail, StoreInfo currentStoreInfo, string sEmailID, string sExportPath)
{
int iAttachCount = 0;
object oAttach = null;
oAttach = getNextAttachment(oEmail, ref iAttachCount);
while (oAttach != null)
{
saveAttachment(sEmailID, sExportPath, oAttach);
oAttach = getNextAttachment(oEmail, ref iAttachCount);
}
}
private object getNextAttachment(object oEmail, ref int iAttachCount)
{
object oAttach = null;
try
{
iAttachCount++;
oAttach = GetProperty(oEmail, "Attachments", new object[] { iAttachCount });
}
catch //(Exception ex)
{
// There was no attachment to be gotten
oAttach = null;
}
return oAttach;
}
Just putting this on here in case anybody else runs into the same issue. Outlook 2K7 has a Level1 file type feature that blocks access to attachments with specific extensions.
However, you can change the registry to allow access to these files. Just remember to set it back to the way it was prior to you modifying it for security's sake.
private void SetLevel1RemoveValues()
{
// Get the base key for the current user HKEY_CURRENT_USER
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, "");
// Get the security key from the registry
Microsoft.Win32.RegistryKey subKey = regKey.OpenSubKey("Software\\Microsoft\\Office\\" + sCurrentOutlookVersion + ".0\\Outlook\\Security", true);
// If the Outlook\Security key doesn't exit, create one
if (subKey == null)
subKey = regKey.CreateSubKey("Software\\Microsoft\\Office\\" + sCurrentOutlookVersion + ".0\\Outlook\\Security");
// Loop through each Value in the registry to see if the Level1Remove key already exists.
string[] sValues = subKey.GetValueNames();
bool bHasLevel1RemoveKey = false;
foreach (string sValue in sValues)
if (sValue == "Level1Remove")
bHasLevel1RemoveKey = true;
// If the key already exists, store the data so we can reset it later
if (bHasLevel1RemoveKey)
sPrevLevel1RemoveValues = subKey.GetValue("Level1Remove").ToString();
else
sPrevLevel1RemoveValues = "";
// Create an array of all Level 1 Extensions
string[] level1Extensions = new string[] { ".ade", ".adp", ".app", ".asp", ".bas", ".bat",
".cer", ".chm", ".cmd", ".com", ".cpl", ".crt", ".csh", ".exe", ".fxp", ".gadget",
".hlp", ".hta", ".inf", ".ins", ".isp", ".its", ".js", ".jse", ".ksh", ".lnk",
".mad", ".maf", ".mag", ".mam", ".maq", ".mar", ".mas", ".mat", ".mau", ".mav", ".maw",
".mda", ".mdb", ".mde", ".mdt", ".mdw", ".mdz", ".msc", ".msi", ".msp", ".mst", ".ops",
".pcd", ".pif", ".pfr", ".prg", ".pst", ".reg", ".scf", ".scr", ".sct", ".shb", ".shs",
".tmp", ".url", ".vb", ".vbe", ".vbs", ".vsmacros", ".vss", ".vst", ".vsw",
".ws", ".wsc", ".wsf", ".wsh" };
// Set the value in the registry to the list of all Level 1 extensions so we can extract them
subKey.SetValue("Level1Remove", string.Join(";", level1Extensions));
// Close (and save) the values
subKey.Close();
regKey.Close();
}