我剛剛使用EWS(Exchange Web服務),我正在尋找一個演示如何發送帶附件的電子郵件的簡單示例。我搜索了一個例子,我找不到任何簡單明瞭的方法。我找到了關於如何發送電子郵件但不發送帶附件的電子郵件的示例。交換Web服務 - 發送帶附件的電子郵件
有沒有人有他們會推薦的例子的鏈接?在這裏發佈一個例子也會起作用!
我剛剛使用EWS(Exchange Web服務),我正在尋找一個演示如何發送帶附件的電子郵件的簡單示例。我搜索了一個例子,我找不到任何簡單明瞭的方法。我找到了關於如何發送電子郵件但不發送帶附件的電子郵件的示例。交換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);
}
你能告訴我們你的進口嗎? – 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");
您是使用託管API還是僅使用EWS?這些位有所不同,但仍然很容易。按照您發現的創建電子郵件實例的教程,然後在託管API中,您只需執行以下操作:email.Attachments.Add(fileName); – Chris 2010-09-30 09:39:24
我只使用EWS。我找到了一個創建FileAttachmentType的例子,然後從該文件附件創建一個CreateAttachmentType。然後它使用CreateAttachmentType調用ews.CreateAttachment。那是我應該做的嗎?正如你的回答所暗示的那樣,我希望它會更直觀一些,但我發現將一個文件附加到電子郵件中比我預期的要更「模糊」。 – Anthony 2010-09-30 14:23:42