在.NET中通过Outlook发送Email

简介:

Sending Emails Through Outlook using C# and VB.NET
Author Date Of Submission User Level
Caspar Boekhoudt 11/09/2001 Intermediate
.web>
.web>

 

Source Code: SendingEmailsThroughOutlookCB.zip

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.

.web>

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


本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/11/14/63702.html,如需转载请自行联系原作者
相关文章
|
.NET 数据安全/隐私保护 开发框架
ASP.NET 3.5 中实现发送email电子邮件
来源:红黑联盟 方法1:cs代码 using System.Net.Mail; using System.Net; 1 string mailServerName = "smtp.
2132 0
|
数据安全/隐私保护
通过.NET实现后台自动发送Email功能的代码示例
原文: 通过.NET实现后台自动发送Email功能的代码示例   通过.NET实现后台自动发送邮件功能的代码,可以将一些基础信息放到web.config文件中进行保存: Web.
1166 0
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
1119 0
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
1171 5
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
658 7
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
492 0
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
557 0
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
700 0
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
967 0

热门文章

最新文章