Sending Mails thru Microsoft Office 365 RRS feed

  • Question

  • What am I doing wrong

    I am new to this and this code worked when we used zimbra hosed email but when I change the info for Office 365 nothing seem to work

    The code I am using is below

    Public Sub SendMail0(ByVal strEmail As String, ByVal strSubj As String, ByVal strBody As String)
        Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
        Const cdoSendUsingPort = 2
        Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
        Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
        Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
        Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
        Const cdoBasic = 1
        Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
        Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
        Const cdoSMTPUseSSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"

        Set objConfig = CreateObject("CDO.Configuration")
        Set Fields = objConfig.Fields

        With Fields
            .Item(cdoSMTPServer) = "smtp.office365.com"
            .Item(cdoSendUsingMethod) = cdoSendUsingPort
            .Item(cdoSMTPServerPort) = 587
            .Item(cdoSMTPConnectionTimeout) = 10
            .Item(cdoSMTPAuthenticate) = cdoBasic
            .Item(cdoSendUserName) = "donotreply@xppower.com"
            .Item(cdoSendPassword) = "********"
            .Item(cdoSMTPUseSSL) = True

    '      .Item(cdoSendUsingMethod) = cdoSendUsingPort
    '      .Item(cdoSMTPServer) = "zimbrahostedemail.com"
    '      .Item(cdoSMTPServerPort) = 25
    '      .Item(cdoSMTPConnectionTimeout) = 10
    '      .Item(cdoSMTPAuthenticate) = cdoBasic
    '      .Item(cdoSendUserName) = "mailer@emcohv.com"
    '      .Item(cdoSendPassword) = "********"
        End With
        Set objmessage = CreateObject("CDO.Message")

        With objmessage
            .configuration = objConfig
            .To = strEmail
            .From = "donotreply@xppower.com"
            .Subject = strSubj
            .TextBody = "Do not reply: This email account is not monitored." & vbNewLine & vbNewLine & strBody
        End With

        Set Fields = Nothing
        Set objmessage = Nothing
        Set objConfig = Nothing
    End Sub

    but I keep getting the following error message

    "Run-time error '-2147220973(80040213)': The transport failed to connect to the server"

    If I change the port to 25 I get the same error

    If I comment out the cdoSMTPUseSSL and the  .Item(cdoSMTPUseSSL) = True I get the following error

    "Run-time error '-2147220978(8004020e)': The server rejects the sender address. The server response was: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM"

    Please help

    I have searched the internet with no avail and tried all different kinds of configurations

    I feel like it is something really easy tat I am missing

    • Edited by Pavel Celba Tuesday, February 16, 2016 10:36 PM -- " --
    Tuesday, February 16, 2016 5:57 PM


  • Thank you so much Pavel

    The way I got it to work FINALLY was to configure the email to direct send with port 25

    it surprised me that I only had to change 2 lines of code

    cdoSMTPServer and the port number

    I have been banging my head for week trying to get this to work
    Monday, February 22, 2016 7:09 PM

All replies