Get unread Mails from Outlook - c#

Is there any way to get all mail from an specific Folder into my Application?

Check this link. Introduction to Outlook Programming will explain things more clearly.
You could loop through the mailitems. Sample code
using System.Runtime.InteropServices;
using OutLook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
OutLook.Application oApp;
OutLook._NameSpace oNS;
OutLook.MAPIFolder oFolder;
OutLook._Explorer oExp;
oApp = new OutLook.Application();
oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
OutLook.Items items = oFolder.Items;
foreach (OutLook.MailItem mail in items)
{
if (mail.UnRead == true)
{
}
}
Edit:
Reference other folders
oFolder.Folders["Foldername"]
OutLook Code
Common Outlook tasks

Looping through all items in a folder is a terrible idea, especially if you are working against an online Exchange store. Items.Find/FindNext or Items.Restrict is the way to go.
Find/FindNext:
OutLook.Items items = oFolder.Items;
OutLook.MailItem mail = items.Find("[Unread] = true");
while (mail != null)
{
MessageBox.Show(mail.Subject);
mail = items.FindNext();
}
Items.Restrict:
OutLook.Items items = oFolder.Items.Restict("[Unread] = true")
foreach (OutLook.MailItem mail in items)
{
MessageBox.Show(mail.Subject);
}

There's some examples of accessing Outlook folders here, one of which deals specifically with unread mail.
Edit: There's a KB article specifically about accessing folders from C#, Programming samples that can reference items and folders in Outlook by using Visual C# .NET
To open another user's folder, use GetSharedDefaultFolder

foreach (Object Unreadmail in folderItems)
{
if ((Unreadmail as Outlook.MailItem) != null && (Unreadmail as Outlook.MailItem).UnRead == true)
{
//DO Your action Here
}
}
I have experienced "COM_object" exception error with above solutions, More info please refer here

Related

Expand exchange online public folders from Outlook VSTO addin

I have a simple VSTO outlook addin that needs to move email messages into public folders. It works fine except when outlook is first launched. After a first launch of outlook (office 365 business) if I try and use the addin to move a message into a public folder using GetFolderFromID(IDValueStoredInMyAddin) I get an error "The operation failed. The messaging interfaces have returned an unknown error. If the problem persists, restart Outlook". If after starting outlook the user manually expands the public folders tree then the addin works without this error. Any ideas what is going on here? Is there a way from within a VSTO addin for expand public folders?
Outlook initializes stores on first access. Try to specify the store entry id (second parameter when calling GetFolderFromID) or open the store first using Namespacve.GetStorefromID followed by Store.GetFolderFromID
I just had the same problem and looks like it is working with this:
public string GetStoreID()
{
if (Properties.Settings.Default.SharedFolderEmail != "")
{
Outlook._NameSpace nSpace = Application.GetNamespace("MAPI");
Outlook.Recipient recip = nSpace.CreateRecipient(Properties.Settings.Default.SharedFolderEmail);
if (recip.Resolve())
{
Outlook.Folder root = nSpace.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
return root.StoreID;
}
else
{
return "";
}
}
else
{
Outlook.Folder root = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
return root.StoreID;
}
}
Outlook._NameSpace nSpace = Application.GetNamespace("MAPI");
_Archive = nSpace.GetFolderFromID(FolderToMonitor_EntryID, GetStoreID());
_Archive.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ItemAdded);
GC.KeepAlive(_Archive);

Move email to another folder with OpenPop

How I can to move emails from inbox to some folder, for example folder "test"
Pop3Client client = new Pop3Client()
client contains method to get email in html, xml, etc. also delete email or delete all emails, but I need to move some email to another folder, it is possible ?
OpenPop implements the POP3 protocol. This protocol is old, and does not know about such things as folders. Therefore, the OpenPop implementation cannot handle folders as well.
If you need to use folders, consider using some IMAP client instead. IMAP is a newer and more modern protocol.
As J. Steen has pointed out. No, you can't with OpenPop.
Just encase you still want to do it and it wasn't a purely academic question. Taken from MSDN
How to: Programmatically Move Items in Outlook
This example moves unread e-mail messages from the Inbox to a folder named Test. The example only moves messages that have the word Test in the Subject field.
Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new Microsoft.Office.Interop.Outlook.
ApplicationEvents_11_NewMailEventHandler
(ThisAddIn_NewMail);
}
private void ThisAddIn_NewMail()
{
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = (Outlook.Items)inBox.Items;
Outlook.MailItem moveMail = null;
items.Restrict("[UnRead] = true");
Outlook.MAPIFolder destFolder = inBox.Folders["Test"];
foreach (object eMail in items)
{
try
{
moveMail = eMail as Outlook.MailItem;
if (moveMail != null)
{
string titleSubject = (string)moveMail.Subject;
if (titleSubject.IndexOf("Test") > 0)
{
moveMail.Move(destFolder);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

Change the status of mail as Read in outlook

i have written a code that will read all unread mails from Outlook 2010 and write them in a file. After that i want to change the status of the mails as Read in outlook.
How do i do it?
I am using Interop for accessing the mails.
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.Items unreadItems = null;
app = new Microsoft.Office.Interop.Outlook.Application();//.CreateItem(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
ns = app.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
unreadItems = inboxFolder.Items.Restrict("[Unread]=true");
Here are some link that could help you:
http://msdn.microsoft.com/en-us/library/aa289167%28VS.71%29.aspx (it is for Outlook 2003 but it still contains very usefull info)
http://msdn.microsoft.com/en-us/library/ms268731%28VS.80%29.aspx
I've a code sample that could help you:
OutLook.Application oApp;
OutLook._NameSpace oNS;
OutLook.MAPIFolder oFolder;
OutLook._Explorer oExp;
oApp = new OutLook.Application();
oNS = (OutLook._NameSpace)oApp.GetNamespace("MAPI");
oFolder = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderInbox);
oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
OutLook.Items items = oFolder.Items;
foreach (OutLook.MailItem mail in items)
{
if (mail.UnRead)
{
mail.UnRead = false;
mail.Save();
}
Marshal.ReleaseCOMObject(mail);
}
Marshal.ReleaseCOMObject(items);
// Dont forget to free all other object, using Marshal.ReleaseCOMObject then close oApp
Please note I've not tested if it works or even compile.
On of general rule with outlook dev is that you need to release ALL com object otherwise you can have strange behavior (save popup when closing outlook app, or even the outlook never close etc.)
EDIT:
I would advice to you indeed use the Restrict method to get only unred mail, because my snippet above will loop in all emails which could be unnecessary and not performant.
Set the OutLook.MailItem's UnRead property to false

how to save mail form outlook with attachments?

I wanted to save Outlook mails in to msg format along with the attachment through C#.
I tried the following code
using Outlook = Microsoft.Office.Interop.Outlook;
private void button1_Click(object sender, EventArgs e)
{
Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
foreach (Outlook.MailItem item in inbox.Items)
{
item.SaveAs(finename, Outlook.OlSaveAsType.olMSG);
}
}
It could save the mail as msg but the attachment part was removed. SaveAs method had no other overloads alos... :(
If i try to save a message from outlook it saves the message along with the attachment embedded in it. Any idea how this can be achieved..?
I am using .Net Framework 3.5 and Outolook 2007
What are you using as a filename? does it end with .msg?
I do something like this and it works as you describe you want it too:
Outlook.MailItem msg;
foreach (object obj in f.Mapi.Items)
{
try
{
msg = obj as Outlook.MailItem;
// ... set file name using message attributes
// string fullPath = "something" + ".msg"
msg.SaveAs(fullPath, Outlook.OlSaveAsType.olMSG);
}
}
The reason I'm so curious in your case is that I am wondering how I can reproduce what you are doing: saving the mail item with out saving the attachments?
I believe that you will have to save them separately.
Use the Attachments property on the MailItem to get all the attachments. then loop through them and call SaveAsFile() for each of the attachments.
examples in below link are for basic, but it should work in C# as well
MailItem::Attachments
http://msdn.microsoft.com/en-us/library/bb207129.aspx
Attachment::SaveAsFile
http://msdn.microsoft.com/en-us/library/bb219926.aspx

Creating a new Outlook store

I am creating a pst file in Outlook using C#, and Addin express.
The new pst file ("My Inbox") should have Inbox,Sent Items, Outbox.
Outlook.Folders olFolders = olNamespace.Folders;
foreach (Outlook.MAPIFolder olTmpFolder in (IEnumerable) olFolders)
{
if(olTmpFolder.Name == "My Inbox")
{
olTmpFolder.Folders.Add("Inbox", Outlook.OlDefaultFolders.olFolderInbox);
olTmpFolder.Folders.Add("Sent", Outlook.OlDefaultFolders.olFolderSentMail);
olTmpFolder.Folders.Add("Outbox", Outlook.OlDefaultFolders.olFolderOutbox);
}
}
But I can only see the Inbox folder .
If there are better ways please let me know.
Thanks
Sujay
Try this and let me know if it works:
Outlook.Folders olFolders = olNamespace.Folders;
foreach (Outlook.MAPIFolder olTmpFolder in (IEnumerable) olFolders)
{
if(olTmpFolder.Name == "My Inbox")
{
olTmpFolder.Folders.Add("Inbox", missing) as Outlook.Folder;
olTmpFolder.Folders.Add("Sent", missing) as Outlook.Folder
olTmpFolder.Folders.Add("Outbox", missing) as Outlook.Folder
}
}

Categories