I'm developing a C# program where you enter an Outlook category name and it opens an Outlook window, which shows all the mails received with that category tag.
For example:
The user enters the Category-Tag "Work" and it launches Outlook and shows all the mails he received and tagged with the Category "Work".
I was able to open the "Send Email" window, but that was not my intention:D
The code:
Outlook.Application outlookApp = new Outlook.Application();
Outlook._NameSpace clientNameSpace = (Outlook._NameSpace)outlookApp.GetNamespace("MAPI");
Outlook.PostItem postitem = (Outlook.PostItem) outlookApp.CreateItem(Outlook.OlItemType.olPostItem);
postitem.Display(true);
I assume by "Send Email" you mean opening a draft message that you can send? You need MailItem object, not PostItem:
Outlook.Application outlookApp = new Outlook.Application();
Outlook._NameSpace clientNameSpace = (Outlook._NameSpace)outlookApp.GetNamespace("MAPI");
Outlook.MailItem mailitem = (Outlook.MailItemItem) outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailitem.Subject = "My test message";
mailitem.Display(true);
You can create different types of items in Outlook using the CreateItem method.
The type of project you create depends on the parameters of CreateItem.
For item type, please see the following link::OlItemType Enum
Related
im working on a small outlook project and i just wanted to know, If i wanted to get the details of a selected email in the inbox such as the EmailID is there other ways to get those details besides ActiveExplorer and ActiveInspector. I am using this method to get the details i want right now:
Outlook.MailItem mailItem = null;
Outlook.Inspector inspector = Application.ActiveInspector();
mailItem = inspector.CurrentItem as Outlook.MailItem;
string EntryID = mailItem.EntryID;
I'm working on a VSTO project for Outlook. Very simple application with an icon on the ribbon. When user clicks on it, the selected email will be sent to the manager. It is working with the following code. But I want to add the sender as CC in the email. So whenever user clicks on it, the selected email will be sent to the manager and the user will be CC'd on it. So user will get a copy too. How can I CC the sender?
here is the code:
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application application = new Outlook.Application();
Outlook.NameSpace ns = application.GetNamespace("MAPI");
try
{
//get selected mail item
Object selectedObject = application.ActiveExplorer().Selection[1];
Outlook.MailItem selectedMail = (Outlook.MailItem)selectedObject;
//create message
Outlook.MailItem newMail = application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
newMail.To = "email#gmail.com";
newMail.CC = Outlook.Account;
newMail.Subject = "Subject Here";
newMail.Attachments.Add(selectedMail, Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem);
newMail.Send();
selectedMail.Delete();
System.Windows.Forms.MessageBox.Show("Message has been sent! You have been CC'd.");
}
catch
{
System.Windows.Forms.MessageBox.Show("You must select a message to report.");
}
}
}
}
First of all, I've noticed the following line of code in the event handler:
Outlook.Application application = new Outlook.Application();
There is no need to create a new Outlook Application instance in the ribbon event handler if you develop VSTO based add-in for Outlook. Instead, you need to use the ThisAddIn.Application property. You can start writing your VSTO Add-in code in the ThisAddIn class. Visual Studio automatically generates this class in the ThisAddIn.vb (in Visual Basic) or ThisAddIn.cs (in C#) code file in your VSTO Add-in project. The Visual Studio Tools for Office runtime automatically instantiates this class for you when the Microsoft Office application loads your VSTO Add-in. To access the object model of the host application, use the Application field of the ThisAddIn class. This field returns an object that represents the current instance of the host application. See Program VSTO Add-ins for more information.
Use the Recipients.Add method to add new recipients to the email. For example, a raw sketch:
var recipient = Item.Recipients.Add(ThisAddIn.Application.GetNameSpace("MAPI").CurrentUser.Address);
recipient.Type = OlMailRecipientType.olCC;
I am trying to search for specific emails that have a particulary subject.
Outlook.Folder inbox = new Outlook.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = inbox.Items;
Outlook.MailItem mailItem = null;
object folderItem;
string subjectName = string.Empty;
string filter = "[Subject] > 's' And [Subject] <'u'";
folderItem = items.Find(filter);
while (folderItem != null)
{
mailItem = folderItem as Outlook.MailItem;
if (mailItem != null)
{
subjectName += "\n" + mailItem.Subject;
}
folderItem = items.FindNext();
}
subjectName = "The follow e-mail messages were found: " + subjectName;
MessageBox.Show(subjectName);
I am getting an error :
"Severity Code Description Project File Line Suppression State
Error CS0426 The type name 'ActiveExplorer' does not exist in the type 'Application'"
You need to create a new Application instance first if you develop a standalone application where Outlook is automated or use the built-in property if you develop a VSTO based add-in instead of the following code:
Outlook.Folder inbox = new Outlook.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
A standalone application should create a new Outlook instance:
Outlook.Application app = new Outlook.Application();
Outlook.Explorer explorer = app.ActiveExplorer();
Outlook.Namespace ns = app.GetNamespace("MAPI");
Outlook.Folder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
In case of VSTO add-in you can use the Application property of the ThisAddin class:
Outlook.Explorer explorer = Application.ActiveExplorer();
Outlook.Namespace ns = app.GetNamespace("MAPI");
Outlook.Folder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
You can read more about the Find/FindNext or Restrict methods of the Items class in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Also you may find the AdvancedSearch method helpful. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
I am not sure why you are even accessing ActiveExplorer - you are not using it, and if Outlook was not running prior, there won't be any open explorers (and inspectors), so ActiveExplorer will return null anyway.
Also keep in mind that Application.Session will be null unless Outlook is already running - you need to log in first.
Thirdly, you are not calling a constructor - that would be new Outlook.Application().Blah (note ()).
Change your code to
Outlook.Application app = new Outlook.Application();
Outlook.Namespace session = app.GetNamespace("MAPI");
session.Logon();
Outlook.Folder inbox = session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Similar to this question,
Open Specific MailItem in Outlook from C# , In a C# VSTO application I'm trying to open an email in a new outlook window/inspector using the method GetFolderFromID and passing it's EntryID and StoreID.
Full code below:
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder mySentBox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.MailItem myMail = ((Outlook.MailItem)mySentBox.Items[1]);
string guid = myMail.EntryID;
string folderStoreID = mySentBox.StoreID;
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(guid, folderStoreID);
getItem.Display();
The below code only opens up the requested email in a new window when the email is already selected within outlook.
getItem.Display();
If not selected, the following error returns.
System.Runtime.InteropServices.ComException: 'A dialog box is open. Close it and try again'.
I've also attempted adding a new inspector & activating/displaying the email object via it with no success.
Kind regards
The error simply means a dialog box is open. Make sure there are no modal windows displayed and make sure you ever call MailItem.Display(true) to display items modally.
I am using Outlook.Application and Outlook.MailItem object for opening Outlook in my C# desktop application. My outlook doesn't display attachments although when I send the mail to myself, I receive mail with attachments. But it is not showing before sending mail (when outlook is open). I am using Outlook 2007. Below is my code:
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
......
//Check if we need to add attachments
if (_files.Count > 0)
{
foreach (string attachment in _files)
{
oMsg.Attachments.Add(attachment,Outlook.OlAttachmentType.olByValue,null,null);
}
}
oMsg.Save();
oMsg.Display(false);
Of course, Type.Missing is used to omit the parameter and use the default value in COM add-ins.
Also I'd suggest breaking the chain of calls and declaring each property or method call on a separate line of code. It will allow to release each underlying COM object instantly.
Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article in MSDN.