切换到宽版
  • 9779阅读
  • 1回复

[分享]Exchange Server 2007 二次开发 (三) [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2011-02-19

若要代某人发送邮件,只需添加以下代码,但要注意,EWS代理账户需要对实际发送者有代理发送邮件权限,否则会收到发送失败的邮件。
message.From = new SingleRecipientType();
message.From.Item = new EmailAddressType();
message.From.Item.EmailAddress = "实际的发送者邮件地址";
创建带附件的邮件:
  1创建带附件的邮件
  2public 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!");
分享到
只看该作者 1楼  发表于: 2011-03-09
Exchange开发的东西可真是多。
快速回复
限60 字节
 
上一个 下一个