交换Web服务 - 发送带附件的电子邮件

问题描述:

我刚刚使用EWS(Exchange Web服务),我正在寻找一个演示如何发送带附件的电子邮件的简单示例。我搜索了一个例子,我找不到任何简单明了的方法。我找到了关于如何发送电子邮件但不发送带附件的电子邮件的示例。交换Web服务 - 发送带附件的电子邮件

有没有人有他们会推荐的例子的链接?在这里发布一个例子也会起作用!

+0

您是使用托管API还是仅使用EWS?这些位有所不同,但仍然很容易。按照您发现的创建电子邮件实例的教程,然后在托管API中,您只需执行以下操作:email.Attachments.Add(fileName); – Chris 2010-09-30 09:39:24

+0

我只使用EWS。我找到了一个创建FileAttachmentType的例子,然后从该文件附件创建一个CreateAttachmentType。然后它使用CreateAttachmentType调用ews.CreateAttachment。那是我应该做的吗?正如你的回答所暗示的那样,我希望它会更直观一些,但我发现将一个文件附加到电子邮件中比我预期的要更“模糊”。 – Anthony 2010-09-30 14:23:42

嗯,我终于明白了这一点。以下是一种方法,它将创建邮件消息,将其存储为草稿,添加附件并发送电子邮件。希望这可以帮助那些无法找到像我这样的好例子的人。

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

你能告诉我们你的进口吗? – 2014-05-30 13:29:15

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

您需要将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呼叫可以采取许多秒 - 如果你知道的网址,然后你就可以避免调用AutodiscoverUrl和直接设置它。 (您可以通过调用AutodiscoverUrl然后打印service.Url来恢复一次。)

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