exchange-server – Exchange Web Services – 发送带附件的电子邮件

exchange-server – Exchange Web Services – 发送带附件的电子邮件,第1张

概述我是新手使用 EWS(Exchange Web服务),我正在寻找一个简单的示例,演示如何发送附件电子邮件.我搜索了一个例子,我找不到任何简单明了的东西.我找到了有关如何发送电子邮件但未发送带附件的电子邮件的示例. 有没有人有他们推荐的例子的链接?在这里发布一个例子也可以正常工作! 好吧,我最终想出来了.这是一种方法,它将创建邮件消息,将其存储为草稿,添加附件,然后发送电子邮件.希望这可以帮助那 我是新手使用 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);        }
总结

以上是内存溢出为你收集整理的exchange-server – Exchange Web Services – 发送带附件的电子邮件全部内容,希望文章能够帮你解决exchange-server – Exchange Web Services – 发送带附件的电子邮件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1132742.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存