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

SMTP Functions

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

Sending E-mail via Winsock


Here is some UDF's for sending e-mail using the WinSock extender rather than Postie.

These UDF's have a couple of advantages over Postie and a number of disadvantages.

- If you are sending a large number of message to/via one e-mail server then the UDF's can take 1/3 the time as Postie connects and disconects from the server for each message.

- The UDF's give you full control over the message allowing you to do BCC's and set the message Headers.

The attached example shows an e-mail being BCC'ed to 2 addresses. If anyone thinks this could be usefull to them, I could do some more examples.

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' "

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:   W15883
File Created: 2004:03:30:15:41:24
Last Updated: 2004:03:30:15:41:24