在.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.
1923 0
|
数据安全/隐私保护
通过.NET实现后台自动发送Email功能的代码示例
原文: 通过.NET实现后台自动发送Email功能的代码示例   通过.NET实现后台自动发送邮件功能的代码,可以将一些基础信息放到web.config文件中进行保存: Web.
945 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
38 0
|
30天前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
28 0
|
30天前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
95 5
|
3月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
25 0
|
3月前
|
前端开发
.net core mvc获取IP地址和IP所在地(其实是百度的)
.net core mvc获取IP地址和IP所在地(其实是百度的)
123 0
|
8月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
114 0