切换到宽版
Exchange 中文站
Exchange 2003
Exchange 2007
Exchange 2010
Exchange 2013
勋章中心
下拉
用户名
电子邮箱
用户名
密 码
记住登录
登录
找回密码
注册
快捷通道
关闭
您还没有登录,快捷通道只有在登录后才能使用。
立即登录
还没有帐号? 赶紧
注册一个
Exchange论坛
Exchange视频
Exchange技术论坛
>
Exchange 二次开发
>
Exchange Server 2007 二次开发 (三)
发帖
回复
返回列表
9865
阅读
1
回复
[分享]
Exchange Server 2007 二次开发 (三)
[复制链接]
上一主题
下一主题
tata
UID:10027
注册时间
2010-11-12
最后登录
2015-11-13
在线时间
866小时
发帖
363
搜Ta的帖子
精华
0
金币
186
访问TA的空间
加好友
用道具
初级工程师
关闭
个人中心可以申请新版勋章哦
立即申请
知道了
加关注
发消息
只看楼主
倒序阅读
0楼
发表于: 2011-02-19
若要代某人发送邮件,只需添加以下代码,但要注意,EWS代理账户需要对实际发送者有代理发送邮件权限,否则会收到发送失败的邮件。
message.From = new SingleRecipientType();
message.From.Item = new EmailAddressType();
message.From.Item.EmailAddress = "实际的发送者邮件地址";
创建带附件的邮件:
1
创建带附件的邮件
2
public
static
ItemIdType CreateMessage(ExchangeServiceBinding esb,
string
subject,
string
body,
string
toEmailAddress)
3
{
4
ItemIdType iiItemid
=
new
ItemIdType();
5
//
Create a CreateItem request object
6
CreateItemType request
=
new
CreateItemType();
7
8
//
Setup the request:
9
//
Indicate that we only want to send the message. No copy will be saved.
10
request.MessageDisposition
=
MessageDispositionType.SaveOnly;
11
request.MessageDispositionSpecified
=
true
;
12
13
//
Create a message object and set its properties
14
MessageType message
=
new
MessageType();
15
message.Subject
=
subject;
16
message.Body
=
new
BodyType();
17
message.Body.BodyType1
=
BodyTypeType.Text;
18
message.Body.Value
=
body;
19
20
message.ToRecipients
=
new
EmailAddressType[
1
];
21
message.ToRecipients[
0
]
=
new
EmailAddressType();
22
message.ToRecipients[
0
].EmailAddress
=
toEmailAddress;
23
message.ToRecipients[
0
].RoutingType
=
"
SMTP
"
;
24
25
//
Note: Same you can set CC and BCC Recipients
26
27
//
Construct the array of items to send
28
request.Items
=
new
NonEmptyArrayOfAllItemsType();
29
request.Items.Items
=
new
ItemType[
1
];
30
request.Items.Items[
0
]
=
message;
31
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
32
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
//
Call the CreateItem EWS method.
33
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
CreateItemResponseType createItemResponse
=
esb.CreateItem(request);
34
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
35
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
if
(createItemResponse.ResponseMessages.Items[
0
].ResponseClass
==
ResponseClassType.Error)
36
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
37
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Error Occured
"
);
38
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(createItemResponse.ResponseMessages.Items[
0
].MessageText);
39
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
40
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
else
41
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
42
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] ItemInfoResponseMessageType rmResponseMessage
=
createItemResponse.ResponseMessages.Items[
0
]
as
ItemInfoResponseMessageType;
43
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
44
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Item was created
"
);
45
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
46
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Item ID :
"
+
rmResponseMessage.Items.Items[
0
].ItemId.Id.ToString());
47
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
48
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
ChangeKey :
"
+
rmResponseMessage.Items.Items[
0
].ItemId.ChangeKey.ToString());
49
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
50
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
51
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] iiItemid.Id
=
rmResponseMessage.Items.Items[
0
].ItemId.Id.ToString();
52
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
53
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] iiItemid.ChangeKey
=
rmResponseMessage.Items.Items[
0
].ItemId.ChangeKey.ToString();
54
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
55
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
return
iiItemid;
56
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img] }
57
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
58
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
//
Here the itemID is returned by the CreateMessage Function
59
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
private
static
ItemIdType CreateAttachment(ExchangeServiceBinding ewsServiceBinding, String fnFileName, ItemIdType iiCreateItemid)
60
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
{
61
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
62
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] ItemIdType iiAttachmentItemid
=
new
ItemIdType();
63
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
64
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] FileStream fsFileStream
=
new
FileStream(fnFileName, System.IO.FileMode.Open,
65
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
66
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] System.IO.FileAccess.Read);
67
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
68
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
byte
[] bdBinaryData
=
new
byte
[fsFileStream.Length];
69
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
70
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
long
brBytesRead
=
fsFileStream.Read(bdBinaryData,
0
, (
int
)fsFileStream.Length);
71
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
72
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] fsFileStream.Close();
73
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
74
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] FileAttachmentType faFileAttach
=
new
FileAttachmentType();
75
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
76
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] faFileAttach.Content
=
bdBinaryData;
77
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
78
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] faFileAttach.Name
=
fnFileName;
79
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
80
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] CreateAttachmentType amAttachmentMessage
=
new
CreateAttachmentType();
81
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
82
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] amAttachmentMessage.Attachments
=
new
AttachmentType[
1
];
83
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
84
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] amAttachmentMessage.Attachments[
0
]
=
faFileAttach;
85
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
86
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] amAttachmentMessage.ParentItemId
=
iiCreateItemid;
87
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
88
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] CreateAttachmentResponseType caCreateAttachmentResponse
=
ewsServiceBinding.CreateAttachment
89
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
90
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] (amAttachmentMessage);
91
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
92
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
if
(caCreateAttachmentResponse.ResponseMessages.Items[
0
].ResponseClass
==
ResponseClassType.Error)
93
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
94
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
95
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Error Occured
"
);
96
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
97
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(caCreateAttachmentResponse.ResponseMessages.Items[
0
].MessageText);
98
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
99
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
100
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
101
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
else
102
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
103
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
104
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] AttachmentInfoResponseMessageType amAttachmentResponseMessage
=
105
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
106
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] caCreateAttachmentResponse.ResponseMessages.Items[
0
]
as
AttachmentInfoResponseMessageType;
107
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
108
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Attachment was created
"
);
109
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
110
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] Console.WriteLine(
"
Change Key :
"
+
amAttachmentResponseMessage.Attachments
111
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
112
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] [
0
].AttachmentId.RootItemChangeKey.ToString());
113
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
114
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] iiAttachmentItemid.Id
=
amAttachmentResponseMessage.Attachments[
0
].AttachmentId.RootItemId.ToString
115
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
116
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] ();
117
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
118
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] iiAttachmentItemid.ChangeKey
=
amAttachmentResponseMessage.Attachments
119
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
120
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] [
0
].AttachmentId.RootItemChangeKey.ToString();
121
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
122
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
123
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
124
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
return
iiAttachmentItemid;
125
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
126
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img] }
127
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
128
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
public
static
bool
SendMessage(ExchangeServiceBinding esb, ItemIdType p_itemId)
129
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
{
130
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
bool
blnResult
=
false
;
131
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] SendItemType siSendItem
=
new
SendItemType();
132
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSendItem.ItemIds
=
new
BaseItemIdType[
1
];
133
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSendItem.SavedItemFolderId
=
new
TargetFolderIdType();
134
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] DistinguishedFolderIdType siSentItemsFolder
=
new
DistinguishedFolderIdType();
135
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSentItemsFolder.Id
=
DistinguishedFolderIdNameType.sentitems;
136
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSendItem.SavedItemFolderId.Item
=
siSentItemsFolder;
137
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSendItem.SaveItemToFolder
=
true
;
138
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
139
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] siSendItem.ItemIds[
0
]
=
(BaseItemIdType)p_itemId;
140
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] SendItemResponseType srSendItemResponseMessage
=
esb.SendItem(siSendItem);
141
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
if
(srSendItemResponseMessage.ResponseMessages.Items[
0
].ResponseClass
==
142
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] ResponseClassType.Success)
143
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
144
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] blnResult
=
true
;
145
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
146
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
else
147
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
{
148
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] blnResult
=
false
;
149
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] }
150
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
return
blnResult;
151
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img] }
152
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
调用过程如下所示:
ItemIdType iiCreateItemid = CreateMessage(esb,"test mail with attachment","this is a mail for testing attachment","收件人邮箱地址");
iiCreateItemid = CreateAttachment(esb, @"c:\\test attachment.txt", iiCreateItemid);
if(SendMessage(esb,iiCreateItemid))
Console.WriteLine("Send mail with attachment success!");
else
Console.WriteLine("Something error!");
共
条评分
回复
举报
分享到
script
UID:11734
注册时间
2011-03-09
最后登录
2015-02-02
在线时间
37小时
发帖
89
搜Ta的帖子
精华
0
金币
170
访问TA的空间
加好友
用道具
技术人员
加关注
发消息
只看该作者
1楼
发表于: 2011-03-09
Exchange开发的东西可真是多。
共
条评分
回复
举报
发帖
回复
返回列表
http://bbs.exchangecn.com
访问内容超出本站范围,不能确定是否安全
继续访问
取消访问
快速回复
限60 字节
您目前还是游客,请
登录
或
注册
进入高级模式
文字颜色
发 布
回复后跳转到最后一页
上一个
下一个
关闭
补充发布信息
验证码:
验证问题:
21 + 22 = ?
发 布
隐藏
快速跳转
Microsoft 企业云服务
Office 365 定制版用户讨论区
Exchange 技术讨论
Exchange Server 2016
Exchange Server 2013
Exchange Server 2010
Exchange Server 2007
Exchange Server 2003
Exchange 相关技术
Exchange 事件日志
Exchange 二次开发
Microsoft Outlook
Exchange 资源下载
Exchange 2003 资源下载
Exchange 2007 资源下载
Exchange 2010 资源下载
Exchange 招聘求职
Exchange 招聘 | 求职
Exchange 社区站务
站内站务
关闭
关闭
选中
1
篇
全选