Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WinSock SMTP login

Status
Not open for further replies.

tlhawkins

Programmer
Dec 28, 2000
797
US

Has anyone used WinSock to send Email?

I found a page that told me how, but it doesn't show how to send login/password information and my SMTP server requires login.

Anyone Know How?

This is how I connect:
Code:
With frmMain.WinSock1
    If .State = sckClosed Then ' Check to see if socket is closed
        DateNow = Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & "" & " -0600"
        first = "mail from: " & FromEmailAddress & vbCrLf ' Get who's sending E-Mail address
        Second = "rcpt to: " & ToEmailAddress & vbCrLf ' Get who mail is going to
        Third = "Date: " & DateNow & vbCrLf ' Date when being sent
        Fourth = &quot;From: &quot;&quot;&quot; & FromName & &quot;&quot;&quot; <&quot; & FromEmailAddress & &quot;>&quot; + vbCrLf ' Who's Sending
        Fifth = &quot;To: &quot; & ToNametxt & vbCrLf ' Who it going to
        Sixth = &quot;Subject: &quot; & EmailSubject & vbCrLf ' Subject of E-Mail
        Seventh = EmailBodyOfMessage & vbCrLf ' E-mail message body
        Ninth = &quot;X-Mailer: Web Alarm&quot; & vbCrLf ' What program sent the e-mail, customize this
        .LocalPort = 0 ' Must set local port to 0 (Zero) or you can only send 1 e-mail per program start
        .protocol = sckTCPProtocol ' Set protocol for sending
        .RemoteHost = MailServerName ' Set the server address
        .RemotePort = 25 ' Set the SMTP Port
        .Connect ' Start connection
        WaitFor (&quot;220&quot;)
        frmMain.Caption = &quot;Connecting....&quot;
        .SendData (&quot;HELO EnterComputerNameHere&quot; & vbCrLf)
        WaitFor (&quot;250&quot;)
        frmMain.Caption = &quot;Connected&quot;

        .SendData (first)
        frmMain.Caption = &quot;Sending Message&quot;

        WaitFor (&quot;250&quot;)
        .SendData (Second)
        WaitFor (&quot;250&quot;)
        .SendData (&quot;data&quot; & vbCrLf)
        WaitFor (&quot;354&quot;)
        .SendData (Fourth & Third & Ninth & Fifth & Sixth & vbCrLf)
        .SendData (Seventh & vbCrLf)
        .SendData (&quot;.&quot; & vbCrLf)
        WaitFor (&quot;250&quot;)
        .SendData (&quot;quit&quot; & vbCrLf)
        frmMain.Caption = &quot;Disconnecting&quot;

        WaitFor (&quot;221&quot;)
        .Close
    Else
        MsgBox (Str(.State))
    End If
End With
The waitfor sub looks like this:
Code:
Public Sub WaitFor(ResponseCode As String)
    Start = frmMain.Timer2 ' Time event so won't get stuck in loop
    While Len(Response) = 0
        Tmr = Start - frmMain.Timer2
        DoEvents ' Let System keep checking for incoming response **IMPORTANT**
        If Tmr > 50 Then ' Time in seconds to wait
            MsgBox &quot;SMTP service error, timed out while waiting for response&quot;, 64, MsgTitle
            Exit Sub
        End If
    Wend
    While Left(Response, 3) <> ResponseCode
        DoEvents
        If Tmr > 50 Then
           MsgBox &quot;SMTP service error, impromper response code. Code should have been: &quot; + ResponseCode + &quot; Code recieved: &quot; + Response, 64, MsgTitle
           Exit Sub
        End If
    Wend
    Response = &quot;&quot; ' Sent response code to blank **IMPORTANT**
End Sub

The process always Hangs, I'm thinking it is because I'm not logging into the server.

any ideas?
 
.RemoteHost = MailServerName ' Set the server address

You need to assign the SMTP server name or IP into MailServerName. Besides, you need to assign all the required values to FromEmailAdress and also ToEmailAdress.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top