gpt4 book ai didi

exchange-server - Exchange Web服务-发送带有附件的电子邮件

转载 作者:行者123 更新时间:2023-12-04 03:41:01 29 4
gpt4 key购买 nike

我刚开始使用EWS(Exchange Web服务),并且正在寻找一个简单的示例,该示例演示如何发送带有附件的电子邮件。我搜索了一个示例,但找不到简单明了的示例。我找到了有关如何发送电子邮件但不发送带有附件的电子邮件的示例。

是否有人链接到他们会推荐的示例?在此处发布示例同样适用!

最佳答案

好吧,我最终明白了这一点。这是一种创建电子邮件,将其存储为草稿,添加附件然后发送电子邮件的方法。希望这能帮助那些找不到像我这样的好榜样的人。

在我的示例中,我只会发送excel文件,这就是为什么按原样设置内容类型的原因。显然,可以将其更改为支持任何类型的文件附件。

供您引用,变量 esb 是ExchangeServiceBinding类型的类级别变量。

编辑

我还应注意,在此示例中,我没有检查交换操作的响应类型是否成功。如果您想知道您对EWS的调用是否确实有效,则应该进行检查。

public void SendEmail(string from, string to, string subject, string body, byte[] attachmentAsBytes, string attachmentName)
{
//Create an email message and initialize it with the from address, to address, subject and the body of the email.
MessageType email = new MessageType();

email.ToRecipients = new EmailAddressType[1];
email.ToRecipients[0] = new EmailAddressType();
email.ToRecipients[0].EmailAddress = to;

email.From = new SingleRecipientType();
email.From.Item = new EmailAddressType();
email.From.Item.EmailAddress = from;

email.Subject = subject;

email.Body = new BodyType();
email.Body.BodyType1 = BodyTypeType.Text;
email.Body.Value = body;

//Save the created email to the drafts folder so that we can attach a file to it.
CreateItemType emailToSave = new CreateItemType();
emailToSave.Items = new NonEmptyArrayOfAllItemsType();
emailToSave.Items.Items = new ItemType[1];
emailToSave.Items.Items[0] = email;
emailToSave.MessageDisposition = MessageDispositionType.SaveOnly;
emailToSave.MessageDispositionSpecified = true;

CreateItemResponseType response = esb.CreateItem(emailToSave);
ResponseMessageType[] rmta = response.ResponseMessages.Items;
ItemInfoResponseMessageType emailResponseMessage = (ItemInfoResponseMessageType)rmta[0];

//Create the file attachment.
FileAttachmentType fileAttachment = new FileAttachmentType();
fileAttachment.Content = attachmentAsBytes;
fileAttachment.Name = attachmentName;
fileAttachment.ContentType = "application/ms-excel";

CreateAttachmentType attachmentRequest = new CreateAttachmentType();
attachmentRequest.Attachments = new AttachmentType[1];
attachmentRequest.Attachments[0] = fileAttachment;
attachmentRequest.ParentItemId = emailResponseMessage.Items.Items[0].ItemId;

//Attach the file to the message.
CreateAttachmentResponseType attachmentResponse = (CreateAttachmentResponseType)esb.CreateAttachment(attachmentRequest);
AttachmentInfoResponseMessageType attachmentResponseMessage = (AttachmentInfoResponseMessageType)attachmentResponse.ResponseMessages.Items[0];

//Create a new item id type using the change key and item id of the email message so that we know what email to send.
ItemIdType attachmentItemId = new ItemIdType();
attachmentItemId.ChangeKey = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemChangeKey;
attachmentItemId.Id = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemId;

//Send the email.
SendItemType si = new SendItemType();
si.ItemIds = new BaseItemIdType[1];
si.SavedItemFolderId = new TargetFolderIdType();
si.ItemIds[0] = attachmentItemId;
DistinguishedFolderIdType siSentItemsFolder = new DistinguishedFolderIdType();
siSentItemsFolder.Id = DistinguishedFolderIdNameType.sentitems;
si.SavedItemFolderId.Item = siSentItemsFolder;
si.SaveItemToFolder = true;

SendItemResponseType siSendItemResponse = esb.SendItem(si);
}

关于exchange-server - Exchange Web服务-发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3823256/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com