C ++ Sending mail using the CSmtp library

I am trying to send an email using CSmtp http://www.codeproject.com/Articles/98355/SMTP-Client-with-SSL-TLS

These lines of code are harmful here:

if((sockAddr.sin_addr.s_addr = inet_addr(szServer)) == INADDR_NONE)
{
    LPHOSTENT host;

    host = gethostbyname(szServer);
    if (host)
        memcpy(&sockAddr.sin_addr,host->h_addr_list[0],host->h_length);
    else
    {
#ifdef LINUX
        close(hSocket);
#else
        closesocket(hSocket);
#endif
        throw ECSmtp(ECSmtp::WSA_GETHOSTBY_NAME_ADDR);
    }               
}

inet_addrand gethostbynameno longer works.

'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings

'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings

How can I make this work with inet_pton()and getaddrinfo()? I tried to find a solution, but havent found anything else so far ...

Thanks!

+4
source share
1 answer

I believe this should be something like this:

#ifdef LINUX

   int retVal = inet_pton(AF_INET, czServer,  &sockAddr.sin_addr);
#else
   int retVal = InetPton(AF_INET, czServer,  &sockAddr.sin_addr);

#endif

if(retVal != 1)
{

    struct addrinfo *result = NULL;
    struct addrinfo hints;

    #ifdef LINUX
        memset(&hints, 0, sizeof(struct addrinfo));
    #else
        ZeroMemory( &hints, sizeof(hints) );
    #endif


    hints.ai_flags = AI_NUMERICHOST;
    hints.ai_family = AF_UNSPEC;


    getaddrinfo(argv[1], NULL, &hints, &result);

    if (host)
        memcpy(&sockAddr.sin_addr,result->ai_addr,result->ai_addrlen);
        freeaddrinfo(result);
    else
    {
#ifdef LINUX
        close(hSocket);
#else
        closesocket(hSocket);
#endif
        throw ECSmtp(ECSmtp::WSA_GETHOSTBY_NAME_ADDR);
    }               
}

Based:

Window:

Linux:

0

Source: https://habr.com/ru/post/1612034/


All Articles