积极答复者
用web service做exchange server 的二次开发,想在邮件中要添加多份附件,这个问题怎么解决

问题
-
大家好,问个问题,我现在用web service做exchange server 的二次开发,关于发邮件,但是邮件中要添加多份附件,现在只能添加一个附件,想实现添加多份附件,各位给我看看,代码已测试完毕,可以正常运行。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Security;
using System.Net;
using System.Net.Mail;
using System.Web.Services;
using WindowsApplication3.com.cgwic.casc;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using System.IO;
using System.Xml;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//验证证书所用的一个函数,解决基础链接关闭的问题
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
}private void button1_Click(object sender, EventArgs e)
{
//设置ExchangeServiceBinding代理类。
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential("sqlsetup", "12345678", "cgwic.casc"); //用户名 密码 域名
esb.Url = "https://bmx00320.cgwic.casc/EWS/exchange.asmx";ItemIdType iicreareItemid = CreateMessage(esb,"主题","内容","portaladmin@cgwic.casc");
iicreareItemid = CreateAttachment(esb, @"c:\1.doc", iicreareItemid);
if (SendMessage(esb, iicreareItemid))
{
Console.WriteLine("Send mail with attachment success");
}
else
{
Console.WriteLine("Something error");}
}
//发送邮件
private void Form1_Load(object sender, EventArgs e)
{
}
public static ItemIdType CreateMessage(ExchangeServiceBinding esb, string subject, string body, string toEmailAddress)
{
ItemIdType iiItemid = new ItemIdType();
// Create a CreateItem request object
CreateItemType request = new CreateItemType();
// Setup the request:
// Indicate that we only want to send the message. No copy will be saved.
request.MessageDisposition = MessageDispositionType.SaveOnly;
request.MessageDispositionSpecified = true;
// Create a message object and set its properties
MessageType message = new MessageType();
message.Subject = subject;
message.Body = new WindowsApplication3.com.cgwic.casc.BodyType();
message.Body.BodyType1 = BodyTypeType.Text;
message.Body.Value = body;
//route problem
message.From = new SingleRecipientType();
message.From.Item = new EmailAddressType();
message.From.Item.EmailAddress = "sharepoint@cgwic.casc";
message.ToRecipients = new EmailAddressType[1];
message.ToRecipients[0] = new EmailAddressType();
message.ToRecipients[0].EmailAddress = toEmailAddress;
message.ToRecipients[0].RoutingType = "SMTP";
//Note: Same you can set CC and BCC Recipients
// Construct the array of items to send
request.Items = new NonEmptyArrayOfAllItemsType();
request.Items.Items = new ItemType[1];
request.Items.Items[0] = message;
// Call the CreateItem EWS method.try
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);CreateItemResponseType createItemResponse = esb.CreateItem(request);
if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
Console.WriteLine("Error Occured");
Console.WriteLine(createItemResponse.ResponseMessages.Items[0].MessageText);
}
else
{
ItemInfoResponseMessageType rmResponseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;Console.WriteLine("Item was created");
Console.WriteLine("Item ID : " + rmResponseMessage.Items.Items[0].ItemId.Id.ToString());
Console.WriteLine("ChangeKey : " + rmResponseMessage.Items.Items[0].ItemId.ChangeKey.ToString());
iiItemid.Id = rmResponseMessage.Items.Items[0].ItemId.Id.ToString();iiItemid.ChangeKey = rmResponseMessage.Items.Items[0].ItemId.ChangeKey.ToString();
}}
catch (Exception ex)
{
throw new Exception("Warning: " + ex.Message);
}
return iiItemid;
}
//创建附件
private static ItemIdType CreateAttachment(ExchangeServiceBinding ewsServiceBinding, String fnFileName, ItemIdType iiCreateItemid)
{
ItemIdType iiAttachmentItemid = new ItemIdType();
FileStream fsFileStream = new FileStream(fnFileName, System.IO.FileMode.Open,
System.IO.FileAccess.Read);
byte[] bdBinaryData = new byte[fsFileStream.Length];
long brBytesRead = fsFileStream.Read(bdBinaryData, 0, (int)fsFileStream.Length);
fsFileStream.Close();
FileAttachmentType faFileAttach = new FileAttachmentType();
faFileAttach.Content = bdBinaryData;
faFileAttach.Name = fnFileName;CreateAttachmentType amAttachmentMessage = new CreateAttachmentType();
amAttachmentMessage.Attachments = new AttachmentType[1];
amAttachmentMessage.Attachments[0] = faFileAttach;
amAttachmentMessage.ParentItemId = iiCreateItemid;CreateAttachmentResponseType caCreateAttachmentResponse = ewsServiceBinding.CreateAttachment
(amAttachmentMessage);
if (caCreateAttachmentResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
Console.WriteLine("Error Occured");
Console.WriteLine(caCreateAttachmentResponse.ResponseMessages.Items[0].MessageText);
}else
{AttachmentInfoResponseMessageType amAttachmentResponseMessage =
caCreateAttachmentResponse.ResponseMessages.Items[0] as AttachmentInfoResponseMessageType;
Console.WriteLine("Attachment was created");
Console.WriteLine("Change Key : " + amAttachmentResponseMessage.Attachments
[0].AttachmentId.RootItemChangeKey.ToString());
iiAttachmentItemid.Id = amAttachmentResponseMessage.Attachments[0].AttachmentId.RootItemId.ToString
();
iiAttachmentItemid.ChangeKey = amAttachmentResponseMessage.Attachments
[0].AttachmentId.RootItemChangeKey.ToString();
}
return iiAttachmentItemid;
}
public static bool SendMessage(ExchangeServiceBinding esb, ItemIdType p_itemId)
{
bool blnResult = false;
SendItemType siSendItem = new SendItemType();
siSendItem.ItemIds = new BaseItemIdType[1];
siSendItem.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType siSentItemsFolder = new DistinguishedFolderIdType();
siSentItemsFolder.Id = DistinguishedFolderIdNameType.sentitems;
siSendItem.SavedItemFolderId.Item = siSentItemsFolder;
siSendItem.SaveItemToFolder = true;siSendItem.ItemIds[0] = (BaseItemIdType)p_itemId;
SendItemResponseType srSendItemResponseMessage = esb.SendItem(siSendItem);
if (srSendItemResponseMessage.ResponseMessages.Items[0].ResponseClass ==
ResponseClassType.Success)
{
blnResult = true;
}
else
{
blnResult = false;
}
return blnResult;
}
}
}
- 已编辑 virus black 2012年3月9日 3:07