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

标签 exchange-server exchangewebservices

我刚开始使用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/

相关文章:

javascript - 在 Outlook 加载项中删除附件文件时出错

c# - 在 outlook 中显示 Outlook 约会扩展属性

azure-active-directory - Exchange 2016 将 TlsCertificateName 分配给接收连接器时出错

c# - 将已发送的 MailMessage 获取到 "Sent Folder"

python - 避免以明文形式存储密码以通过 Python 进行 IMAP 访问

java - MS 交换日历和 Java 应用程序之间的双向同步

c# - Exchange Web 服务 - 从服务收到的响应不包含有效的 XML

vb.net - Powershell 脚本可以在 Powershell 中运行,但不能在 VB.Net 中运行

c# - TimeZoneInfo - 为什么我似乎不能创建它的实例?

html - EWS Managed API、Appointment MessageBody 以及 HTML/CSS 的销毁