C# get mail and its attachment from outlook - c#

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);

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.

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

Outlook 2010 - C# - Get the account associated to a mail

I'm creating an Outlook add-in that can save selected emails to an external database.
Using the Office.IRibbonControl I can get the list of the selected email, but I need to know to which account those mails are associated.
I mean, if Outlook get messages from toto#exemple.com and from otot#exemple.com, when I want to save a message I need to know that information.
I can't use the informations like sender / receiver because it can be an outcome like an income email.
Currently, the only I have found is using the current folder path..
public void SayHello(Office.IRibbonControl control)
{
MessageBox.Show(
"Folder: " + (control.Context as Outlook.Explorer).CurrentFolder.FolderPath,
"Test",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
But the method isn't good enough. If I open a message (in a separated window) and then I change the current folder, it fails.
Also, Outlook.Explorer.CurrentAccount does not work as I expected.
So here is my question :
How can I access the related account having a Outlook.MailItem object ?
You can get the parent folder (MailItem.Parent) of an Outlook.MailItem to determine its Folder path (Folder.FolderPath).
Outlook.Folder parent = MailItem.Parent as Outlook.Folder;
string itemPath = parent.FolderPath;

Set a MailItem as sent before calling SaveAs in Outlook Addin with C#

I am building an Outlook 2010 addin to integrate it with some business software and have trapped the ItemSend Event. I check if it is a MailItem and if it is I call the SaveAs function to save it as a .msg to the file system (in the users temp folder).
void Application_ItemSend(object Item, ref bool Cancel)
{
if(Item is Outlook.MailItem)
{
Outlook.MailItem mailitem = (Outlook.MailItem)Item;
string filename = "somefilename.msg";
string path = System.IO.Path.GetTempPath();
string fullPathName = path+filename;
mailitem.SaveAs(fullPathName, Outlook.OlSaveAsType.olMSG);
}
}
I go on to read the file contents and send the file to the server using webservices. It all works fine.
The issue I have is if I go and open the file that it saves, then Outlook opens it as a message still being composed and the user could very easily click the Send button again.
Is there a way to flag that the item has been sent before it gets saved so when opened after the fact it opens as a readable email rather than an in-composition email?
Take a look at this post (and additionally at the last answer on that page), this might help you.
(Makes use of ItemAdd event)

Get email folder from MailItem via MAPI interface

I'm creating a program to automatically generate reports from incoming email attachments and it is almost complete save for one area. The incoming emails are automatically filtered into folders which differentiate which client and server they originate from. I can't figure out how to get the path of the folder from the email Item.
I'm using the NewMailEx event to call the method below and this.AppNamespace and this.ReportFolder are confirmed to be instantiated properly.
void AppClass_NewMailEx(string EntryIDCollection)
{
Outlook.MailItem Item = (Outlook.MailItem)this.AppNamespace.GetItemFromID(EntryIDCollection, this.ReportFolder.StoreID);
string FolderName = ""; //How do I get this?
}
The MSDN on MailItem is here. Am I missing something or approaching this the incorrect way?
I think there's a Parent that you can check - it should return a MAPIFolder that you can check the Name of.

Categories

Resources