Sending Emails Through Outlook using C# and VB.NET

本文介绍如何使用C#和VB.NET编程语言向Microsoft Outlook的待发邮件文件夹添加电子邮件。示例中包含三个类:Form1.cs用于展示调用功能的简易Windows窗体;CSharp.OutlookMail.cs与VBNET.OutlookMail.vb分别用C#与VB.NET实现向Outlook待发邮件文件夹添加邮件的功能。

 

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.



http://www.c-sharpcorner.com//Internet/SendingEmailsThroughOutlookCB.asp

转载于:https://www.cnblogs.com/bluewelkin/archive/2008/08/21/1273404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值