by

EWS Fundamentals : Sending Messages utilizing EWS

EWS Fundamentals : Sending Messages utilizing EWS One of the fundamental belongings you may need to do with EWS is Ship a Message, whereas its could sound straightforward sending messages programmatically will be at occasions complicated.

However first lets take one step again for a second from EWS and have a look at the other ways you might ship E-mail programmatically on Change.

SMTP : (Easy Mail Switch Protocol) SMTP is the spine protocol for E-mail and is how most E-mail is transferred between servers on the Web. Its additionally the protocol POP and IMAP purchasers use to ship e mail. As a result of SMTP is a protocol (which means its a algorithm outlined in a RFC) quite then an API to make use of it it’s essential to use a library that gives you some objects that you could code in opposition to that can then generated the mandatory communication that follows the protocol guidelines. Some instance of those are CDOSys, and System.NET.Mail. From an Change technical standpoint once you submit a message through SMTP your submitting it straight into the Transport Pipeline so it would not going by Mailbox Retailer.

Pickup and Replay Directories : These are particular directories on the Transport server that you could place Messages into https://technet.microsoft.com/en-us/library/bb124230(v=exchg.150).aspx sometimes utilized by specialised purposes like overseas connectors https://technet.microsoft.com/en-us/library/aa996779(v=exchg.150).aspx

Mailbox API’s : These are the particular API’s that Microsoft has made accessible to entry a Mailbox that are MAPI, EWS Managed API and for Workplace365 the brand new REST api. (ActiveSync is also used however is a extra specialised protocol) . If you ship a Message through one among these Mailbox API’s you first have to speak to the Mailbox position server (through the CAS ) and the message is submitted to Change Retailer which can then ship it by into the transport pipeline. As a part of that course of you might select to avoid wasting a replica of the message your sending into the sentItems folder of the consumer who’s sending the message. As you’re sending through the Retailer you can too assert the SendOnBehalf rights should you ship a message on behalf of a delegate.

EWS : To Ship a Message utilizing EWS you utilize the SendItem operation https://msdn.microsoft.com/en-us/library/workplace/aa580238(v=exchg.150).aspx you can too use the CreateItem Operation and set the SendDisposition to Ship or SendAndSaveCopy. Relying on in case your sending a message with Attachments you might have to make a number of requests to the server to create a draft message after which add attachments. Within the EWS Managed API this complexity is carried out within the API so you do not have to fret about it. eg the next is a fundamental perform for sending a message utilizing EWS in PowerShell.

perform Ship-EWSMessage  {
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$false)] [string]$url,
[Parameter(Place=6, Obligatory=$true)] [String]$To,
[Parameter(Place=7, Obligatory=$true)] [String]$Topic,
[Parameter(Place=8, Obligatory=$true)] [String]$Physique,
[Parameter(Place=9, Obligatory=$false)] [String]$Attachment
)
Start
{
if($url){
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials -url $url
}
else{
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials
}
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$folderid= new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
$SentItems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$EmailMessage = New-Object Microsoft.Change.WebServices.Information.EmailMessage -ArgumentList $service
$EmailMessage.Topic = $Topic
#Add Recipients
$EmailMessage.ToRecipients.Add($To)
$EmailMessage.Physique = New-Object Microsoft.Change.WebServices.Information.MessageBody
$EmailMessage.Physique.BodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::HTML
$EmailMessage.Physique.Textual content = "Body"
$EmailMessage.From = $MailboxName
if($Attachment)
{
$EmailMessage.Attachments.AddFileAttachment($Attachment)
}
$EmailMessage.SendAndSaveCopy($SentItems.Id)

}
}

One different method of sending a Message in EWS is you need to use the MIMEContent that possibly generated by one other library or possibly a message that was exported or saved eg

perform Ship-MimeMessage {
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$false)] [switch]$useImpersonation,
[Parameter(Place=3, Obligatory=$false)] [String]$MimeMessage
)
Start
{
if($url){
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials -url $url
}
else{
$service = Join-Change -MailboxName $MailboxName -Credentials $Credentials
}
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$folderid= new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
$EmailMessage = New-Object Microsoft.Change.WebServices.Information.EmailMessage -ArgumentList $service
[byte[]]$bdBinaryData1 = [System.IO.File]::ReadAllBytes($MimeMessage)
$EmailMessage.MimeContent = new-object Microsoft.Change.WebServices.Information.MimeContent("UTF-8", $bdBinaryData1)
$EmailMessage.SendAndSaveCopy($SentItems.Id)
}

}

Each of those features come from the next github script https://github.com/gscales/Powershell-Scripts/blob/grasp/EWSSend.ps1

Sending Choices : To Ship a Message as one other consumer (eg different then the present consumer getting used to authenticate) it’s essential to have been granted the SendAs permission on a Mailbox (which is a separate permission from the Mailbox rights granted through Add-MailboxPermission). The opposite possibility in EWS is that you need to use EWS impersonation which might give the caller the identical rights because the Mailbox proprietor,  the script I’ve posted on GitHub has an possibility to make use of EWS Impersonation.

Each of those instance features save a replica of the message being despatched into the SentItemFolder of the Mailbox handed into the Perform. That is managed through the

$EmailMessage.SendAndSaveCopy($SentItems.Id)

line should you did not need to save a replica to the SentItem folder you need to use the Ship Technique as a substitute eg

$EmailMessage.Ship()

This may set the mandatory MessageDispostion worth within the SOAP request.

EWS solely lets you ship utilizing one physique format eg it’s essential to select between HTML or Textual content should you do need to use the extra superior physique varieties allowed in MIME look to make use of the MIME Content material choice to ship a message. There may be additionally an instance of sending an encrypted message utilizing the MIME content material  https://blogs.msdn.microsoft.com/emeamsgdev/2015/08/10/ews-how-to-send-signed-email-using-the-ews-managed-api/

Utilizing VBA or VB6 : In case your nonetheless having fun with the retro Programing languages like VBA or VB6 its nonetheless potential to make use of EWS to ship a message out of your code by manually setting up the SOAP message concerned. This possibly helpfully the place your enviorment has been migrated to the cloud and your CDOsys code that was utilizing  SMTP to ship messages now not works. As a piece round you need to use a easy perform like this to do a Ship utilizing EWS in Workplace365 (or change the URL to you OnPrem EWS endpoint).

Sub SendMessage(Topic As String, Recipient As String, Physique As String, Consumer As String, Password As String)
Dim sReq As String
Dim xmlMethod As String
Dim XMLreq As New MSXML2.XMLHTTP60
Dim EWSEndPoint As String
EWSEndPoint = "https://outlook.office365.com/EWS/Exchange.asmx"
sReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
sReq = sReq & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">" & vbCrLf
sReq = sReq & "<soap:Header>" & vbCrLf
sReq = sReq & "<t:RequestServerVersion Version=""Exchange2010""/>" & vbCrLf
sReq = sReq & "</soap:Header>" & vbCrLf
sReq = sReq & "<soap:Body>" & vbCrLf
sReq = sReq & "<CreateItem MessageDisposition=""SendAndSaveCopy"" xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages"">" & vbCrLf
sReq = sReq & "<SavedItemFolderId>" & vbCrLf
sReq = sReq & "<t:DistinguishedFolderId Id=""sentitems"" />" & vbCrLf
sReq = sReq & "</SavedItemFolderId>" & vbCrLf
sReq = sReq & "<Items>" & vbCrLf
sReq = sReq & "<t:Message>" & vbCrLf
sReq = sReq & "<t:ItemClass>IPM.Note</t:ItemClass>" & vbCrLf
sReq = sReq & "<t:Subject>" & Topic & "</t:Subject>" & vbCrLf
sReq = sReq & "<t:Body BodyType=""Text"">" & Physique & "</t:Body>" & vbCrLf
sReq = sReq & "<t:ToRecipients>" & vbCrLf
sReq = sReq & " <t:Mailbox>" & vbCrLf
sReq = sReq & " <t:EmailAddress>" & Recipient & "</t:EmailAddress>" & vbCrLf
sReq = sReq & " </t:Mailbox>" & vbCrLf
sReq = sReq & "</t:ToRecipients>" & vbCrLf
sReq = sReq & "</t:Message>" & vbCrLf
sReq = sReq & "</Items>" & vbCrLf
sReq = sReq & "</CreateItem>" & vbCrLf
sReq = sReq & "</soap:Body>" & vbCrLf
sReq = sReq & "</soap:Envelope>" & vbCrLf
xmlMethod = "POST"
XMLreq.Open xmlMethod, EWSEndPoint, False, Consumer, Password
XMLreq.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
XMLreq.setRequestHeader "Translate", "F"
XMLreq.setRequestHeader "User-Agent", "VBAEWSSender"
XMLreq.ship sReq
If XMLreq.Standing = 200 Then
' Message Despatched okay
Else
' One thing went Unsuitable
Finish If
Finish Sub