home *** CD-ROM | disk | FTP | other *** search
/ 66.142.0.142 / 66.142.0.142.tar / 66.142.0.142 / EWSTest.aspx.vb < prev    next >
Text File  |  2014-11-01  |  10KB  |  176 lines

  1. ∩╗┐Imports AspNetMaker7_tfpssnet
  2. Imports System.Net
  3. Imports System.Net.Security
  4. Imports Microsoft.Exchange.WebServices.Data
  5. Imports Microsoft.Exchange.WebServices.Autodiscover
  6. Imports System.Security.Cryptography.X509Certificates
  7.  
  8.  
  9. Partial Class EWSTest
  10.     Inherits System.Web.UI.Page
  11.  
  12.     'Dim service As New ExchangeService(requestedServerVersion:=ExchangeVersion.Exchange2010)
  13.     Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
  14.  
  15.     Private Sub cmdSendMail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  16.         'Add a valid EWS service end point here or user Autodiscover
  17.         '        service.Url = New Uri("https://server/ews/exchange.asmx")
  18.         'service.Url = New Uri("https://server14/ews/exchange.asmx")
  19.         service.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")
  20.         'Add a valid user credentials
  21.         'service.Credentials = New WebCredentials("prodservices", "production88", "Business")
  22.         service.Credentials = New WebCredentials("productionservices@tfc.org", "production88")
  23.         service.AutodiscoverUrl("productionservices@tfc.org", AddressOf RedirectionUrlValaditionCallBack)
  24.         'To address the SSL challenge
  25.         ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
  26.  
  27.         Try
  28.             'Create new message object and set properties required to send a mail
  29.             Dim message As EmailMessage = New EmailMessage(service)
  30.             message.Subject = "Hello from the EWS Managed API"
  31.             message.Body = "This is a test message from the new EWS mail transport!"
  32.             'message.ToRecipients.Add("tony.clayton@suddenlink.net")
  33.             'message.ToRecipients.Add("tonyc@tfchurch.org")
  34.             message.ToRecipients.Add(txtTO.Text)
  35.             message.From = "productionservices@tfc.org"
  36.             message.SendAndSaveCopy()
  37.             lblMessage.Text = "Email sent."
  38.         Catch ex As Exception
  39.             lblMessage.Text = ex.Message
  40.         End Try
  41.     End Sub
  42.  
  43.     Private Shared Function RedirectionUrlValaditionCallBack(ByVal redirectionURL As String) As Boolean
  44.         Dim result As Boolean = False
  45.         Dim redirectionuri As New Uri(redirectionURL)
  46.         If redirectionuri.Scheme = "https" Then
  47.             result = True
  48.         End If
  49.         Return result
  50.     End Function
  51.     Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
  52.         'Return True to force the certificate to be accepted.
  53.         Return True
  54.     End Function
  55.  
  56.     Private Sub cmdCreateAppointment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppt.Click
  57.         'Add a valid EWS service end point here or user Autodiscover
  58.         '        service.Url = New Uri("https://server/ews/exchange.asmx")
  59.         'service.Url = New Uri("https://server14/ews/exchange.asmx")
  60.         service.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")
  61.         'Add a valid user credentials
  62.         'service.Credentials = New WebCredentials("prodservices", "production88", "Business")
  63.         service.Credentials = New WebCredentials("productionservices@tfc.org", "production88")
  64.         service.AutodiscoverUrl("productionservices@tfc.org", AddressOf RedirectionUrlValaditionCallBack)
  65.         'To address the SSL challenge
  66.         ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
  67.         service.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, txtTO.Text)
  68.  
  69.         lblBody.Text = ""
  70.         lblEndDateTime.Text = ""
  71.         lblMessage.Text = ""
  72.         lblStartDateTime.Text = ""
  73.         lblSubject.Text = ""
  74.         lblMisc.Text = ""
  75.         Try
  76.             'Create appointment object and set properties as required
  77.             Dim appt As Appointment = New Appointment(service)
  78.             appt.Subject = "Test Calendar Entry"
  79.             appt.Body = "This is an EWS appointment test."
  80.             appt.Start = New DateTime(Now().Year, Now().Month, Now().Day, 8, 0, 0)
  81.             appt.End = appt.Start.AddHours(1)
  82.             appt.IsReminderSet = False
  83.  
  84.             appt.Save(WellKnownFolderName.Calendar, SendInvitationsMode.SendToNone)
  85.             lblMessage.Text = "EWS Test Appointment added to calendar."
  86.         Catch ex As Exception
  87.             lblMessage.Text = ex.Message
  88.         End Try
  89.     End Sub
  90.  
  91.     Private Sub cmdDeleteAppointment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteAppt.Click
  92.         'Add a valid EWS service end point here or user Autodiscover
  93.         '        service.Url = New Uri("https://server/ews/exchange.asmx")
  94.         'service.Url = New Uri("https://server14/ews/exchange.asmx")
  95.         service.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")
  96.         'Add a valid user credentials
  97.         'service.Credentials = New WebCredentials("prodservices", "production88", "Business")
  98.         service.Credentials = New WebCredentials("productionservices@tfc.org", "production88")
  99.         service.AutodiscoverUrl("productionservices@tfc.org", AddressOf RedirectionUrlValaditionCallBack)
  100.         'To address the SSL challenge
  101.         ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
  102.         service.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, txtTO.Text)
  103.  
  104.         lblBody.Text = ""
  105.         lblEndDateTime.Text = ""
  106.         lblMessage.Text = ""
  107.         lblStartDateTime.Text = ""
  108.         lblSubject.Text = ""
  109.         lblMisc.Text = ""
  110.         Try
  111.             lblMessage.Text = ""
  112.             Dim findResults As FindItemsResults(Of Appointment) = service.FindAppointments(WellKnownFolderName.Calendar, New CalendarView(DateTime.Now, DateTime.Now))
  113.             Dim items As New List(Of Item)()
  114.             For Each appointment As Appointment In findResults
  115.                 items.Add(appointment)
  116.             Next
  117.  
  118.             If items.Count <> 0 Then
  119.                 service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties)
  120.                 For Each Appointment As Appointment In items
  121.                     If Not Appointment.IsCancelled Then
  122.                         If Appointment.Subject = "Test Calendar Entry" Then
  123.                             Appointment.Delete(DeleteMode.HardDelete)
  124.                             lblMessage.Text = "EWS Test Appointment deleted from calendar."
  125.                         End If
  126.                     End If
  127.                 Next
  128.             End If
  129.         Catch ex As Exception
  130.             lblMessage.Text = ex.Message
  131.         End Try
  132.     End Sub
  133.  
  134.     Private Sub cmdFindAppointment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmFindAppt.Click
  135.         'Add a valid EWS service end point here or user Autodiscover
  136.         '        service.Url = New Uri("https://server/ews/exchange.asmx")
  137.         'service.Url = New Uri("https://server14/ews/exchange.asmx")
  138.         service.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")
  139.         'Add a valid user credentials
  140.         'service.Credentials = New WebCredentials("prodservices", "production88", "Business")
  141.         service.Credentials = New WebCredentials("productionservices@tfc.org", "production88")
  142.         service.AutodiscoverUrl("productionservices@tfc.org", AddressOf RedirectionUrlValaditionCallBack)
  143.         'To address the SSL challenge
  144.         ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
  145.         service.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, txtTO.Text)
  146.  
  147.         lblBody.Text = ""
  148.         lblEndDateTime.Text = ""
  149.         lblMessage.Text = ""
  150.         lblStartDateTime.Text = ""
  151.         lblSubject.Text = ""
  152.         lblMisc.Text = ""
  153.         Dim findResults As FindItemsResults(Of Appointment) = service.FindAppointments(WellKnownFolderName.Calendar, New CalendarView(DateTime.Now, DateTime.Now))
  154.         Dim items As New List(Of Item)()
  155.         For Each appointment As Appointment In findResults
  156.             items.Add(appointment)
  157.         Next
  158.  
  159.         If items.Count <> 0 Then
  160.             service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties)
  161.             For Each Appointment As Appointment In items
  162.                 If Not Appointment.IsCancelled Then
  163.                     'lblMessage.Text += "From " & Appointment.Start.Date & " " & Appointment.Start.TimeOfDay.ToString & " thru " & Appointment.End.Date & " " & Appointment.End.TimeOfDay.ToString & ": " & Appointment.Body.Text & vbCrLf & vbCrLf & vbCrLf
  164.                     lblStartDateTime.Text = "Start: " & Appointment.Start.Date & " " & Appointment.Start.TimeOfDay.ToString
  165.                     lblEndDateTime.Text = "End: " & Appointment.End.Date & " " & Appointment.End.TimeOfDay.ToString
  166.                     lblSubject.Text = "Subject: " & Appointment.Subject
  167.                     lblBody.Text = "Body: " & Appointment.Body.Text
  168.                     lblMisc.Text = "Location: " & Appointment.Location
  169.                 End If
  170.             Next
  171.         End If
  172.  
  173.     End Sub
  174.  
  175. End Class
  176.