Blue Theme Orange Theme Green Theme Red Theme
 
Click Here for 3 Month Free of ASP.NET Hosting!
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Internet & Web » Sending Emails Through Outlook using C# and VB.NET

Sending Emails Through Outlook using C# and VB.NET

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.

Total page views :  89077
Total downloads :  2422
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SendingEmailsThroughOutlookCB.zip
 
Become a Sponsor


Download Free Book

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.


Login to add your contents and source code to this article
 About the author
 
casper boekhoudt
Caspar Boekhoudt is a consultant for Information Methodologies Inc. (imi), higher ed's leading enterprise web integrator. He has a M.S. in Computer Science and is a Sun Certified Programmer for the Java 2 Platform and IBM Certified Developer, XML and Related Technologies V1.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SendingEmailsThroughOutlookCB.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
MAPI FOLDER by shalini On May 19, 2007
What is MAPI folder? I couldnt understand MAPI ?
Reply | Email | Delete | Modify | 
Re: MAPI FOLDER by kondala On March 11, 2008

MAPI means  Microsoft Application Programming interface..

MAPI is inbuit one with the Microsoft Exchnage Server for mailing functionality using Outlook..

Reply | Email | Delete | Modify | 
Don´t have library by Jorge On June 1, 2007
Hi, Where can I find this library: Microsoft Outlook 9.0 Object Library I can´t find it on my .NET Thanks
Reply | Email | Delete | Modify | 
How to add attachments by Mohamed On August 31, 2007
Hi, The article is good. How to add attachments to the mail.
Reply | Email | Delete | Modify | 
Re: How to add attachments by kondala On March 11, 2008
MailObject.attachment=attachmnet file;
Reply | Email | Delete | Modify | 
adding reference:option not available in COM tab by Don On February 1, 2008
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
Reply | Email | Delete | Modify | 
Re: adding reference:option not available in COM tab by kondala On March 11, 2008

add the microsoft outlook 12.0 object library in place of 9.0..

this is updated library than 9.0..

Reply | Email | Delete | Modify | 
how to get tablenames to a combobox or dropdown control from sqldatabase using C# and asp.net? by a On March 20, 2008
replay me soon
Reply | Email | Delete | Modify | 
Remove from Outbox by Jon On September 13, 2008
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;
Reply | Email | Delete | Modify | 
Fantastic by Craig On October 24, 2008
Thanks so musch this is great
Reply | Email | Delete | Modify | 
outlook express by nir On March 1, 2009
Hello , I tried to use this code with outlook express with no success someone can help please ? Nir
Reply | Email | Delete | Modify | 
How to detect that outlook is installed in my computer by djelloul On May 13, 2009
hello

How to detect that outlook is installed in my computer;

best regard
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved