博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在.NET中通过Outlook发送Email
阅读量:6232 次
发布时间:2019-06-21

本文共 3639 字,大约阅读时间需要 12 分钟。

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: 

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,如需转载请自行联系原作者
你可能感兴趣的文章
MongodDB学习笔记(二)(复制)
查看>>
oracle在线迁移同步数据,数据库报错
查看>>
Java中1.0 / 0.0 会输出什么?
查看>>
linux性能剖析工具
查看>>
DP ZOJ 3872 Beauty of Array
查看>>
jQuery Ajax实例 ($.ajax_$.post_$.get)
查看>>
垃圾桶丁
查看>>
HDU 4757 可持久化trie树
查看>>
spring-boot入门
查看>>
USB HID 分析
查看>>
驱动属性
查看>>
IOS 学习笔记(6) 控件 文本域(UITextField)的使用方法
查看>>
第一次写JQuery插件--用于显示子菜单
查看>>
Java的几种对象(PO,VO,DAO,BO,POJO)解释
查看>>
Quartz总结(一):Quartz集成Spring的2个方法
查看>>
读取文件,输出单词
查看>>
zabbix常用的python类api
查看>>
Oracle第三章——SQL语言
查看>>
《代码大全》阅读笔记-19-一般控制问题
查看>>
VB编程技巧推荐
查看>>