Synchronize single IMAP folder - c#

I am trying to write an application that presents the user with a list of mail headers from a selected folder from shared IMAP folder tree. Users of the application use Outlook 2007 or 2010, and have the shared IMAP folder tree mapped to their account.
I am able to access a folder and iterate over the mail items using Outlook Interop like this:
Outlook.Application outlook = new Outlook.Application();
Outlook._NameSpace ns = outlook.GetNamespace("MAPI");
Outlook.Folder folder = MyFolderFinder(...);
foreach (var item in folder.Items)
{
if (item is Outlook.MailItem)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;
// do something with mailItem
}
}
Unfortunately this method does not appear to synchronize the folder, so unless the user does a manual "Update Folder" using Outlook, they may see a stale list.
Is there a way to programmatically synchrionize a single IMAP folder using Outlook Interop?

Using the new mail receive event, I think you can do this.
private void ThisApplication_Startup(object sender, System.EventArgs e)
{
this.NewMail += new Microsoft.Office.Interop.
Outlook.ApplicationEvents_11_NewMailEventHandler(
ThisApplication_NewMail);
}
void ThisApplication_NewMail()
{
}
Reference: http://msdn.microsoft.com/en-us/library/ms268998(v=vs.80).aspx

Related

Outlook add-in error: The operation cannot be performed because the message has been changed

i am trying to create my own add-in for Outlook. My point is to extract some data from mails and then move these mails to Archive folder. When i open unread mails it works as i expect, but i got an error when i open unread mails in the moment when i am trying to move mail to Archive folder. I got an instance of mail from inspector. Here is some code.
Outlook.MailItem mail = inspector.CurrentItem as Outlook.MailItem;
var email = mail.UserProperties.Session.CurrentUser.Address;
Outlook.NameSpace ouNs = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.MAPIFolder baseFolder = ouNs.Folders[email];
var archiveFolder = findFolderRecursive(baseFolder, archiveFolderName);
mail.Move(archiveFolder);
Messages returned from the inspector disallow some methods. Try to track the Inspector.Close event, store the message entry id in a variable, and enable a timer (use Timer class from the Forms namespace - it runs on the same thread). When the timer fires, disable it, open the item by its entry id using Namespace.GetItemFromID, then move it.

Is it possible to get outlook Inspector.CurrentItem (MailItem) file path, which was opened from file explorer?

I have such code where I get an email from the active outlook inspector:
var app = new Microsoft.Office.Interop.Outlook.Application();
var inspector = app.ActiveInspector();
if(inspector?.CurrentItem is MailItem) {
MailItem mailItem = inspector.CurrentItem;
}
Email was opened from windows desktop.
Is it possible to get file path, where is this email physically located?
No, because once it's loaded it's no different than any other Mailitem that wasn't loaded from the file system, i.e. one that you opened from an explorer window.

how to access my yahoo or gmail account in outlook (c#)?

I have 3 data files:
Outlook data file (is empty)
Gmail data file (from my gmail pop3)
Yahoo data file (from my yahoo imap)
I can access the Outlook data file inbox (which is always empty, I don't know how to automatically move from my google and yahoo account to my outlook data file) with
this code:
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
I have 2 questions:
How can I make my gmail and yahoo account automatically move to my outlook data file?
How to code to access my inbox in my gmail and yahoo account?
I have tried this function to make return to my MAPIFolder:
public Outlook.MAPIFolder GetInbox(string userName)
{
Outlook.Application oOutlook = new Outlook.Application();
Outlook.NameSpace oNs = oOutlook.GetNamespace("MAPI");
Outlook.Recipient oRep = oNs.CreateRecipient(userName);
Outlook.MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, Outlook.OlDefaultFolders.olFolderInbox);
return inbox;
}
But it didn't work. help me pleaseee..
I'm not sure I understand your first question, would you like to join all the data files into the main outlook data file?
About your second question, Outlook data files are represented by stores in the API, and if you call GetDefaultFolder on the Session object, you'll always end up with getting the default store's default folder (Inbox of your Outlook data file).
You can list all the store files and use GetDefaultFolder on them to retrieve the inbox folders for each store/data file:
Outlook.Stores stores = this.Appliction.Session.Stores;
foreach( Store store in stores )
{
Outlook.Folder inboxOfStore = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//Do stuff with your inbox folder.. Check store properties for infomation on which data file this store represents
}
See documentation for more information:
http://msdn.microsoft.com/en-us/library/office/bb176405%28v=office.12%29.aspx

C# get mail and its attachment from outlook

I've made program which one of the purposes is to open OutLook client where user can write his email, add attachments etc. After sending I want to get all attachments which were added to email, as well as all email in my program.
I tried to handle close event
((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)email.oMsg).Close += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CloseEventHandler(GetAttachmentsInfo);
and then
if (email.oMsg.Attachments.Count > 0)
{
foreach (Microsoft.Office.Interop.Outlook.Attachment at in email.oMsg.Attachments )
{ attachments.Add(at); }
}
email is OutlookEMail
oMsg is Email Item
attachments is List<Attachment>
but when I close client Attachment throws exception that cannot find object. as well as these in List.
I know that Microsoft.Office.Interop.Outlook.Attachment is not a file, but only something like path to this file containing it name and size.
So question : is it possible to save attachments after client is closed in my program?
(Without using Email.SaveAttachments os SaveEmail methods, because it uses time and computer space)?
Here are two other options you could try:
1. You could listen for MailItem additions to the SentItems Folder via Folder.ItemAdd.
Outlook.Folder sentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
sentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(GetAttachmentsInfo);
2. You could attach to the MailItem.Send event.
((Outlook.ItemEvents_10_Event)MailItem).Send += new Outlook.ItemEvents_10_SendEventHandler(GetAttachmentsInfo);

How to close all the activemail msg windows automatically after saved in draft

I am new to C# and trying to find a way to close windows after all new messages are saved in the draft folders.
I have the following code which does close all message windows, but at the same time, it shuts down the entire outlook.
I think there are two ways to resolve but don't know how to code...
close all active new message windows without shutting down outlook
relaunch outlook inbox after shutting down outlook.exe
// now generate emails
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem newMail = oApp.CreateItem(Outlook.OlItemType.olMailItem);
newMail.Subject = subject_header;
newMail.Recipients.Add(email_add[i]);
newMail.Recipients.ResolveAll();
newMail.Body = body;
newMail.HTMLBody += summary_html;
//Add attachment
foreach (string attachment in attachments)
{
newMail.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue);
}
newMail.Display(false);
newMail.Save();
oApp.Quit()

Categories

Resources