Sending SMTP mail via Office 365 exchange using CDO.Message and other applications

We love Office 365, specifically the Exchange/Outlook email features such as calendar sharing, etc.  But when we switched to it most of our automated email applications and scripts failed as the SMTP (and POP) configurations are different.

So here are my findings on the subject in the hope that it can help some people.

1.  Obtain SMTP Configuration Parameters

First get the parameters of your Office 365 account by going to Office 365 live (https://portal.microsoftonline.com/) login and access Outlook

Then access the Options menu – See All Options

image

Now click on the hyperlink marked “Settings for POP, IMAP, and SMTP access…”

image

and you will get a window with the server and port settings.  Something like:

Server name: podxxxxx.outlook.com
Port: 587
Encryption method: TLS

2. External Applications

If you are using applications that have their own emailing engine such as Final Builder, Telerik Test Studio, TFS, etc.

Most likely you will be able to fill in the above information easily something like:

image

Its important to notice that we need to Authenticate and use SSL and enter our user credentials

3. CDO.Message

Sending email using CDO.Message is (for reasons unknown to me slightly different).  I use VB Script from a Windows 2008 server as follows:

Dim objCDO
Set objCDO = CreateObject("CDO.Message")
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "podxxxxx.outlook.com"
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username@clicdata.com"
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
objCDO.Configuration.Fields.Update
' Rest of sending code goes here...

Please note here that the port is not 587 but 25.  Do not understand why this works but it does.  If you can explain it please let me know.

15 thoughts on “Sending SMTP mail via Office 365 exchange using CDO.Message and other applications

  1. Pingback: Help with sending email via Ouloook 365 (OWA) in Excel

  2. Great Article, this has massively helped me to send mails from my application through the Microsoft Online servers. Thank you very much!!

  3. Does not work for Office365 for me.. The combination port25+TLS shows “transport failed” error.

    Also, MS does not say that CDO.Message supports TLS:

    http://msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx

    The only working solution that I was able to find, is the one using SMTPProxy:

    http://community.office365.com/en-us/forums/158/t/18581.aspx

    I’d rather not install any extra software, but looks like this is the only way so far..
    I’m on MS Access 2010

    • Hi Pavlo,

      Sorry you are having issues. The script has described and the settings still work for us here at ClicData. So perhaps it is a difference between our accounts.

      I checked the links you sent and noticed an error or two on their example scripts. For example on one script the spelled smtperver and not smtpserver so if you copied and pasted their script take a look and see if there are no silly errors like that.

      Best of luck!

  4. Hi, Telmo,

    Congratulations for your article.

    Please with your help.

    I have a next message:

    “The transport failed to connect server”.

    Password, User, Server, all is ok.

    What do you think?

    • I think that means there is no mail server on the other end so things to check are: IP address, port # and any mail proxy settings or SSL settings (since the port # will be different).

      • Hi Telmo,

        I solved the problem, it seems the problem is in the network of the company, to test the application on an external network (eg from home), the program does work.

        Have you ever had this kind of problem?

        Anyway thank you very much for your help.

        Have a good day

        regards,

        Lenin

  5. Hi,

    i try it on my remote server but i took an error : 424 Object required

    here is my code can you help me pls?

    Dim ObjSendMail
    Dim cdoConfig
    Dim Fields ‘ As ADODB.Fields

    Set cdoConfig = CreateObject(“CDO.Configuration”)
    Set Fields = cdoConfig.Fields

    ‘This section provides the configuration information for the remote SMTP server.

    With Fields
    .Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “pod51017.outlook.com”
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”) = 30
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1
    .Item(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “insankaynaklari@tedankara.k12.tr”
    .Item(“http://schemas.microsoft.com/cdo/configuration/sendpassword”)= “Tedk123″
    .Item(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”) = False
    .Update
    End With

    ‘End remote SMTP server configuration section==

    strHTML = “”
    strHTML = strHTML & “”
    strHTML = strHTML & “”
    strHTML = strHTML & “ This is the test HTML message body
    strHTML = strHTML & “”
    strHTML = strHTML & “”

    Set ObjSendMail = CreateObject(“CDO.Message”)
    Set ObjSendMail.Configuration = cdoConfig

    With ObjSendMail

    .To = “aylink@tedankara.k12.tr” ‘ToDo: Enter a valid email address.
    .From = “insankaynaklari@tedankara.k12.tr” ‘ToDo: Enter a valid email address.
    .Subject = “This is a test CDOSYS message (Sent via Port 25)”
    .HTMLBody = strHTML

    on error resume next
    cdoMessage.Send
    if Err.Number 0 then
    response.Write “Email send failed: ” & Err.Number & “-” & Err.Description &””&vbcrlf
    end if

    .Send
    End With

  6. Hi I have this script and I stopped working when migrating to Office365 can help me thanks

    Dim objPectraOBPI
    Set objPectraOBPI = CreateObject(“PectraOBPI.Engine”)

    Dim objCDO ‘As CDO.Message

    Set objCDO = CreateObject(“CDO.Message”)

    Dim OVnro, OVSucursal, DirMail, OVProductor, OVNroCta

    OVnro = objPectraObpi.AttributeGetValue(TrxId, “OVNumero”, “”)
    OVSucursal = objPectraObpi.AttributeGetValue(TrxId, “OVSucComprobante”, “”)
    DirMail = objPectraObpi.AttributeGetValue(TrxId, “OVUMail”, “”)
    OVProductor = objPectraObpi.AttributeGetValue(TrxId, “OVRazonSocial”, “”)
    OVNroCta = objPectraObpi.AttributeGetValue(TrxId, “OVNroCta”, “”)

    actividad = ” Stock”

    comm2 = “”
    comm2 = comm2 & “”
    comm2 = comm2 & “Proceso ”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “Proceso Autorizacion de Ordenes de Venta
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “La Orden de venta Nº:” & OVSucursal & “-” & OVnro
    comm2 = comm2 & “, de la entidad ” & OVNroCta & “-” & OVProductor & ” no supero la ” & actividad & “.

    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “ Para procesar la OV ingrese a ”
    comm2 = comm2 & “<a

    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”
    comm2 = comm2 & “”

    Dim cmd, Server, WshShell, BD, usr, pass, RbtMailfrom

    Set oNode = Nothing

    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = “pod51018.outlook.com”
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “ejemple@ejemplo.com”
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “ejemplo”
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1
    objCDO.Configuration.Fields(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”) = true

    objCDO.To = “ejemo@ejemp.com”

    objCDO.CC = DirMail

    objCDO.From = “ejempl@ejemplo.com”
    objCDO.BCC = “ejemplo@ejemplo.com”
    objCDO.Subject = ” Disponibilidad (OV: ” & OVSucComprobante & “-” & OVnro & “) Productor: ” & OVNroCta & “-” & OVProductor

    objCDO.HTMLBody = comm2
    objCDO.Send

    Set objCDO = Nothing

    MsgBox (“Correo Enviado…”)

  7. Great article Telmo! I spent the entire day wondering why my script kept giving me the 530 5.7.1 authentication error. Just came across your post and changed the port to 25. Not sure why this is so but it works! Thanks!

  8. I just can’t get this to work – any thoughts?

    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2 ‘Send the message using the network (SMTP over the network).
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserver”) =”pod51018.outlook.com”
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpusessl”) = true ‘Use SSL for the connection (True or False)
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”) = 60

    ‘ If your server requires outgoing authentication uncomment the lines bleow and use a valid email address and password.
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1 ‘basic (clear-text) authentication
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/sendusername”) =”automation@xxxx.com”
    ObjSendMail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/sendpassword”) =”xxxxx”

    ObjSendMail.Configuration.Fields.Update

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s