Search code examples
.netvb.netemailexchange-serverexchangewebservices

Send email via Exchange Web Services (EWS)


I'm trying to send an email through exchange server using Exchange Web Services (EWS). I've configured some parameters but it can't send. I've tried other methods, like third party assemblies, and with that is possible to send, because of that I think I've missed some parameters or method to configure my connection.

I hope you can help me.

I've pasted some code below ...

        Try
            Dim vFrom As String = "mail@domain.exchange.com"
            Dim vTo As String = "personal@mydomain.es" 
            Dim vSubject As String = "Test " & DateTime.Now.ToString
            Dim vBody As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit ..."
            Dim vHost As String = "webmail.domain.exchange.com"
            Dim vUsr As String = "boss@mydomain.es"
            Dim vPsw As String = "1234567890" 

            Dim service As New ExchangeService(ExchangeVersion.Exchange2007_SP1)
            service.Credentials = New WebCredentials(vUsr, vPsw, vHost)
            service.Url = New System.Uri("https://" & vHost)

            Dim mail As New EmailMessage(service)
            mail.From = vFrom
            mail.ToRecipients.Add("John", vTo)
            mail.Subject = vSubject
            mail.Body = vBody
            mail.SendAndSaveCopy()

        Catch ex As ServiceRequestException
            MsgBox(ex.Message + " - sendMailExchange(ServiceRequestException)")
        Catch ex As WebException
            MsgBox(ex.Message + " - sendMailExchange(WebException)")
            'Catch ex As SmtpException
            '    MsgBox(ex.Message + " - sendMailExchange(SmtpException)")
        Catch ex As Exception
            MsgBox(ex.Message + " - sendMailExchange(Exception)")
        End Try

Thanks for help...


Solution

  • I know this is an old thread ... but since I had a similar problem today for which I found the solution I thought I would let you know ...

    The issue is the URI you are using. Instead of "https://webmail.domain.exchange.com" you should try "https://webmail.domain.exchange.com/EWS/Exchange.asmx"