Looking for a (non-proprietary) way of sending mail from Microsoft Access 2016 that does not use CDO (or Outlook). We have been doing this for years using CDO:
...but now my company is changing to a different IT-provider, that does not support CDO. We have been advised to change to Exchange Web Services. I use the following found here (third option, slightly modified), but get error message 500:
The SOAP resolves to:
Code:
Dim MyMessage As Object
Set MyMessage = CreateObject("CDO.Message")
MyMessage.Subject = MySubject
MyMessage.To = MyRecipient
MyMessage.TextBody = MyText
MyMessage.CC = MyCC
MyMessage.From = MyFrom
With MyMessage
.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = pubMailServer
.Configuration.Fields.Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
.Configuration.Fields.Update
On Error GoTo Err_mail
.Send
End With
...but now my company is changing to a different IT-provider, that does not support CDO. We have been advised to change to Exchange Web Services. I use the following found here (third option, slightly modified), but get error message 500:
Code:
Sub SendMessage()
Dim Subject As String, Recipient As String, Body As String, User As String, Password As String
Dim sReq As String
Dim xmlMethod As String
Dim xmlreq As New MSXML2.XMLHTTP60
Dim EWSEndPoint As String
Subject = "Mijn onderwerp"
Recipient = "xxxx@xxxx.nl"
Body = "Hello!"
User = "xxxxx"
Password = "xxxxx"
EWSEndPoint = "[URL unfurl="true"]https://mail.xxxxx.nl/EWS/Exchange.asmx"[/URL]
sReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
sReq = sReq & "<soap:Envelope xmlns:soap=""[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/""[/URL] xmlns:t=""[URL unfurl="true"]http://schemas.microsoft.com/exchange/services/2016/types"">"[/URL] & vbCrLf
sReq = sReq & "<soap:Header>" & vbCrLf
sReq = sReq & "<t:RequestServerVersion Version=""Exchange2016""/>" & vbCrLf
sReq = sReq & "</soap:Header>" & vbCrLf
sReq = sReq & "<soap:Body>" & vbCrLf
sReq = sReq & "<CreateItem MessageDisposition=""SendAndSaveCopy"" xmlns=""[URL unfurl="true"]http://schemas.microsoft.com/exchange/services/2016/messages"">"[/URL] & 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>" & Subject & "</t:Subject>" & vbCrLf
sReq = sReq & "<t:Body BodyType=""Text"">" & Body & "</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, User, Password
xmlreq.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
xmlreq.setRequestHeader "Translate", "F"
xmlreq.setRequestHeader "User-Agent", "VBAEWSSender"
xmlreq.Send sReq
If xmlreq.Status = 200 Then
MsgBox "Gelukt!" ' Message Sent okay
Else
MsgBox "Mislukt! " & xmlreq.Status ' Something went Wrong
End If
End Sub
The SOAP resolves to:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/"[/URL] xmlns:t="[URL unfurl="true"]http://schemas.microsoft.com/exchange/services/2016/types">[/URL]
<soap:Header>
<t:RequestServerVersion Version="Exchange2016"/>
</soap:Header>
<soap:Body>
<CreateItem MessageDisposition="SendAndSaveCopy" xmlns="[URL unfurl="true"]http://schemas.microsoft.com/exchange/services/2016/messages">[/URL]
<SavedItemFolderId>
<t:DistinguishedFolderId Id="sentitems" />
</SavedItemFolderId>
<Items>
<t:Message>
<t:ItemClass>IPM.Note</t:ItemClass>
<t:Subject>Mijn onderwerp</t:Subject>
<t:Body BodyType="Text">Hello!</t:Body>
<t:ToRecipients>
<t:Mailbox>
<t:EmailAddress>xxxx@xxxx.nl</t:EmailAddress>
</t:Mailbox>
</t:ToRecipients>
</t:Message>
</Items>
</CreateItem>
</soap:Body>
</soap:Envelope>