Introduction
In this article I will give you an example of how to add an e-mail to your Microsoft Outlook outbox folder using C# and/or VB.net. This example also show how easy it is to call functions written in VB.net from C#
The code consists of three classes: Form1.cs, CSharp.OutlookMail.cs, VBNET.OutlookMail.vb.
Form1: a simple Windows Forms which shows how easy it is to call a C# or VB.net function.
CSharp.OutlookMail.cs: C# class with one function to add an e-mail to outlook outbox.
VBNET.OutlookMail.cs: VB.net class with one function to add an e-mail to outlook outbox.
The first thing you need to do is to add a reference to "Microsoft Outlook 9.0 Object Library" Click on add Reference, select the COM tab and select "Microsoft Outlook 9.0 Object Library".
public class OutlookMail
{
private Outlook.Application oApp;
private Outlook._NameSpace oNameSpace;
private Outlook.MAPIFolder oOutboxFolder;
public OutlookMail()
{
//Return a reference to the MAPI layer
oApp = new Outlook.Application();
The Namespace object represents the messaging service provider. In order to get access to all Outlook folders and items we have to use the MAPI namespace.
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
Now that we have the MAPI namespace, we can log on using using:
<mapinamespace>.Logon(object Profile, object Password, object ShowDialog, object NewSession)
Profile: This is a string value that indicates what MAPI profile to use for logging on. Set this to null if using the currently logged on user, or set to an empty string ("") if you wish to use the default Outlook Profile.
Password: The password for the indicated profile. Set to null if using the currently logged on user, or set to an empty string ("") if you wish to use the default Outlook Profile password.
ShowDialog: Set to True to display the Outlook Profile dialog box.
NewSession: Set to True to start a new session or set to False to use the current session.
oNameSpace.Logon(null,null,true,true);
We now choose which folder we want to work with. A MAPIFolder object represents a single Outlook folder. For example you could use:
Calender: Outlook.OlDefaultFolders.olFolderCalendar
Contacts: Outlook.OlDefaultFolders.olFolderContacts
Inbox: Outlook.OlDefaultFolders.olFolderInbox
For this example we choose the Outbox folder
//gets defaultfolder for my Outlook Outbox
oOutboxFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
}
The following function takes 3 string as parameters. These will be the values that we will add to the to, subject and the email body fields. We create a MailItem, and set the To, Subject, and Body fields.
public void addToOutBox(string toValue, string subjectValue, string bodyValue)
{
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = toValue;
oMailItem.Subject = subjectValue;
oMailItem.Body = bodyValue;
oMailItem.SaveSentMessageFolder = oOutboxFolder;
//uncomment this to also save this in your draft
//oMailItem.Save();
//adds it to the outbox
oMailItem.Send();
}
}
Conclusion:
Microsoft .NET is extremely powerful and yet simple to work with. In this example, I showed how to add e-mail to Outlook outbox. In the next verion, I will add functions to add tasks, calender and contacts items.
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
sa zaPosted Jun 4, 2012, 11:19 AM
private Outlook.Application oApp; private Outlook._NameSpace oNameSpace; private Outlook.MAPIFolder oOutboxFolder; These declaration are having showing missing directives. I already add the reference for the outlook. Any suggestions
JayaPosted Apr 18, 2012, 6:45 AM
plsss
MattPosted Feb 3, 2012, 2:45 PM
Is there any way we can set sender of the Outlook email message? Something like "oMailItem.From = [email protected]"? Or How to set a sender of Outlook email in C#?
bindiyaPosted Aug 31, 2011, 8:52 AM
private void btnattach_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); if (openFileDialog1.FileName.Length > 0) { //Outlook.MailItem mitem2 = this.app.CreateItem(Outlook.OlItemType.olMailItem); Outlook.MailItem mitem1 = (Outlook.MailItem)this.app.CreateItem(Outlook.OlItemType.olMailItem); string sSource = (string)openFileDialog1.FileName; //String sDisplayName = "MyFirstAttachment"; //double iPosition = (double)mitem1.Body.Length + 1; //int iAttachType = (int)Outlook.OlAttachmentType.olByValue; ////Outlook.Attachment oAttach = mitem1.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); Outlook.Attachment attach = mitem1.Attachments.Add(sSource, Outlook.OlAttachmentType.olByValue, 1, "1ST Attachment"); }
bindiyaPosted Aug 31, 2011, 8:50 AM
How to get the background colour of an email sent through outlook using C# changed based on colour selected by the user on btn_click from the colordialogbox and background image of emails changed based on image selected by user from opendialogbox.Pasting my code below:- private void btnSend_Click(object sender, EventArgs e) { try { Outlook.MailItem omsg = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem); omsg.To = txtTo.Text; omsg.Subject = txtSub.Text; omsg.Body = txtcontent.Text; omsg.Send(); omsg = null; app = null; txtcontent.Text = ""; txtTo.Text = ""; txtSub.Text = ""; } catch (Exception ex) { } } private void BackBlue_Click(object sender, EventArgs e) { Outlook.MailItem mitem3 =(Outlook.MailItem)this.app.CreateItem(Outlook.OlItemType.olMailItem); Form form1 = new Form(); ColorDialog cd = new ColorDialog(); cd.ShowDialog(); form1 = cd.Color; }
bindiyaPosted Aug 31, 2011, 8:49 AM
How to get the background colour of an email sent through outlook using C# changed based on colour selected by the user on btn_click from the colordialogbox and background image of emails changed based on image selected by user from opendialogbox.Pasting my code below:- private void btnSend_Click(object sender, EventArgs e) { try { Outlook.MailItem omsg = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem); omsg.To = txtTo.Text; omsg.Subject = txtSub.Text; omsg.Body = txtcontent.Text; omsg.Send(); omsg = null; app = null; txtcontent.Text = ""; txtTo.Text = ""; txtSub.Text = ""; } catch (Exception ex) { } } private void BackBlue_Click(object sender, EventArgs e) { Outlook.MailItem mitem3 =(Outlook.MailItem)this.app.CreateItem(Outlook.OlItemType.olMailItem); Form form1 = new Form(); ColorDialog cd = new ColorDialog(); cd.ShowDialog(); form1 = cd.Color; }
Manish RajguruPosted Jun 9, 2011, 8:48 AM
Hi casper, Thanks for this nice article, I'm trying this code with a webapplication, but getting errror. Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005. But the sample code provided by you is working fine on same machine. So is it related with rights/previlages, or it is not at all possible with webapps? Please help Thanks.. Manish
Preeti SinghPosted Feb 22, 2011, 3:37 AM
I used above code to send mail. But at receiver end mails are moving to "Junk Mail". How to resolve this ?
zb ssPosted Jan 13, 2011, 6:36 AM
what about attaching Files..?
mohanad romanahPosted Aug 9, 2010, 1:51 AM
this for this pro but i need add the attachment file thx you
eliza sahooPosted Jun 2, 2010, 3:00 AM
Following is the code sample used to display the outlook folder dialog using VB.NET [VB.NET CODE STARTS] Dim objOutlook As Object Dim objOlNamespace As Object Dim objOlFolder As Object objOutlook = CreateObject("Outlook.Application") ' create outlook application object at the run time objOlNamespace = objOutlook.GetNamespace("MAPI") objOlFolder = objContactsNS.PickFolder ' displays the folder dialog
Abid ShaikPosted Apr 22, 2010, 11:37 AM
public partial class SendEmailOutlook : System.Web.UI.Page { private Outlook.Application oApp; //private Outlook._NameSpace oNameSpace; // we don't need this //private Outlook.MAPIFolder oOutboxFolder;//MAPIFolder depricated so don't use public SendEmailOutlook() // this is my Constructor { oApp = new Outlook.Application(); //oNameSpace = oApp.GetNamespace("MAPI"); // no need to use this //oNameSpace.Logon(null, null,true, true); // we don't need this aswell takes current automatically //oOutboxFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox); //don't need this since No Namespace is used } public void AddToOutBox(string toValue, string subjectValue, string bodyValue) { Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); oMailItem.To = toValue;// we can use Semicolon delimited string to send to many users. oMailItem.Subject = subjectValue; oMailItem.Body = bodyValue; oMailItem.BodyFormat = OlBodyFormat.olFormatRichText; //Copy of sent email is saved in Sent items folder out outlook by default oMailItem.Send(); } //I have used 3 text boxes in the page and then a Button and its click event handler is here protected void BtnSendEmail_Click(object sender, EventArgs e) { AddToOutBox(TxtBxTo.Text, TxtBxSubject.Text, TxtBxBody.Text); } }
Former memberPosted May 13, 2009, 8:00 AM
hello How to detect that outlook is installed in my computer; best regard
nirPosted Mar 1, 2009, 11:04 AM
Hello , I tried to use this code with outlook express with no success someone can help please ? Nir
Craig JenkinsPosted Oct 24, 2008, 10:32 AM
Thanks so musch this is great
Jon GoldmaneditedPosted Sep 13, 2008, 3:40 PMEdited Sep 13, 2008, 3:42 PM
Very helpful article and download. Add the line oMailItem.DeleteAfterSubmit = true; before the .Send line in addToOutbox in OutlookMail.cs to have the email removed from the Outbox after it sends. Also, I needed the following line at the top of OutlookMail.cs using Outlook = Microsoft.Office.Interop.Outlook;
a bPosted Mar 20, 2008, 7:03 AM
replay me soon
Don broadmanPosted Feb 1, 2008, 8:07 AM
I am using MS2005 with SQL server2005 , In my add reference->COM tab -> Microsoft Outlook 9.0 Object Library -option not available. so please tell me how to add reference in my application
Mohamed IqbalPosted Aug 31, 2007, 5:31 AM
Hi, The article is good. How to add attachments to the mail.
Jorge SaenzPosted Jun 1, 2007, 12:01 AM
Hi, Where can I find this library: Microsoft Outlook 9.0 Object Library I can´t find it on my .NET Thanks
shalini gandhiPosted May 19, 2007, 11:23 PM
What is MAPI folder? I couldnt understand MAPI ?