【问题标题】:Exchange Web Services - Send email with attachmentExchange Web 服务 - 发送带附件的电子邮件
【发布时间】:2011-04-18 21:43:33
【问题描述】:

我是使用EWS(Exchange Web 服务)的新手,我正在寻找一个简单的示例来演示如何发送带有附件的电子邮件。我搜索了一个示例,但找不到任何简单明了的示例。我找到了有关如何发送电子邮件但不发送带附件的电子邮件的示例。

有人有他们推荐的示例的链接吗?在这里发布一个例子也可以!

【问题讨论】:

  • 您使用的是托管 API 还是仅使用 EWS?这些位略有不同,但仍然很容易。按照您找到的教程创建一个电子邮件实例,然后在托管 API 中您需要做的就是:email.Attachments.Add(fileName);
  • 我只使用 EWS。我找到了一个创建 FileAttachmentType 然后从该文件附件创建 CreateAttachmentType 的示例。然后它使用 CreateAttachmentType 调用 ews.CreateAttachment。那是我应该做的吗?正如您的回答所暗示的那样,我希望它会更直观一些,但我发现将文件附加到电子邮件中比我预期的更“模糊”。

标签: exchange-server exchangewebservices


【解决方案1】:

好吧,我最终想通了。这是一种创建邮件消息、将其存储为草稿、添加附件然后发送电子邮件的方法。希望这可以帮助那些无法找到像我这样的好例子的人。

在我的示例中,我只会发送 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);
        }

【讨论】:

    【解决方案2】:

    我知道这个问题很老了,但是我在谷歌搜索后登陆了这里。这是使用语句的更新简化工作答案。

    您需要将 nuget 包 Microsoft.Exchange.WebServices 添加到您的项目中(当前版本为 2.2.0)。

    using Microsoft.Exchange.WebServices.Data;
    
    namespace Exchange
    {
        public static class Emailer
        {
            public static void SendEmail(string from, string to, string subject, string body, byte[] attachmentBytes, string attachmentName)
            {
                var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.AutodiscoverUrl(from);
                var message = new EmailMessage(service)
                {
                    Subject = subject,
                    Body = body,
                };
                message.ToRecipients.Add(to);
                message.Attachments.AddFileAttachment(attachmentName, attachmentBytes);
                message.SendAndSaveCopy();
            }
        }
    }
    

    对 service.AutodiscoverUrl 的调用可能需要几秒钟 - 如果您知道 url,那么您可以避免调用 AutodiscoverUrl 并直接设置它。 (您可以通过调用 AutodiscoverUrl 然后打印 service.Url 来恢复它。)

    // service.AutodiscoverUrl(from); // This can be slow
    service.Url = new System.Uri("https://outlook.domain.com/ews/exchange.asmx");
    

    【讨论】:

    • 我遇到了一个系统,该系统使用另一篇文章中提到的保存到草稿方法,并且存在一个问题,即发送电子邮件地址被交换更改并且随后没有发送邮件,所以我会现在使用@davestevens 方法不仅因为它更简单,而且因为它更不容易出错。 AutoDiscoverUrl 方法的重要提示。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2011-12-03
    • 2014-06-13
    • 2017-07-28
    • 2011-03-30
    • 2015-08-28
    • 2014-07-19
    相关资源
    最近更新 更多