Pages

Advertisement

Thursday, July 12, 2007

Sending Emails Through Outlook using C# and VB.NET

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.

10 comments:

chetan said...

Hi,
I am sending email as per code. I am able to send mail to Outlook's Outbox. But the email gets stuck into Outbox. It doesn't get deleted or moved to Sent Items.

Do you know what is the reason.

S3 Technology Softwares said...

actually chetan u have to only change the default folder(outbox) for saving sent items to ur desired (sent mails) .


u have to only change the this code :
oOutboxFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

to >
oOutboxFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);


u have to change only the olFolderOutbox to olFolderSentMail

.. In the same way u can also add u r desired folder name and can save a copy of all the messages u had sent through ur application ...


.. I m glad to here queries from u


-----------------------------

Alex said...

Usually at work with mails I use next software-outlook 2003 .pst viewer,because it has many resources and application is free as far as I know,it will help you to solve mail recovery problems,program was developed as a good addition for Microsoft Outlook, our customers usually recover their mailboxes, stored on Microsoft Outlook,easily guide you through all steps of recovery process, it does not matter, which level of computer skills do you have, Outlook 2003 .pst file viewer is easy for everyone, from beginners to professionals. We believe, that documentation is not needed.

Anonymous said...

Today I saw that my outlook doesn't work,because my password was lost,and accidentally I found in inet-outlook password open.And fortunatley tool helped me fast.It is free as far as I know,besides that it will help to retrieve current user password Outlook and restore access to your email account.

Alexis said...

Once I worked with folders into MS Outlook and something happened.After that all my mails were deleted.I was frightened to death.But fortunately myself helped-corrupted pst file outlook 2007.This tool I found in twitter.Tool is free as far as I know.Moreover utility can help to extract data from corrupted files in *.ost and *.pst format and avoid losing of critical contacts, calendars and messages.

Jess said...

It can't get any better than this.

Anonymous said...

Hello !.
You may , probably curious to know how one can reach 2000 per day of income .
There is no initial capital needed You may begin to receive yields with as small sum of money as 20-100 dollars.

AimTrust is what you thought of all the time
The firm represents an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

It is based in Panama with affiliates everywhere: In USA, Canada, Cyprus.
Do you want to become a happy investor?
That`s your choice That`s what you really need!

I`m happy and lucky, I started to take up income with the help of this company,
and I invite you to do the same. If it gets down to choose a correct partner utilizes your savings in a right way - that`s the AimTrust!.
I take now up to 2G every day, and what I started with was a funny sum of 500 bucks!
It`s easy to join , just click this link http://luhisyxe.fcpages.com/lojorofu.html
and go! Let`s take our chance together to get rid of nastiness of the life

Anonymous said...

Hi!
You may probably be very interested to know how one can manage to receive high yields on investments.
There is no initial capital needed.
You may begin to get income with a money that usually goes
on daily food, that's 20-100 dollars.
I have been participating in one project for several years,
and I'll be glad to share my secrets at my blog.

Please visit my pages and send me private message to get the info.

P.S. I make 1000-2000 per day now.

http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Anonymous said...

Good day, sun shines!
There have been times of hardship when I didn't know about opportunities of getting high yields on investments. I was a dump and downright pessimistic person.
I have never imagined that there weren't any need in large starting capital.
Nowadays, I feel good, I begin to get real income.
It's all about how to select a correct companion who utilizes your money in a right way - that is incorporate it in real deals, and shares the income with me.

You may ask, if there are such firms? I'm obliged to answer the truth, YES, there are. Please be informed of one of them:
http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Alex said...

Today I woke up for my computer had blown up on unknown cause. I lost all my data. But to my good fortune almost whole data I could restore except outlook accounts data and passwords. In this case my friend helped me very much - how to recover outlook password. This tool, which he had found on an unknown blog resolved my trouble without even trying.