How to send email and create appointment item using EWS Managed API in VB.net?
It does seem to be a bit of a dearth of VB.net samples for Exchange Web Service Managed API. So here is a sample VB.net code which demonstrates:
- Send Email
- Create Appointment
NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This sample code assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment.
Imports System.Net
Imports System.Net.Security
Imports Microsoft.Exchange.WebServices.Data
Imports System.Security.Cryptography.X509Certificates
Public Class Form1
'Creating Servie object for EWS service binding endpoint
Dim service As New ExchangeService(requestedServerVersion:=ExchangeVersion.Exchange2007_SP1)
Private Sub cmdSendMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSendMail.Click
Try
'Create new message object and set properties required to send a mail
Dim message As EmailMessage = New EmailMessage(service)
message.Subject = "Hello from the EWS Managed API"
message.Body = "Now that's easy!"
message.ToRecipients.Add("testex2007@bex2007.com")
message.SendAndSaveCopy()
MessageBox.Show("Email Sent!!!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add a valid EWS service end point here or user Autodiscover
service.Url = New Uri("https://server/ews/exchange.asmx")
'Add a valid user credentials
service.Credentials = New WebCredentials("User", "Password", "Domain")
'To address the SSL challenge
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
End Sub
Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal
chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
'Return True to force the certificate to be accepted.
Return True
End Function
Private Sub cmdCreateAppointment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles cmdCreateAppointment.Click
Try
'Create appointment object and set properties as required
Dim appt As Appointment = New Appointment(service)
appt.Subject = "Holidays"
appt.Body = "The appointment is for holiday placeholder"
appt.Start = New DateTime(2010, 11, 1)
appt.End = appt.Start.AddHours(24)
appt.IsAllDayEvent = True
appt.LegacyFreeBusyStatus = LegacyFreeBusyStatus.OOF
appt.Save(WellKnownFolderName.Calendar, SendInvitationsMode.SendToNone)
MessageBox.Show("Appointment Added to Calendar")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
We can refer to the articles mentioned below for further reading:
- Exchange Web Services Architecture
https://msdn.microsoft.com/en-us/library/aa579369.aspx - Introducing the Exchange Web Services Managed API 1.0
https://msdn.microsoft.com/en-us/library/dd637749(EXCHG.80).aspx - Working with the EWS Managed API
https://msdn.microsoft.com/en-us/library/dd633696(EXCHG.80).aspx - Exchange Web Services Managed API
https://www.microsoft.com/downloads/details.aspx?FamilyID=C3342FB3-FBCC-4127-BECF-872C746840E1&displaylang=en&displaylang=en - EWSEditor
https://code.msdn.microsoft.com/ewseditor - Creating Appointments and Meetings
https://msdn.microsoft.com/en-us/library/dd633661(EXCHG.80).aspx
Enjoy EWS and Happy Holidays!!!