B
Brad Pears
I have some sample code on connecting to a mail server using the winsock
control to send an email automatically from an Access 2000 project...
However, I am not getting a connect request back from the mail server at
all...There must be a missing step somewhere...
Does someone have some sample code they could send my way that will allow
the client computer to connect to an SMTP server over port 25 and send an
email?
The code I have seems to work but the .Connect request to the email server
just sits there and never comes back. It seems as a connection is never
established.
Here is the sample code....
(Form declaration code)
Option Compare Database
Dim winsock1 As Winsock
(command button code)
Private Sub cmdSendMail_Click()
Call SMTPSend("mydomain.com", "192.168.2.15", "bradp", "friggin", "This is
the Subject", "This is the body")
End Sub
' Routine to send email
Sub SMTPSend(strMyDomain As String, _
strEmailServer As String, _
strEmailAddressWithoutDomain As String, _
strWhoToSayThisIsFrom As String, _
strSubject As String, _
strMessageBody As String)
Set winsock1 = Me!axWinsockServer.Object
winsock1.Protocol = sckTCPProtocol
winsock1.RemoteHost = strEmailServer
winsock1.RemotePort = 25
winsock1.Connect
' Wait for connection
WaitForIt
Select Case Left$(strWSin, 3)
Case "220"
' connected ok, send HELLO
winsock1.SendData "HELO " & _
strMyDomain & _
vbCrLf
WaitForIt
winsock1.SendData "MAIL FROM: " & _
strWhoToSayThisIsFrom & _
vbCrLf
WaitForIt
winsock1.SendData "RCPT TO: " & _
strWhoToSayThisIsFrom & _
vbCrLf
WaitForIt
winsock1.SendData "DATA" & vbCrLf & _
"DATE:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"FROM: " & _
strWhoToSayThisIsFrom & vbCrLf & _
"TO:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"DATE:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"SUBJECT: " & _
strSubject & _
vbCrLf & _
strMessageBody & _
vbCrLf & _
"." & vbCrLf
' note: . & vbcrlf terminates the "send"
WaitForIt
' parse and validate the return from the sever
End Select
' tell the server you're done:
winsock1.SendData "QUIT" & vbCrLf
' and that's it!
winsock1.Close
End Sub
Private Sub WaitForIt()
WaitingforData = True
While WaitingforData = True
DoEvents
Wend
End Sub
' This should run when a connection has been established - but it never
does....
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Temp As String
Temp = String(bytesTotal, " ")
winsock1.GetData Temp, vbString
Do
If Right$(Temp, 1) = vbLf Then
Temp = Left$(Temp, Len(Temp) - 1)
End If
Loop While Right$(Temp, 1) = vbLf
strWSin = Temp
WaitingforData = False
End Sub
Any help with this would be greatly appreciated!!!
Thanks,
Brad
control to send an email automatically from an Access 2000 project...
However, I am not getting a connect request back from the mail server at
all...There must be a missing step somewhere...
Does someone have some sample code they could send my way that will allow
the client computer to connect to an SMTP server over port 25 and send an
email?
The code I have seems to work but the .Connect request to the email server
just sits there and never comes back. It seems as a connection is never
established.
Here is the sample code....
(Form declaration code)
Option Compare Database
Dim winsock1 As Winsock
(command button code)
Private Sub cmdSendMail_Click()
Call SMTPSend("mydomain.com", "192.168.2.15", "bradp", "friggin", "This is
the Subject", "This is the body")
End Sub
' Routine to send email
Sub SMTPSend(strMyDomain As String, _
strEmailServer As String, _
strEmailAddressWithoutDomain As String, _
strWhoToSayThisIsFrom As String, _
strSubject As String, _
strMessageBody As String)
Set winsock1 = Me!axWinsockServer.Object
winsock1.Protocol = sckTCPProtocol
winsock1.RemoteHost = strEmailServer
winsock1.RemotePort = 25
winsock1.Connect
' Wait for connection
WaitForIt
Select Case Left$(strWSin, 3)
Case "220"
' connected ok, send HELLO
winsock1.SendData "HELO " & _
strMyDomain & _
vbCrLf
WaitForIt
winsock1.SendData "MAIL FROM: " & _
strWhoToSayThisIsFrom & _
vbCrLf
WaitForIt
winsock1.SendData "RCPT TO: " & _
strWhoToSayThisIsFrom & _
vbCrLf
WaitForIt
winsock1.SendData "DATA" & vbCrLf & _
"DATE:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"FROM: " & _
strWhoToSayThisIsFrom & vbCrLf & _
"TO:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"DATE:" & _
Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
"SUBJECT: " & _
strSubject & _
vbCrLf & _
strMessageBody & _
vbCrLf & _
"." & vbCrLf
' note: . & vbcrlf terminates the "send"
WaitForIt
' parse and validate the return from the sever
End Select
' tell the server you're done:
winsock1.SendData "QUIT" & vbCrLf
' and that's it!
winsock1.Close
End Sub
Private Sub WaitForIt()
WaitingforData = True
While WaitingforData = True
DoEvents
Wend
End Sub
' This should run when a connection has been established - but it never
does....
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Temp As String
Temp = String(bytesTotal, " ")
winsock1.GetData Temp, vbString
Do
If Right$(Temp, 1) = vbLf Then
Temp = Left$(Temp, Len(Temp) - 1)
End If
Loop While Right$(Temp, 1) = vbLf
strWSin = Temp
WaitingforData = False
End Sub
Any help with this would be greatly appreciated!!!
Thanks,
Brad