如何使用 Exchange Web 服务 (EWS) 通过共享邮箱发送电子邮件 API

How to send email via a Shared MailBox using Exchange Web Services (EWS) API

我正在使用 MS Exchange Web 服务通过共享邮箱发送电子邮件 API。

发送电子邮件有效,但它们没有保存在已发送的项目中。如下所示,手动操作有效,项目保存在已发送的项目中,但通过我的代码没有'保存它们:

using Microsoft.Exchange.WebServices.Data;
using System;

//Ref to Microsoft.Exchange.WebServices  v15
//Re to Microsoft.Exchange.WebServices.Auth  v15

namespace Emailing
{
    public class Email 
    {
        private string _sharedOutlookMailAccount = "aSharedEmailAccount@something.com";        
        private ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

        public Email(string exchangeURL = "https://webmail.something.com/ews/exchange.asmx")
        {
            try
            {
                exchangeService.AutodiscoverUrl(_sharedOutlookMailAccount);
                //exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, _sharedOutlookMailAccount);

                exchangeService.UseDefaultCredentials = true;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                //...
            }
            catch (Exception ex)
            {
                //...
            }
        }

        public bool SendEmailFromSharedMailBox(string emailTo, string emailCc, string emailBcc, string emailSubject, string emailBody, string[] emailFileAttachments, bool emailFromSharedMailbox = false, bool sendToDraftOnly = false)
        {
            EmailMessage message = default(EmailMessage);
            message = new EmailMessage(exchangeService);

            emailTo = emailTo.TrimEnd(';', ',');
            string[] emailArr = emailTo.Split(';', ',');
            if (emailTo.Length > 0) message.ToRecipients.AddRange(emailArr);

            emailCc = emailCc.TrimEnd( ';', ',' );
            emailArr = emailCc.Split(';', ',' );
            if (emailCc.Length > 0) message.CcRecipients.AddRange(emailArr);

            emailBcc = emailBcc.TrimEnd(';', ',');
            emailArr = emailBcc.Split(';', ',');
            if (emailBcc.Length > 0) message.BccRecipients.AddRange(emailArr);

            #if DEBUG
                emailSubject = "IGNORE - TESTING ONLY - " + emailSubject;
            #endif

            message.Subject = emailSubject;

            EmailAddress fromSender = new EmailAddress();
            fromSender.MailboxType = MailboxType.Mailbox;
            fromSender.Address = _sharedOutlookMailAccount;
            message.From = fromSender;

            if (emailFileAttachments != null)
            {
                foreach (string fileAttachment in emailFileAttachments)
                {
                    if (string.IsNullOrEmpty(fileAttachment) == false)
                        message.Attachments.AddFileAttachment(fileAttachment);
                }
            }

            message.Sensitivity = Sensitivity.Private;
            message.Body = new MessageBody();
            message.Body.BodyType = BodyType.HTML;
            message.Body.Text = emailBody; //+= "_sharedMailSignature";

            //Save the email message
            try
            {
                //THIS WORKS AND IS REQUIRED FOR THE Send() &/or SendAndSaveCopy() METHODS TO WORK
                message.Save(new FolderId(WellKnownFolderName.Drafts, _sharedOutlookMailAccount));
            }
            catch (Exception ex)
            {
                //...
                return false;
            }

            if (!sendToDraftOnly)
            {
                try
                {
                    //==================================================================
                    //THIS SENDS EMAILS BUT THEY ARE NOT SAVED IN THE SENT ITEMS!
                    //==================================================================
                    message.SendAndSaveCopy(new FolderId(WellKnownFolderName.Drafts, _sharedOutlookMailAccount));
                }
                catch (Exception ex)
                {
                    //...
                    return false;
                }
            }
        return true;
    }
}

有谁知道如何将已发送的电子邮件保存到“已发送邮件”文件夹中?

我需要使用 WellKnownFolderName.SentItems 而不是草稿。

  message.SendAndSaveCopy(new FolderId(WellKnownFolderName.SentItems, _sharedOutlookMailAccount));

此 Microsoft 知识库 (KB) 文章中描述了此问题的

其他 causes/resolutions: https://support.microsoft.com/en-au/help/2958272/email-sent-using-outlook-are-not-saved-to-the-sent-items-folder

是的,您可以使用 SendAndSaveCopy 函数来实现您的需求。这是关于 EWS 的官方文档 API.

Send the email message and save a copy