How to send EmailAndSave by email with EWS with auth and windows without a mailbox?

My scenario is this:

  • I connect to Exchange Web Services (EWS) using a service account and Windows authentication.
  • I do not use impersonation
  • The service account does not have a mailbox
  • I connect to the MyMailbox mailbox to read and send emails. Whenever I send emails, I use SendAndSaveCopy()by specifying the MyMailbox SentItems folder to save. Not indicating that the folder SendAndSaveCopy()is failing is understandable, since there is no mailbox associated with my service account where he could save the email.
  • Sometimes I want to send as or send on behalf of another user, so I set the property Fromfor this main SMTP address of another user.

In this case, everything works fine until I send an email with the application. In this case, it fails with ResponseCode = ErrorMissingEmailAddressandMessageText = When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids.

Looking at the SOAP message sent from mine SendAndSaveCopy(), I see that it is completely different when I have an attachment - pay attention toMessageDisposition="SaveOnly"

  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2010_SP2" />
    </soap:Header>
    <soap:Body>
      <m:CreateItem MessageDisposition="SaveOnly">
        <m:Items>
          <t:Message>
            <t:Subject>My test email subject</t:Subject>
            <t:Body BodyType="HTML">....

whereas when sent without attachment it has <m:CreateItem MessageDisposition="SendAndSaveCopy">. It seems reasonable that EWS first saves the email and then sends it. Indeed, the doc API says

This method does not work if the message contains unsaved attachments. In this case, the message must first be saved and then sent.

, SendAndSaveCopy() , , MSDN SendAndSaveCopy() - . , SendAndSaveCopy() EWS: CreateItem, CreateAttachment, SendItem.

: EWS , SendAndSaveCopy(), ?

: ? SendAndSaveCopy(), . CreateItem?

EWS 15.0.0.0 Exchange2010_SP2 api.

UPDATE:

, EWS Managed API EWS SendAndSaveCopy(), , auth. EWS:

  • <m:CreateItem MessageDisposition="SaveOnly"> , , SavedItemFolderId. !? , , Windows auth, .
  • ItemId .
  • EWS <m:SendItem SaveItemToFolder="true">, SavedItemFolderId

, SendAndSaveCopy(): . , API EWS Managed API ?

+4
1

SavedItemFolderId. !? , , windows auth, .

- , . EWS - API, , , , .

, MIME , , , , .

EmailMessage test2 = new EmailMessage(service);
String bodyContent = "<html><head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">Hello World</head><body></body></html> ";

CDO.Message msMessage = new CDO.Message();
msMessage.BodyPart.Charset = "UTF-8";
msMessage.HTMLBody = bodyContent;
msMessage.HTMLBodyPart.Charset = "UTF-8";
msMessage.AddAttachment("c:\\temp\\Document.docx");
ADODB.Stream asMessageStream = msMessage.GetStream();
asMessageStream.Type = ADODB.StreamTypeEnum.adTypeBinary;
byte[] bdBinaryData1 = new byte[asMessageStream.Size];
bdBinaryData1 = (byte[])asMessageStream.Read(asMessageStream.Size);
service.TraceEnabled = true;
test2.MimeContent = new MimeContent("UTF-8", bdBinaryData1);
test2.ToRecipients.Add("user@domain.com");
test2.Subject = "test";
test2.SendAndSaveCopy();

Glen

+2

Source: https://habr.com/ru/post/1621172/


All Articles