Software & Finance





Visual C++ - Socket Programming - Simple Email Client Application





I have come up with a simple Email client application. GUI is designed with MFC dialog based application and the sending email is established through socket programming by implementing SMTP Client Protocol.

The complete source code and executable linkes are given at the bottom of this page

 


Source Code


bool SendEmail(std::string smtpdomain, std::string fromname, std::string  fromdomain, std::string toname, std::string todomain, std::string subject, std::string message)

{

    SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if(s == -1)

    {

        AfxMessageBox("Socket Initialiation Error");

        return false;

    }

    SOCKADDR_IN serveraddr;

    struct hostent *hostentry;

 

    int portno = 25;

    bool bSent = false;

   

    hostentry = gethostbyname(smtpdomain.c_str());

    char *pipaddr = inet_ntoa (*(struct in_addr *)*hostentry->h_addr_list);

 

 

     memset(&serveraddr,0, sizeof(serveraddr));

     serveraddr.sin_family = AF_INET;

     serveraddr.sin_port = htons(portno);

     serveraddr.sin_addr.s_addr = inet_addr(pipaddr);

 

    //serv_addr.sa_data = htons(portno);

 

    if (connect(s,(SOCKADDR*)&serveraddr,sizeof(SOCKADDR_IN)) < 0)

    {

         AfxMessageBox("ERROR connecting to the server");

         exit(1);

    }

 

    char sbuf[1024], rbuf[1024];

   

    if(recv(s, rbuf, 1024, 0) > 0)

    {

        if(strnicmp(rbuf, "220", 3) == 0)

        {

            strcpy(sbuf, "helo kathir\r\n");

            if(send(s, sbuf, strlen(sbuf), 0) == strlen(sbuf))

            {

                recv(s, rbuf, 1024, 0);

                if(strnicmp(rbuf, "250", 3) == 0)

                {

                    sprintf(sbuf, "mail from: <%s@%s>\r\n", fromname.c_str(), fromdomain.c_str());

                    send(s, sbuf, strlen(sbuf), 0);

                    recv(s, rbuf, 1024, 0);

                    if(strnicmp(rbuf, "250", 3) == 0)

                    {

                        sprintf(sbuf, "rcpt to: <%s@%s>\r\n", toname.c_str(), todomain.c_str());

                        send(s, sbuf, strlen(sbuf), 0);

                        recv(s, rbuf, 1024, 0);

                        if(strnicmp(rbuf, "250", 3) == 0)

                        {

                            strcpy(sbuf, "data\r\n");

                            send(s, sbuf, strlen(sbuf), 0);

                            recv(s, rbuf, 1024, 0);

                            if(strnicmp(rbuf, "354", 3) == 0)

                            {

                                sprintf(sbuf, "from: %s@%s\r\nto:%s@%s\r\nsubject:%s\r\n\r\n%s\r\n.\r\n", fromname.c_str(), fromdomain.c_str(), toname.c_str(), todomain.c_str(), subject.c_str(), message.c_str());

                                send(s, sbuf, strlen(sbuf), 0);

                                recv(s, rbuf, 1024, 0);

                                if(strnicmp(rbuf, "250", 3) == 0)

                                {

                                    bSent = true;  

                                }

                            }

                        }

                    }

                }

            }

        }

    }

 

    if(bSent == false)

        AfxMessageBox(rbuf);

    ::closesocket(s);

}


Click here to download the Visual C++ source code and executable file.

 

Output