大佬教程收集整理的这篇文章主要介绍了exchange-server – Exchange Web Services – 发送带附件的电子邮件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人有他们推荐的例子的链接?在这里发布一个例子也可以正常工作!
在我的例子中,我只会发送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 Services – 发送带附件的电子邮件全部内容,希望文章能够帮你解决exchange-server – Exchange Web Services – 发送带附件的电子邮件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。