| Sending Email w/Winsock |
| Ok,
well let's see... I'll try and describe how the code below works and
hopefully you can go from there. First off, in the declarations you will
notice an Enum statement:
Private Enum SMTP_State Now if your not familiar with enum statements yet, it basically lets you create a new type, which is an integer type variable that lets you specify names for each value. For instance in this one, MAIL_CONNECT would be 0, MAIL_HELO would be 1 and so on. So when SMTP_State = 1 it also = MAIL_HELO. Next in the declarations you see: Private m_State As SMTP_State Which is just declaring m_State as that new enum we created. And then of course we Dim all the string variables to hold all our addresses and message subject and message body etc. The way the whole thing works is when the SendEmail Sub is run, it collects all the message info and then tells winsock to connect to your mail server. ( you have to specify that and save it to the EMLHost variable. ) and connects to it in port 25. The rest is done in the DataArrival Event. When its connected the server will send you a string that starts with a number. If the number is 250, 220,or 354 then basically that's your green light, your go ahead to send the next part. The steps are as follows: Once connected, send the HELO string with your mailbox. If your 'From' Address was general@xi0n.com then the mailbox would be general. Winsock1.SendData "HELO general" & vbCrLf Then once that's accepted send the MAIL FROM string: Winsock1.SendData "MAIL FROM:general@xi0n" & vbCrLf ...once accepted send the RCPT (recipient) TO string: Winsock1.SendData "RCPT TO:support@xi0n.com" & vbCrLf ...once accepted we tell the server its data now: Winsock1.SendData "DATA" & vbCrLf ...and once that's accepted you'll see its just a matter of sending the subject, and then the message line for line. Once that's complete then you send a dot to let the server know its done with the data part. Winsock1.SendData "." & vbCrLf Then just send the QUIT string: Winsock1.SendData "QUIT" & vbCrLf And close your winsock.
|
|
| Declarations |
'//-------------------------------------------------------------------------------- Dim EMLFrom As String
'//-------------------------------------------------------------------------------- |
|
| Code |
'//-------------------------------------------------------------------------------- Private Sub SendEmail() 'save
the Subject 'now
connect to the STMP host on port 25 '//-------------------------------------------------------------------------------- '//-------------------------------------------------------------------------------- Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) |
|