WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples From Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WinSock - SMTP with Sockets

As requested. Here is some simple code to use the Winsock Extender to send mail. This works by basicly telnetting to the server via port 25 (which is what your main client does).

This example only sends one e-mail but you can send as many e-mails as you want between the connect and disconect. But untill you disconect you still have a connection open to the mail server.

Iain.


AddExtender("WWWSK34i.DLL")

;--------------------------------------------------------------------------------;

;smtpConnect : Connects to a SMTP server                                         ;

;--------------------------------------------------------------------------------;

;server_address   : e-mail address as IP or resolveable name                     ;

;port             : port to connect on                                           ;

;local_name       : local server name                                            ;

;                   (some servers require full resolveable domain name)          ;

;--------------------------------------------------------------------------------;

;Returns          : socket handle if succesful , False otherwise                 ;

;--------------------------------------------------------------------------------;

#DefineFunction smtpConnect(server_address,port,local_name)

   max_line = 256

   handle = sOpen ()

   If !sConnect (handle, server_address, port) Then Return @FALSE

   line = sRecvLine (handle, max_line)

   If !smtpCheckReturn(line,220) Then Return StrCat(@FALSE,@TAB,line)

   sSendLine(handle,StrCat("HELO ",local_name))

   line = sRecvLine (handle, max_line)

   If !smtpCheckReturn(line,250) Then Return StrCat(@FALSE,@TAB,line)

   Return handle

#EndFunction



;--------------------------------------------------------------------------------;

;smtpDisconect : Disconect from a SMTP server                                    ;

;--------------------------------------------------------------------------------;

;handle           : handle returned from smtpConnect                             ;

;force            : true to not try a nice disconect                             ;

;--------------------------------------------------------------------------------;

;Returns          : True if succesful , False otherwise                          ;

;--------------------------------------------------------------------------------;

#DefineFunction smtpDisconect(handle,force)

   If !force Then

      max_line = 256

      sSendLine(handle,"QUIT")

      line = sRecvLine (handle, max_line)

      If !smtpCheckReturn(line,221) Then Return StrCat(@FALSE,@TAB,line)

   EndIf

   Return sClose (handle)

#EndFunction



;--------------------------------------------------------------------------------;

;smtpReset : Reset a smtp conversation                                           ;

;--------------------------------------------------------------------------------;

;handle           : handle returned from smtpConnect                             ;

;--------------------------------------------------------------------------------;

;Returns          : True if succesful , False otherwise                          ;

;--------------------------------------------------------------------------------;

#DefineFunction smtpReset(handle)

   max_line = 256

   sSendLine(handle,"REST")

   line = sRecvLine (handle, max_line)

   Return smtpCheckReturn(line,250)

#EndFunction



;--------------------------------------------------------------------------------;

;smtpSendMessage : Send a message to a e-mail server                             ;

;--------------------------------------------------------------------------------;

;handle           : handle returned from smtpConnect                             ;

;to_list          : tab dalimited list of e-mail addresses                       ;

;from_addr        : e-mail address to tell server the message is from            ;

;headers          : string with the e-mail headers ending in @crlf,              ;

;                   This should include:                                         ;

;                   From:                                                        ;

;                   Subject:                                                     ;

;body             : string with the body of the message                          ;

;--------------------------------------------------------------------------------;

;Returns          : True if succesful , False otherwise                          ;

;--------------------------------------------------------------------------------;

#DefineFunction smtpSendMessage(handle,to_list,from_addr,headers,body)

   max_line = 256

   sSendLine(handle,StrCat("MAIL FROM: ",from_addr))

   line = sRecvLine (handle, max_line)

   If !smtpCheckReturn(line,250) Then Return StrCat(@FALSE,@TAB,line)

   For a = 1 To ItemCount(to_list,@TAB)

      sSendLine(handle,StrCat("RCPT TO: ",ItemExtract(a,to_list,@TAB)))

      line = sRecvLine (handle, max_line)

      If !smtpCheckReturn(line,250) Then Return StrCat(@FALSE,@TAB,line)

   Next a

   sSendLine(handle,"DATA")

   line = sRecvLine (handle, max_line)

   If !smtpCheckReturn(line,354) Then Return StrCat(@FALSE,@TAB,line)

   sSendString(handle,headers)

   sSendLine(handle,@CR)

   body = StrReplace(body,StrCat(@CRLF,".",@CRLF),StrCat(@CRLF,"..",@CRLF))

   sSendString(handle,body)

   sSendLine(handle,StrCat(@CRLF,"."))

   line = sRecvLine (handle, max_line)

   Return smtpCheckReturn(line,250)

#EndFunction



;--------------------------------------------------------------------------------;

;smtpCheckReturn : Check returned message for expected error number              ;

;--------------------------------------------------------------------------------;

;line             : string returned from e-mail server                           ;

;number           : expected error number                                        ;

;--------------------------------------------------------------------------------;

;Returns          : True if succesful , False otherwise                          ;

;--------------------------------------------------------------------------------;

#DefineFunction smtpCheckReturn(line,number)

   Return ItemExtract(1,line," ") == number

#EndFunction





; Example

email_server = "emailserver.office.com"

local_name = "mypc.office.com"

subject = "Test Message"

from_txt = "'John Doe' <john@hotmail.com>"

to_txt = "(My friends)"

to_list = StrCat("jane@hotmail.com",@TAB,"joe@hotmail.com")

headers = StrCat("From: ",from_txt,@CRLF,"Subject: ",subject,@CRLF,"To: ",to_txt,@CRLF)

from_addr = "john@hotmail.com"

msg = "test message"

smtp_handle = smtpConnect(email_server,25,local_name)

If smtp_handle Then

   smtpSendMessage(smtp_handle,to_list,from_addr,headers,msg)

   If !smtpDisconect(smtp_handle,@FALSE) Then smtpDisconect(smtp_handle,@TRUE)

EndIf


Article ID:   W15882
File Created: 2004:03:30:15:41:24
Last Updated: 2004:03:30:15:41:24