2015-10-17 82 views
4

我試圖使用CSMTP類 http://www.codeproject.com/Articles/98355/SMTP-Client-with-SSL-TLSC++使用CSMTP類庫

這些代碼行發送郵件發送郵件在這裏造成損害:

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_addrgethostbyname不工作了。

'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 

我怎麼能得到這個使用inet_pton()getaddrinfo()工作? 我試圖找到解決方案,但迄今沒有發現任何東西......

謝謝!

回答

0

我相信它應該是這樣的:

#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); 
    }    
} 

基於:

的Windows:

的Linux: