Skip to content

Instantly share code, notes, and snippets.

@hongyangqin
Created January 5, 2018 15:37
Show Gist options
  • Save hongyangqin/7c8496d8d8aaf73fa29865fa18731717 to your computer and use it in GitHub Desktop.
Save hongyangqin/7c8496d8d8aaf73fa29865fa18731717 to your computer and use it in GitHub Desktop.
C++ Socket 发送邮件 winsock

C++ Socket 发送邮件

from : http://www.news2news.com/vfp/?example=385&ver=vcpp

// Yuri Zarechny for News2News (c) 2006

#include "stdafx.h"
//#include < windows.h >
#include < winsock2.h >  //link ws2_32.lib
#include "conio.h"

struct _sendmail {
    LPSTR host;
    LPSTR IP;
    LPSTR sender;
    LPSTR recip;
    LPSTR subject;
    LPSTR body;
    SOCKET hSocket;
} sendmail;

BOOL InitSock(LPSTR Server)
{
    WSADATA wsd;
    if (WSAStartup(MAKEWORD(2,2), &wsd) > 0)
    {
        printf("Winsock init error: %d\n", WSAGetLastError());
        return false;
    }
    return true;
}

LPSTR GetIP(LPSTR h)
{
    in_addr ia;
    hostent* hbn;
    unsigned int addr;

    if (isalpha(h[0])) hbn = gethostbyname(h);
    else
    {
        addr = inet_addr(h);
        hbn = gethostbyaddr((char *)&addr, 4, AF_INET);
    }

    if (hbn==0) return "";
    ia.S_un.S_addr=*(DWORD *)hbn->h_addr_list[0];
    return inet_ntoa(ia);
}

BOOL ValidateEnvelope(LPSTR h, LPSTR r, LPSTR s, LPSTR *ip)
{
    if (h=="" || r=="" || s=="")
    {
        printf("Invalid host or sender/recipient email address\n");
        return false;
    }
    *ip=GetIP(h);
    if (*ip=="")
    {
        printf("Can not resolve host name %s to ip address\n", h);
        return false;
    }
    return true;
}

BOOL ConnectTo()
{
    const u_short SMTP_PORT=25;
    sockaddr_in sa;
    sa.sin_family=AF_INET;
    sa.sin_addr.S_un.S_addr=inet_addr(sendmail.IP);
    sa.sin_port=htons(SMTP_PORT);
    return (connect(sendmail.hSocket, (SOCKADDR *)&sa, sizeof(sa))==0);
}

BOOL snd(LPSTR txt, bool resp)
{
    char tx[4096];
    sprintf(tx, "%s\r\n", txt);

    if (send(sendmail.hSocket, tx, strlen(tx),0)==SOCKET_ERROR)
    {
        printf("%s%s\n", tx, " - send operation failed.");
        return false;
    }
    if (!resp) return true;

    while (true)
    {
        char rcv[1024];
        char outp[255]="";

        HANDLE hEvent=WSACreateEvent();
        WSAEventSelect(sendmail.hSocket,hEvent,FD_READ);
        DWORD nWait=WSAWaitForMultipleEvents(1, &hEvent, 0, 1000, 0);
        WSACloseEvent(hEvent);
        if (nWait!=0) return false;
        recv(sendmail.hSocket, rcv, sizeof(rcv),0);

        strncpy(outp, rcv, strcspn(rcv, "\n")+1);
        printf("%s\n", outp);
    }
    return true;
}

BOOL SendMail()
{
    if (!ValidateEnvelope(sendmail.host, sendmail.recip,
        sendmail.sender, &sendmail.IP)) return false;

    char tmp[255];
    if (sendmail.sender=="" || sendmail.recip=="" ||
        sendmail.body=="" || sendmail.subject=="") return false;

    sendmail.hSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sendmail.hSocket==SOCKET_ERROR) return false;

    if (!ConnectTo())
    {
        printf("Unable to connect to the server.\n");
        return false;
    }
    snd("HELO", true);

    sprintf(tmp, "%s%s%s", "MAIL FROM:<", sendmail.sender, ">");
    snd(tmp, true);
    sprintf(tmp, "%s%s%s", "RCPT TO:<", sendmail.recip, ">");
    snd(tmp, true);

    snd("DATA", true);
    sprintf(tmp, "%s%s", "From: ", sendmail.sender);
    snd(tmp, false);
    sprintf(tmp, "%s%s", "To: ", sendmail.recip);
    snd(tmp, false);
    sprintf(tmp, "%s%s", "Subject: ", sendmail.subject);
    snd(tmp, false);
    snd("", false);
    snd(sendmail.body ,false);
    snd(".", true);

    snd("NOOP", true);
    snd("QUIT", true);
    closesocket(sendmail.hSocket);

    return true;
}

void _tmain() {
    // provide valid host name and email addresses
    sendmail.host = "smtp.myisp.ca";
    sendmail.sender = "sender@myisp.ca";
    sendmail.recip = "recipient@myisp.ca";

    sendmail.subject = "Testing Winsock SMTP functionality";
    sendmail.body = "Test message:\n\n"
        "Windows Sockets (Winsock) provides a general-purpose networking "
        "application programming interface (API) based on the socket "
        "interface from the University of California at Berkeley.\n";

    if (InitSock(sendmail.host))
    {
        SendMail();
        WSACleanup();
    }

    printf("\nPress ENTER to quit");
    getch();
    return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment