2015-04-24 87 views
0

我有一個傳統的VB6代碼庫,我想擴展到包括支持通過外部SMTP服務器(smtp.live.com)發送郵件。無法通過SMTP發送郵件從VB6程序

我使用CDO發送郵件。我的機器運行Windows 7. 不幸的是,當嘗試發送郵件時,出現「傳輸無法連接到服務器」錯誤消息。 以下是代碼。

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 

我不認爲這個問題是防火牆或帳戶安全問題相關的,因爲下面的C#代碼工作沒有任何問題。

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!!!");      
       } 

任何幫助表示讚賞!

感謝

//彼得

+0

你知道VB版本是否使用ssl? – Rob

+0

如果這可以提供任何幫助,那麼在使用Gmail帳戶(TCP/465上的smtp.googlemail.com)發送郵件時,您的代碼就可以正常工作,就像使用我自己的使用CDO的代碼一樣。但是,我從來沒有能夠通過CDO發送帶有Live/Hotmail/Outlook.com帳戶的電子郵件(這次再試一次,以防萬一)。 – johnwait

回答

0

我覺得你的問題是在這裏:

oFlds(cdoSMTPUseSSL) = True 

這應該是一個整數,而不是一個布爾值。當VB6轉換爲和int時,該值爲-1。我建議你將該行改爲:

oFlds(cdoSMTPUseSSL) = 1 
+0

該字段確實是'布爾' – Bob77

+0

謝謝,但結果是相同的,無論我將其設置爲True還是1。 –