---
title: "ppc 程序，实现邮件收发及短信收发"
author: "addday"
date: "2008-08-21T23:49:08Z"
tags: []
slug: "ppc"
draft: false
lastmod: "2008-08-21T23:49:08Z"
id: 231379
url: "https://cardone.iteye.com/blog/231379"
---

这篇文章主要讲述：Microsoft.WindowsMobile.PocketOutlook命名空间下的五个类：  
1.OutlookSession  
2.AppointmentCollection, Appointment  
3.ContactCollection, Contact  
4.SmsMessage  
5.EmailMessage

该命名空间主要针对PocketOutlook中的所提供的Appointment(约会), Task(计划), Contact(联系人), 短信, 邮件的操作。  
注：在WM5.0系统中已提供该命名空间，即CF2.0下也提供支持。

下面就开始逐个介绍。首先运行下程序效果。  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_00.JPG)

OutlookSession是取得PocketOutlook中的所有信息的一个对象, 该对象提供访问PocketOutlook对象模型的一个入口。通过该对象, 可以操作PocketOutlook所提供的所有操作。

当点击Appointments时, 取得已存在的Appoint, 并添加一个新的Appoint.  
通过OutlookSession.Appointments.Item得到AppointmentCollection(所有集合). 该集合内存放所有Appointment.  
这些Appointment按照时间的顺序排列. 当然, 你也可以指定按什么排序. 使用Sort方法, 并指定排序字段即可.  
AppointmentCollection的AddNew方法：在集合对象中创建一个新Appointment, 并将该对象返回, 类似于DataRow.  
返回新对象后, 对该对象的属性进行赋值后, 通过该对象的Update方法, 就能自动更新集合内信息.

代码如下：

 1![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            OutlookSession session = new OutlookSession();  
 2![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)  
 3![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            foreach (Appointment app in session.Appointments.Items)  
 4![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
 5![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("ItemId: " + app.ItemId);  
 6![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Subject: " + app.Subject);  
 7![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("AllDayEvent: " + app.AllDayEvent.ToString());  
 8![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Body: " + app.Body);  
 9![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("BusyStatus: " + app.BusyStatus.ToString());  
10![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Location: " + app.Location);  
11![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("");  
12![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)            }  
13![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)  
14![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            Appointment appAdd = session.Appointments.Items.AddNew();  
15![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.Subject = "Add Now";  
16![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.Location = "Shanghai";  
17![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.Start = System.DateTime.Now.AddHours(1);  
18![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.End = System.DateTime.Now.AddHours(2);  
19![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.BusyStatus = BusyStatus.Busy;  
20![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)            appAdd.Update();

效果如下：  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_01.JPG)  
Appoint没有添加时的效果.

Appointment与AppointmentCollection也有很多事件, 这里举一个AppointmentCollection的ListChanged事件.  
当AppointmentCollection集合中的任一Appoint发生变化(新增, 修改, 删除)时, 该事件即会被触发.  
代码如下：

 1![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)        void Items\_ListChanged(object sender, ListChangedEventArgs e)  
 2![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
 3![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            //this.listBox1.Items.Add("Type Name: " + sender.GetType().FullName);  
 4![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            AppointmentCollection appCol = (AppointmentCollection)sender;  
 5![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            if (e.ListChangedType == ListChangedType.ItemChanged)  
 6![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
 7![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("ItemId: " + appCol\[e.NewIndex].ItemId);  
 8![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Subject: " + appCol\[e.NewIndex].Subject);  
 9![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("AllDayEvent: " + appCol\[e.NewIndex].AllDayEvent.ToString());  
10![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Body: " + appCol\[e.NewIndex].Body);  
11![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("BusyStatus: " + appCol\[e.NewIndex].BusyStatus.ToString());  
12![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Location: " + appCol\[e.NewIndex].Location);  
13![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("");  
14![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
15![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)        }

效果如下：  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_02.JPG)  
Subject为Add Now为添加的Appoint.

Contact对象, 通过OutlookSession的Contacts.Items得到ContactCollection.  
访问方法与Appoint一致.在此, 只演示代码.

 1![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)        private void menuItem7\_Click(object sender, EventArgs e)  
 2![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
 3![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            OutlookSession session = new OutlookSession();  
 4![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)  
 5![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            foreach (Contact contact in session.Contacts.Items)  
 6![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedSubBlock.gif)            ![](http://www.cnblogs.com/Images/dot.gif){  
 7![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("File As: " + contact.FileAs);  
 8![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("Email1Address: " + contact.Email1Address);  
 9![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                this.listBox1.Items.Add("");  
10![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
11![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)  
12![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            Contact contactAdd = session.Contacts.Items.AddNew();  
13![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            contactAdd.FileAs = "Test Man";  
14![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            contactAdd.FirstName = "Test";  
15![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            contactAdd.LastName = "Man";  
16![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            contactAdd.Email1Address = "Test@abs.com";  
17![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            contactAdd.Update();  
18![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)        }

效果如下：  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_03.JPG)

Task, TaskCollection：看过上述的Appoint与Contact后, 相信大家也一定能够使用Task的. 使用方式完全一致.  
Appoint, Contact, Task. 在OutlookSession中唯一标识符为ItemId. 可以在集合中通过ItemId来取得你需要的某一个内容.  
并且它们可以创建除ItemId以外, 内容完全一致的对象, 这是完全存在的.  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_04.JPG)  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_05.JPG)

SmsMessage：短信对象.可以在构造函数时传入要发送的号码及内容, 也可以在创建完对象后对其属性进行修改.  
该对象提供Send方法, 用于发送短信. 当然, 也可以通过OutlookSession的SmsAccount.Send方法发送, 这都是一样的.  
注：短信不支持彩信功能, 只能发送文字信息. 当程序自己发送消息完后, 发送的消息并不会出现在短信的已发送栏里.这和邮件发送不一致的地方, 需了解. 另外, 微软的IDE(VS2008)中存在一个BUG, 就是访问SmsMessage对象的Body时, 显示提示居然说是只读的, 但其实是读写都可以的.  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_09.jpg)  
应该改为Gets Or Sets the SMS message's text body.  
代码如下：

 1![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)        private void menuItem4\_Click(object sender, EventArgs e)  
 2![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
 3![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            SmsMessage smsMessage = new SmsMessage(strContract, strMessage);  
 4![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            smsMessage.RequestDeliveryReport = false;  
 5![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            //smsMessage.Send();  
 6![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)  
 7![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            OutlookSession session = new OutlookSession();  
 8![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            this.listBox1.Items.Add("SmsAccount Name: " + session.SmsAccount.Name);  
 9![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            //this.listBox1.Items.Add("ItemID: " + smsMessage.ItemId.ToString());  
10![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            session.SmsAccount.Send(smsMessage);  
11![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)  
12![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            SmsMessage sms = new SmsMessage();  
13![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            sms.Body = "Test";  
14![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            sms.To.Add(new Recipient(strContract));  
15![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            session.SmsAccount.Send(sms);  
16![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)        }

通过Call Emulator工具,取得短信.  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_06.JPG)

EmailMessage：发送邮件对象.  
如果没有设置好

EmailAccounts, 是无法正常发送Email的.  
代码如下：

 1![](http://addday.iteye.com/Images/OutliningIndicators/None.gif)        private void menuItem5\_Click(object sender, EventArgs e)  
 2![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://addday.iteye.com/Images/OutliningIndicators/ContractedBlock.gif)        ![](http://www.cnblogs.com/Images/dot.gif){  
 3![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            EmailMessage mail = new EmailMessage();  
 4![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            mail.To.Add(new Recipient("appleseeker@gmail.com"));  
 5![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            mail.Importance = Importance.High;  
 6![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            mail.Subject = "It is test mail";  
 7![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            mail.BodyText = "It is test mail";  
 8![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            mail.Attachments.Add(new Attachment(@"\\flower.jpg"));  
 9![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)  
10![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            OutlookSession session = new OutlookSession();  
11![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)            if (session.EmailAccounts.Count &gt; 0)  
12![](http://addday.iteye.com/Images/OutliningIndicators/InBlock.gif)                mail.Send(session.EmailAccounts\[1]);  
13![](http://addday.iteye.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)        }

OutlookSession.EmailAccount中, 我访问集合中第2个元素的理由：  
因为当前是模拟器开发,所有该EmailAccounts中第1个元素是模拟器.只有后面的才是在模拟器上说设置的Mail帐户.  
效果如下：  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_07.JPG)  
没有发送邮件时.  
![](http://images.cnblogs.com/cnblogs_com/appleseeker/appleseeker_2008_03_24_08.JPG)  
EmailMessage对象调用Send后,邮箱的邮件情况.

本文就PocketOutlook中的一些基本操作内容讲述完毕,改天会对如何操作PocketOutlook.MessageInterception命名空间作一下讲述,该空间提供消息拦截,起到过滤效果.

代码下载：[SmartDeviceOutlookDemo\_2008\_03\_24.rar](http://files.cnblogs.com/appleseeker/SmartDeviceOutlookDemo_2008_03_24.rar)