Sending an email through VB6

后端 未结 4 1481
广开言路
广开言路 2021-01-23 04:18

I am wondering if there is a way to send an email (SMTP) through VB6. I have an application that just needs to send a simple email when the user is done to let a group know that

4条回答
  •  梦毁少年i
    2021-01-23 04:39

    I found this here:

    Dim UserName$, UserMail$, MailRecipiant$, MailBody$, SockData$
    
    Private Sub Command1_Click()
    UserName = "YourUserName_or_Addr"
    UserMail = "Your Name "
    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
    

提交回复
热议问题