我刚开始使用 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);
}
我知道这个问题很老了,但我在搜索谷歌后来到这里。这是使用 using 语句的更新的简化工作答案。
您需要将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");
当尝试使用 SOAP Web 服务通过 EWS 发送带有文件附件的电子邮件时,我遇到了同样的问题。终于找到解决方案了。
1)创建消息并获取消息ID。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Body>
<CreateItem MessageDisposition="SaveOnly"
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<SavedItemFolderId>
<t:DistinguishedFolderId Id="drafts" />
</SavedItemFolderId>
<Items>
<t:Message>
<t:ItemClass>IPM.Note</t:ItemClass>
<t:Subject>test 2</t:Subject>
<t:Body BodyType="HTML">this is
<b>bold</b></t:Body>
<t:ToRecipients>
<t:Mailbox>
<t:EmailAddress>[email protected]</t:EmailAddress>
</t:Mailbox>
</t:ToRecipients>
<t:IsRead>false</t:IsRead>
</t:Message>
</Items>
</CreateItem>
</soap:Body>
</soap:Envelope>
得到回应:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1497" MinorBuildNumber="46" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:CreateItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Items>
<t:Message>
<t:ItemId Id="AAAYAE5pa29sYXkuR3VyeWFub3ZAYXphbC5hegBGAAAAAAAM4X07f1m5TruMA1KYuHzKBwCYiVmSeaoqQ5DPKXS6XYPUAAAAv1MsAACiYL/sCHf3TKGIeSfzqioDAAbhmG/mAAA=" ChangeKey="CQAAABYAAACiYL/sCHf3TKGIeSfzqioDAAbiJMe3"/>
</t:Message>
</m:Items>
</m:CreateItemResponseMessage>
</m:ResponseMessages>
</m:CreateItemResponse>
</s:Body>
</s:Envelope>
2)创建文件附件。在此之前,请将文件转换为二进制代码并使用 Base64 方法对其进行加密。然后将文件的哈希值放入标签“Content”,如下例所示:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Body>
<CreateAttachment xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<ParentItemId Id="AAAYAE5pa29sYXkuR3VyeWFub3ZAYXphbC5hegBGAAAAAAAM4X07f1m5TruMA1KYuHzKBwCYiVmSeaoqQ5DPKXS6XYPUAAAAv1MsAACiYL/sCHf3TKGIeSfzqioDAAbhmG/mAAA=" />
<Attachments>
<t:FileAttachment>
<t:Name>kassa.ini</t:Name>
<t:Content>
W0dlbmVyYWxdDQpzaG9wbm89OTkNCg0KDQpkYXRhc2hvcD1jOlxkcmVhbVxkYXRhc2hvcCAyLjAuYWNjZGINCmRhdGFISz1jOlxkcmVhbVxkYXRhSEsubWRiDQoNCnJlbSB2b29ycmFhZCB3YWFyc2NodXdpbmcNCnZvb3JyYWFkd2FhcnNjaHV3aW5nPS0xDQoNCnJlbSBwaW5uZW4NCkhlZWZ0UE9JUD0wDQpYVElMTkVYVD0tMQ0KDQpyZW0gbW9uc3RlciBrYXNzYQ0KSXNtb25zdGVyPTANCg0KcmVtIGF1dG9pbnZvZXIga25vcA0KYXV0b2ludm9lcj0tMQ0KDQpyZW0gdG91Y2hzY3JlZW4NCnRvdWNoc2NyZWVuPTANCg0KDQpbcGluXQ0KZGVmYXVsdHBpbj0wNQ0KcGluMDU9UkVOVDAxDQpwaW4wMz1HU00wMQ0KDQpbZXJyb3JdDQo=</t:Content>
</t:FileAttachment>
</Attachments>
</CreateAttachment>
</soap:Body>
</soap:Envelope>
得到回复:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1497" MinorBuildNumber="46" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:CreateAttachmentResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:CreateAttachmentResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Attachments>
<t:FileAttachment>
<t:AttachmentId Id="AAAYAE5pa29sYXkuR3VyeWFub3ZAYXphbC5hegBGAAAAAAAM4X07f1m5TruMA1KYuHzKBwCYiVmSeaoqQ5DPKXS6XYPUAAAAv1MsAACiYL/sCHf3TKGIeSfzqioDAAbhmG/mAAABEgAQAPuV+vqkAj1FltQh+hc7myw=" RootItemId="AAAYAE5pa29sYXkuR3VyeWFub3ZAYXphbC5hegBGAAAAAAAM4X07f1m5TruMA1KYuHzKBwCYiVmSeaoqQ5DPKXS6XYPUAAAAv1MsAACiYL/sCHf3TKGIeSfzqioDAAbhmG/mAAA=" RootItemChangeKey="CQAAABYAAACiYL/sCHf3TKGIeSfzqioDAAbiJMe5"/>
</t:FileAttachment>
</m:Attachments>
</m:CreateAttachmentResponseMessage>
</m:ResponseMessages>
</m:CreateAttachmentResponse>
</s:Body>
</s:Envelope>
3)现在您可以使用以下请求发送电子邮件。确保在请求中输入正确的 ID 和“更改密钥”值。当收到上述请求的响应时,您会得到这些值。
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Body>
<SendItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
SaveItemToFolder="true">
<ItemIds>
<t:ItemId Id="AAAYAE5pa29sYXkuR3VyeWFub3ZAYXphbC5hegBGAAAAAAAM4X07f1m5TruMA1KYuHzKBwCYiVmSeaoqQ5DPKXS6XYPUAAAAv1MsAACiYL/sCHf3TKGIeSfzqioDAAbhmG/mAAA="
ChangeKey="CQAAABYAAACiYL/sCHf3TKGIeSfzqioDAAbiJMe5" />
</ItemIds>
</SendItem>
</soap:Body>
</soap:Envelope>
回复:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1497" MinorBuildNumber="46" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:SendItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:SendItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
</m:SendItemResponseMessage>
</m:ResponseMessages>
</m:SendItemResponse>
</s:Body>
</s:Envelope>
现在一切都完成了。带有文件附件的电子邮件已成功发送。