Change the status of mail as Read in outlook - c#

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

Related

Refresh inbox microsoft interop outlook

I have the following code to login to an outlook inbox to retrieve emails. If I don't find a certain email that I'm looking for I would like to refresh Inbox folder and try again. Is it as simple as running this function again?
I want the equivalent of clicking on "Send/Receive" button in outlook to fetch any new emails.
private Microsoft.Office.Interop.Outlook.Items loginEmail()
{
//TODO.............................................
//RDOSession session = new RDOSession();
//session.Logon();
//Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
//login
mapiNameSpace.Logon(null, null, false, false);
mapiNameSpace.Logon("login#mywebsite.com", "pass", false, true);
//Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;
// Console.WriteLine("Total : ", myItems.Count);
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// Console.Write(myInbox.Name);
Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
// Console.WriteLine("Total : ", inboxItems.Count);
//Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);
//mapiNameSpace.Logon("Profile", Missing.Value, false, true);
return inboxItems;
}
Call this function to fetch new mail
mapiNameSpace.SendAndReceive(false);
The NameSpace.SendAndReceive method initiates immediate delivery of all undelivered messages submitted in the current session, and immediate receipt of mail for all accounts in the current profile.
SendAndReceive provides the programmatic equivalent to the Send/Receive All command that is available when you click Tools and then Send/Receive.
If you don't need to synchronize all objects, you can use the SyncObjects collection object to select specific objects. For more information, see NameSpace.SyncObjects.
private void DirectSendAndReceiveCall(object sender, IRibbonControl control, bool pressed)
{
Outlook.NameSpace ns = OutlookApp.GetNamespace("MAPI");
ns.SendAndReceive(false);
if (ns != null) Marshal.ReleaseComObject(ns);
}
See How To: Perform Send/Receive in Outlook programmatically for more information.

Interact between Outlook and my WPF application

So, this may be a silly question (or something that's impossible), but I just wanted to ask in case anyone knows something. I'm trying to open Outlook (either through Office 365- Browser or through the Outlook Desktop Application) to compose an email. I got that working fine. What I want to know is, if there's a way that I can capture what was composed (like, Body, To, Subject, Attachments) in my WPF application so that I can update it on my end. Do you guys think if this is possible?
Here's the sample code I have: (For opening this in the browser)
string To = "abc#ftr.com";
string subject = "Test Email";
string body = "This is a test email, Please ignore";
string url = #"https://outlook.office.com/?path=/mail/action/compose&to=" + To + "&subject=" + subject + "&body=" + body;
System.Diagnostics.Process.Start(url);
And here's the code for opening it in Outlook Desktop App:
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMsg.Subject = "subject something";
oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
oMsg.HTMLBody = "Test Email";
oMsg.Attachments.Add("c:/temp/test.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
oMsg.Display(true);
Thank you!
You can hook the ItemSend event, which will give you a reference to the MailItem object that is about to be send. Here is some sample code I copied from the Microsoft Community Forums
public void SendEnMail(Office.IRibbonControl control) //OnAction Function
{
Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem myMail = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
myMail.Display(true);
Outlook.Application application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
string a = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
System.Windows.Forms.MessageBox.Show(a);
Cancel = true;
}

How can I get the received mails (or any other items ) in an addin C # for outlook 2010

I try this code :
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
mapiNameSpace.Logon(null, null, false, false);
mapiNameSpace.Logon("MyEmailID", "PasswordOfMyEmail", Missing.Value, true);
Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
MessageBox.Show(myInbox.Items.Count + "");
But the MessageBox display me 0 (myInbox.Items.Count).
So maybe I am not able to access my outlook account !!!
I use Visual Studio 2010.
Can someone help me please ?
Firstly, if you are in an add-in, you should never create a new instance of the Outlook.Application object - you already have it exposed to your addin through globals.
Secondly, do not call Logon - Outlook will do that for you. That method is really for the external applications, not addins.
Thirdly, you call Logon once with null for the profile name. If you do not want to pass an optional parameter, pass Missing.Value, not null. And the second call to Logon (why do you need it?) will do absolutely nothing. Passing any email will not work anyway - the parameter must be the profile name as seen in Control Panel | Mail | Show Profiles.
And most importantly, when do you make that call? Does your Inbox actually contain any messages? Or is it a brand new profile and Outlook did not yet have a chance to download the messages from the mailbox?
So you can use :
Explorer currentExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
MAPIFolder myInbox = Globals.ThisAddIn.Application.ActiveExplorer().CurrentFolder;
try
{
if ( (currentExplorer != null) && (currentExplorer.Selection != null) && (currentExplorer.Selection.Count > 0) )
{
object item = currentExplorer.Selection[1];
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
MessageBox.Show(myInbox.Items.Count + "");
}
}
}

Get unread Mails from Outlook

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

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

Categories