API通过共享邮箱发送电子邮件

API通过共享邮箱发送电子邮件

本文介绍了如何使用Exchange Web服务(EWS)API通过共享邮箱发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sending emails via a shared mailbox using the MS Exchange Web Services API.

发送电子邮件有效,但未将其保存在已发送的邮件中.如下所示,手动进行时,邮件已保存在已发送邮件"中,但通过我的代码无法保存:

Sending emails works but they are not saved in the sent items. As shown below doing it manually works, the items are saved in the Sent Items, but via my code doesn't save them:

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 = "[email protected]";
        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;
    }
}

有人知道我如何将已发送的电子邮件保存到已发送邮件"文件夹中吗?

Does anyone know how I can save the sent emails into the Sent Items folder?

推荐答案

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

I needed to use WellKnownFolderName.SentItems not Drafts.

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


此Microsoft知识库(KB)文章中介绍了此问题的其他原因/解决方案: https://support.microsoft.com/zh-cn/help/2958272/email-sent-using-outlook-are-not-saved-to-the-sent-items-folder


Other causes/resolutions for this issue are described in this Microsoft Knowledge Base (KB) Article:https://support.microsoft.com/en-au/help/2958272/email-sent-using-outlook-are-not-saved-to-the-sent-items-folder

这篇关于如何使用Exchange Web服务(EWS)API通过共享邮箱发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:11