2011-04-01 347 views
6

我想知道是否有辦法通過VB6發送電子郵件(SMTP)。我有一個應用程序,只需要在用戶完成後發送一封簡單的電子郵件,以便讓一個小組知道應用程序已經處理完畢。有沒有辦法做到這一點?通過VB6發送電子郵件

+0

另請參閱http://stackoverflow.com/questions/3539242/sending-e-mail-via-smtp-using-vb6 http://stackoverflow.com/questions/5155611/send-emails-through-vb6-如果沒有電子郵件客戶端 – MarkJ 2011-04-02 08:12:18

回答

10

是的 - 取決於哪個版本的Windows您正在使用。假設其中一個更新的版本 - CDO.Message很好。

Sub SendMessage(MailFrom,MailTo,Subject,Message) 
    Dim ObjSendMail 
    Set ObjSendMail = CreateObject("CDO.Message") 

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

    With ObjSendMail.Configuration.Fields 
    .Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network). 
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smpt server Address" 
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False) 
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 

    ' If your server requires outgoing authentication uncomment the lines below and use a valid email address and password. 
' .Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication 
' .Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailFrom 
' .Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourpassword 

    .Update 
    End With 

    'End remote SMTP server configuration section== 

    ObjSendMail.To = MailTo 
    ObjSendMail.Subject = Subject 
    ObjSendMail.From = MailFrom 

    ' we are sending a html email.. simply switch the comments around to send a text email instead 
    ObjSendMail.HTMLBody = Message 
    'ObjSendMail.TextBody = Message 

    ObjSendMail.Send 

    Set ObjSendMail = Nothing 
End Sub 
+2

通過設置對庫的引用,您還可以獲得這些字段名稱和幻數的命名常量。 – Bob77 2011-04-01 19:45:45

3

我發現這個here

Dim UserName$, UserMail$, MailRecipiant$, MailBody$, SockData$ 

Private Sub Command1_Click() 
UserName = "YourUserName_or_Addr" 
UserMail = "Your Name <[email protected]>" 
MailRecipiant = UserMail 
MailBody = "The message goes here" 
Winsock1.LocalPort = 0 
Winsock1.RemoteHost = "smtp-server" 
Winsock1.RemotePort = 25 
Winsock1.Connect 
End Sub 

Private Sub Winsock1_Connect() 
Label1 = "Sending message..." 
Winsock1.SendData "EHLO " & UserName & vbCrLf 
If Not WaitFor("250") Then GoTo 100 
Winsock1.SendData "MAIL FROM: " & UserMail & vbCrLf 
If Not WaitFor("250") Then GoTo 100 
Winsock1.SendData "RCPT TO: " & MailRecipiant & vbCrLf 
If Not WaitFor("250") Then GoTo 100 
Winsock1.SendData "DATA" & vbCrLf 
If Not WaitFor("354") Then GoTo 100 
Winsock1.SendData MailBody & vbCrLf & "." & vbCrLf 
If Not WaitFor("250") Then GoTo 100 
Winsock1.SendData "QUIT" & vbCrLf 
If Not WaitFor("221") Then GoTo 100 
Label1 = "Message sent" 
GoTo 200 
100 
Label1 = SockData 
200 
Winsock1.Close 
End Sub 

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) 
Winsock1.GetData SockData 
End Sub 

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 
Label1 = "Error: " & Description 
SockData = "Error" 
Winsock1.Close 
End Sub 

Private Function WaitFor(SockResponse As String) As Boolean 
Do While Left(SockData, 3) <> SockResponse And Left(SockData, 3) <> "220" And Left(SockData, 3) <> "250" 
    DoEvents 
    If Left(SockData, 3) > "400" Then Exit Function 
Loop 
WaitFor = 1 
SockData = "" 
End Function 
+0

爲什麼擊落投票? – SQLMason 2011-04-01 18:35:59

+0

我測試了它,它在VB 6.0中工作... – SQLMason 2011-04-01 18:45:24

+0

也許因爲如果您需要加密身份驗證或SSL/TLS傳輸,此方法會失敗?這些都是相當普遍的要求了。附件和HTML郵件也爲這種「自己動手」的方式增加了更多的麻煩。儘管如此,在有限的情況下仍然很棒 – Bob77 2011-04-01 19:48:35

0

戴夫有一個很好的解決方案,如果你真的需要從客戶端的PC發送電子郵件。但是,有時您會遇到防火牆等問題。在連接到SQL Server的情況下,如果通過SQL Server代理郵件(通過將郵件排隊到發送郵件表中,或者調用xp_sendmail存儲過程本身),我發現它更簡單,更容易管理。

這是關於如何獲取SQL Mail設置和在服務器上工作的tutorial,並且最後它展示瞭如何使用存儲過程發送電子郵件。

我發現這個解決方案是有利的,因爲:

  • Windows 7的電腦被封鎖所有出站SMTP
  • 實現所有的重試次數和這樣做的出站電子郵件「右」是相當複雜
  • 使用與SQL Server,但在我的開發或測試數據庫實際上沒有設置SQL Mail的隊列方法,該電子郵件停留在隊列中,除非我在生產服務器上運行