0
votes

I have a legacy VB6 codebase which I would like to extend to include support for sending mails through an external SMTP server (smtp.live.com).

I use CDO for sending the mail. My machine runs Windows 7. Unfortunately I get a "The transport failed to connect to the server" error message when trying to send send the mail. Below is the code.

VB6

Dim oNewMessage As CDO.Message
Dim iConf As New CDO.Configuration
Dim oFlds As ADODB.Fields
Dim strbody As String


On Error GoTo errSMPT
    iConf.Load cdoDefaults
    Set oFlds = iConf.Fields
    oFlds(cdoSendUsingMethod) = cdoSendUsingPort
    oFlds(cdoSMTPServer) = "smtp.live.com"
    oFlds(cdoSMTPServerPort) = 587
    oFlds(cdoSMTPConnectionTimeout) = 30
    oFlds(cdoSMTPUseSSL) = True
    oFlds(cdoSMTPAuthenticate) = cdoBasic
    oFlds(cdoSendUserName) = "[email protected]"
    oFlds(cdoSendPassword) = "mypassword"
    oFlds.Update

    strbody = "Sample message " & Time

    Set oNewMessage = New CDO.Message
    Set oNewMessage.Configuration = iConf

    With oNewMessage
        .To = txtTo.Text
        .From = txtFrom.Text
        .Subject = "subject"
        .TextBody = strbody
        .Send
    End With
    Exit Sub

errSMPT:
    MsgBox Err.Description

I don't think that the problem is related to firewall or account security issues since the C# code below works without any problems.

C#

using (MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtText.Text))
                {
                    SmtpClient mailClient = new SmtpClient("smtp.live.com", 587);

                    mailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
                    mailClient.EnableSsl = true;
                    mailClient.Send(message);
                    MessageBox.Show("Message successfully sent!!!");                    
                }

Any help is appreciated!

Thanks

//Peter

1
Do you know if the VB version is using ssl? - Rob
If this can be of any help, your code works fine for me when sending with a Gmail account (smtp.googlemail.com on TCP/465), just as with my own code that uses CDO. However, I've never been able to send an email through CDO with a Live/Hotmail/Outlook.com account (tried again this time, just in case, without success). - johnwait

1 Answers

0
votes

I think your problem is here:

oFlds(cdoSMTPUseSSL) = True

This should be an integer instead of a boolean. When VB6 converts true to and int, the value is -1. I suggest you change that line to:

oFlds(cdoSMTPUseSSL) = 1