` Printed Icetips Article

Icetips Article



Par2: SMTP Email with Messaging templates
2009-12-08 -- Ron Schofield
 
The code for the messaging templates is bad. Too bad SV didn't clean 
the code up to make it work. I ran through the same problems until I 
rewote the code just to do SMTP. All I did was use code from codeproject 
and converted it to Clarion.

http://beta.codeproject.com/KB/IP/zsmtp.aspx

SOCKET CFastSmtp::_connectServerSocket(const char server[],
                                        const unsigned short port)
{
     int                nConnect;
     short            nProtocolPort;
     LPHOSTENT        lpHostEnt;
     LPSERVENT        lpServEnt;
     SOCKADDR_IN        sockAddr;

     SOCKET            hServerSocket = INVALID_SOCKET;

     lpHostEnt = gethostbyname(server);
     if (lpHostEnt) {
         hServerSocket = socket(PF_INET, SOCK_STREAM,DEFAULT_PROTOCOL);
         if (hServerSocket != INVALID_SOCKET) {
             if (port != NULL) {
                 nProtocolPort = port;
             }else{
                 lpServEnt = getservbyname("mail", DEFAULT_PROTOCOL);
                 if (lpServEnt == NULL)
                     nProtocolPort = htons(IPPORT_SMTP);
                 else
                     nProtocolPort = lpServEnt->s_port;
             }
             sockAddr.sin_family = AF_INET;
             sockAddr.sin_port = nProtocolPort;
             sockAddr.sin_addr = *((LPIN_ADDR)*lpHostEnt->h_addr_list);
             nConnect = connect(hServerSocket, (PSOCKADDR)&sockAddr,
                                sizeof(sockAddr));
             if(nConnect)
                 hServerSocket = INVALID_SOCKET;
         } else {
             printf("Invalid socket\r\n");
             throw;
         }
     }
     return(hServerSocket);
}


gets converted to

SMTPServerSocket PROCEDURE(*CSTRING ServerName, SHORT Port)

hServerSocket               SOCKET

lpHostEnt                   LONG
HostEntry                   LIKE(HOSTENT)
lpServEnt                   LONG
ServiceEntry                LIKE(SERVENT)
nProtocolPort               SHORT
SockAddrIn                  LIKE(SOCKADDR_IN)
ServiceName                 CSTRING('mail')
nConnect                    LONG

lpAddrList                  LONG

   CODE

   hServerSocket = INVALID_SOCKET
   lpHostEnt = GetHostByName(ServerName)
   IF lpHostEnt
     hServerSocket = CreateSocket(PF_INET,SOCK_STREAM,DEFAULT_PROTOCOL)
     IF hServerSocket <> INVALID_SOCKET
       IF Port <> 0
         nProtocolPort = Htons(Port)
       ELSE
         lpServEnt = GetServByName(ServiceName,DEFAULT_PROTOCOL)
         IF lpServEnt = 0
           nProtocolPort = Htons(IPPORT_SMTP)
         ELSE
           memcpy(ADDRESS(ServiceEntry),lpServEnt,SIZE(ServiceEntry));
           nProtocolPort = ServiceEntry.s_port
         END
       END
       memcpy(ADDRESS(HostEntry),lpHostEnt,SIZE(HostEntry));
       memcpy(ADDRESS(lpAddrList),HostEntry.h_addr_list,HostEntry.h_length)
       SockAddrIn.sin_family = PF_INET
       SockAddrIn.sin_port = nProtocolPort
       SockAddrIn.sin_addr = SMTPData.SMTPIP
       memcpy(ADDRESS(SockAddrIn.sin_addr),lpAddrList,HostEntry.h_length)
       nConnect = 
WinsockConnect(hServerSocket,ADDRESS(SockAddrIn),SIZE(SockAddrIn))
       IF nConnect = SOCKET_ERROR
         Alog.log('Connect returned ' & WSAGetLastError() & ' 
error.',LOG:Error)
         hServerSocket = INVALID_SOCKET
       END
     ELSE
       Alog.log('Invalid Socket',LOG:Error)
     END
   END

   RETURN(hServerSocket)

Hopefully this will get you started. SMTP isn't a big bad boogy man when 
you get the idea how it works.



Printed November 21, 2024, 11:50 am
This article has been viewed/printed 35278 times.