Smtp Engine

Rapid explanation with simple sample code. 

    Sending email message using SMTP protocol is not that hard but the problem I faced that I usually do my codes in win32asm(masm32) while today I tried vc++ ,which I quit for long time ago.

    So lets see what do we need to send an e-mail through MX server(MX=Mail exchange),I will take yahoo servers as an example ,for other MX servers you should resolve it by yourself[see at the end of article] .                                  First we need to connect to MX server through port 25,and then use these commands( an telnet sample):

     as you see we used helo,mail from,rcpt to,data,(dot) ,and the server answers accordingly see rfc821.I didnt proceed in the above example to the whole message because it needs some sort of headers,you can check headers of mail message by opening .eml file created simple for example by outlook.

 

Now see the following code:

--------------------------CODE----------------------------------------

#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>

void main()
{
    char buffer_recv [256];
    DWORD socket_;
    struct sockaddr_in sock_addr;
    WSADATA ws;
    char buffer_send[256];
    int leng ;
    char mail_msg_1 [] = "MIME-Version: 1.0\nContent-Type: multipart/mixed;\n";
    char mail_msg_2 [] = "    boundary=\"----=_NextPart_000_0006_01C54F98.91D69670\"\n";
    char mail_msg_3 [] = "X-Priority: 3\nX-MSMail-Priority: Normal\n";
    char mail_msg_4 [] = "XX-Unsent: 1\n\nThis is a multi-part message in MIME format.\n\n";
    char mail_msg_5 [] = "------=_NextPart_000_0006_01C54F98.91D69670\n";
    char mail_msg_6 [] = "Content-Type: text/plain;\n    format=flowed;\n";
    char mail_msg_7 [] = "Content-Transfer-Encoding: 7bit\n\n\nhello\n";
    char hello_msg  [] =  "helo oops.com\n";
    char mail_from  [] =  "mail from:<asdf@ert.com>\n";
    char rcpt_to    [] =  "rcpt to:<person-recpient-email@yahoo.com>\n";/*change acording to your recpient*/
    char data_        [] =    "DATA\n";
    char dot_        [] =    "\r\n.\r\n";
    char subject_    [] =    "Subject:hello my friend\n";
    char from_        [] =    "From:\"rawbinsoft@somewhere.com\"\n";

    if (WSAStartup(2,&ws)!=0)
    {
        printf("failed intiation wsastartuperror message\n\n");
            return 0;
    }

    socket_=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (socket_ ==INVALID_SOCKET)
    {
        printf("failed intiation wsastartuperror message\n\n");
        return 0;
    }
   


sock_addr.sin_family=AF_INET;
    sock_addr.sin_port=htons(25);
    sock_addr.sin_addr.S_un.S_addr = inet_addr("67.28.113.71"); /*mx1.mail.yahoo.com*/
   
    if(connect(socket_,(struct sockaddr_in*)&sock_addr,sizeof sock_addr)!=0)
    {
        printf("unable to establish connection\n\n");
            return 0;
    }

    recv(socket_,&buffer_recv,256,0);
    printf(&buffer_recv);

    send(socket_,&hello_msg,lstrlen (hello_msg),0);
    recv(socket_,&buffer_recv,256,0);
    printf(&buffer_recv);
   
    send(socket_,&mail_from,lstrlen (mail_from),0);
    recv(socket_,&buffer_recv,256,0);
    printf(&buffer_recv);

    send(socket_,&rcpt_to,lstrlen(rcpt_to),0);
    recv(socket_,&buffer_recv,256,0);
    printf(&buffer_recv);
   
    send(socket_,&data_,lstrlen(data_),0);
    recv(socket_,&buffer_recv,256,0);
    printf(buffer_recv);
   
   
    send(socket_,&subject_,lstrlen(subject_),0);
    send(socket_,&from_,lstrlen(from_),0);
    send(socket_,&mail_msg_1,lstrlen( mail_msg_1),0);
    send(socket_,&mail_msg_2,lstrlen(mail_msg_2),0);
    send(socket_,&mail_msg_3,lstrlen(mail_msg_3),0);
    send(socket_,&mail_msg_4,lstrlen(mail_msg_4),0);
    send(socket_,&mail_msg_5,lstrlen(mail_msg_5),0);
    send(socket_,&mail_msg_6,lstrlen(mail_msg_6),0);
    send(socket_,&mail_msg_7,lstrlen(mail_msg_7),0);
    send(socket_,&dot_,lstrlen(dot_),0);
    recv(socket_,&buffer_recv,256,0);
    printf(&buffer_recv);
    MessageBox(0,"end of smtp-connection","RawBinSoft",0);
    WSACleanup();


    return 0;

    }
   }

-------------------END OF  CODE----------------------------------------



     The above code is to be compiled under vc++.And there are some changes to be done inorder to get this code work properly like mail from and rcpt to parts.Notice that this code could be used to forge emails easily,but note also that some mail service providers do put these kind of forged mails into Bulk.   to get Mail eXchange servers you can use nslookup or check out dnsstuff.com inorder getting these kind of servers .For sending attachments I might discuss it in near future.

Note:for any feed back, questions,&critics feel free contacting me.