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












1















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:



enter image description here



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;
}
}


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










share|improve this question





























    1















    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:



    enter image description here



    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;
    }
    }


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










    share|improve this question



























      1












      1








      1








      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:



      enter image description here



      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;
      }
      }


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










      share|improve this question
















      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:



      enter image description here



      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;
      }
      }


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







      c# email exchange-server exchangewebservices exchange-server-2010






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 8 at 3:18







      Jeremy Thompson

















      asked Nov 21 '18 at 23:14









      Jeremy ThompsonJeremy Thompson

      39.7k13108203




      39.7k13108203
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I needed to use WellKnownFolderName.SentItems not Drafts.



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




          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






          share|improve this answer

































            0














            Yes, you can use the SendAndSaveCopy function to achieve your needs. Here is an official document about EWS API.



            Send the email message and save a copy






            share|improve this answer
























            • That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

              – Jeremy Thompson
              Nov 22 '18 at 22:12











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421766%2fhow-to-send-email-via-a-shared-mailbox-using-exchange-web-services-ews-api%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I needed to use WellKnownFolderName.SentItems not Drafts.



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




            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






            share|improve this answer






























              0














              I needed to use WellKnownFolderName.SentItems not Drafts.



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




              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






              share|improve this answer




























                0












                0








                0







                I needed to use WellKnownFolderName.SentItems not Drafts.



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




                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






                share|improve this answer















                I needed to use WellKnownFolderName.SentItems not Drafts.



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




                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







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 1:21

























                answered Nov 21 '18 at 23:23









                Jeremy ThompsonJeremy Thompson

                39.7k13108203




                39.7k13108203

























                    0














                    Yes, you can use the SendAndSaveCopy function to achieve your needs. Here is an official document about EWS API.



                    Send the email message and save a copy






                    share|improve this answer
























                    • That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                      – Jeremy Thompson
                      Nov 22 '18 at 22:12
















                    0














                    Yes, you can use the SendAndSaveCopy function to achieve your needs. Here is an official document about EWS API.



                    Send the email message and save a copy






                    share|improve this answer
























                    • That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                      – Jeremy Thompson
                      Nov 22 '18 at 22:12














                    0












                    0








                    0







                    Yes, you can use the SendAndSaveCopy function to achieve your needs. Here is an official document about EWS API.



                    Send the email message and save a copy






                    share|improve this answer













                    Yes, you can use the SendAndSaveCopy function to achieve your needs. Here is an official document about EWS API.



                    Send the email message and save a copy







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 11:20









                    Simon LiSimon Li

                    23014




                    23014













                    • That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                      – Jeremy Thompson
                      Nov 22 '18 at 22:12



















                    • That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                      – Jeremy Thompson
                      Nov 22 '18 at 22:12

















                    That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                    – Jeremy Thompson
                    Nov 22 '18 at 22:12





                    That won't work, you NEED to use the message.SendAndSaveCopy(); overload that accepts the FolderId. Otherwise you get the error message: SendOnly cannot be used by a user without a mailbox. Use SendAndSaveCopy and specify a folder ID in a mailbox to send an item from an account that doesn't have a mailbox.

                    – Jeremy Thompson
                    Nov 22 '18 at 22:12


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421766%2fhow-to-send-email-via-a-shared-mailbox-using-exchange-web-services-ews-api%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith