Question

So I am trying to send an automated message from an Excel VBA backend. I'm very competent in my VBA skills and have used the method below with great success in the past. I am having issues using this method in my new environment and I know it has something to do with the configuration of the Exchange server, the issue is that the guy who manages the exchange server nor I are Exchange experts.

Public Function SendEmail(
    txtTo As String, 
    txtSubject As String, 
    txtBody As String,    
    Optional txtSender As String = vbNullString, 
    Optional txtCC As String = vbNullString, 
    Optional txtBCC As String = vbNullString)

    Dim objMsg As Object, objconfig As Object, objFields As Object

    Set objMsg = CreateObject("CDO.Message")
    Set objconfig = CreateObject("CDO.Configuration")
    Set objFields = objconfig.fields

    'Set the email configuration
    With objFields
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        .Update
    End With

    'Apply the various fields to the email object
    With objMsg
        .Configuration = objconfig
        .From = txtSender
        .Subject = txtSubject
        .To = txtTo
        .Cc = txtCC
        .Bcc = txtBCC
        .TextBody = txtBody         
        .Send
    End With

    SendEmail = IIf(Err.Number = 0, True, False)

    'clean up
    Set objMsg = Nothing
    Set objconfig = Nothing
    Set objFields = Nothing

End Function

I am attempting to send to a distribution group which requires authentication. We've figured out how to remove that "feature" to send an email but this is not ideal. I've tried using basic/clear trust (1) for the authentication method, supplied my username and password to the required fields in the configuration object and still no dice. When I send an email from Outlook I have no issues sending to an email that requires authentication, but when using CDO Exchange won't allow me to authenticate. It also never resolves the sender address, not sure if that is related or not but for the sake of full disclosure there it is. I know that the Exchange/SMTP server is configured to allow for anonymous sending, but that seems to be the only option that CDO will use.

I'm at my wits end. Any Exchange gurus out there that might be able to offer some suggestions?

Thanks, Chad

Was it helpful?

Solution

Well, I finally managed to solve my own issue. If anyone else is having similar issues I thought I'd share. It turned out to be a port issue.

The standard ports for exchange are 25 and 465 for SSL (I believe).

In this instance, port 25 was configured for sending anonymously from behind our firewall (by printers & fax machines mainly). I tried using port 465 but it reported back that the port did not exist.

After much digging on the exchange server I found that some arbitrary port number was used to configure the authenticated messages against. Once I hardcoded the correct port number in the code above, I had no issues getting it to work. Happy coding!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top