Click here to Skip to main content
11,640,866 members (62,418 online)
Click here to Skip to main content

SMTP Client

, 16 Jul 2010 CPOL 262.9K 12.9K 175
Rate this:
Please Sign up or sign in to vote.
The CSmtp class allows to send emails with attachments. It only provides the AUTH LOGIN authentication.

Introduction

The CSmtp class allows to send emails from your program. The inspiration to write the CSmtp class was the article: CFastSmtp - A fast and easy SMTP class... I have used the code of CFastSmtp and introduced the following changes:

  • some bags have been removed (i.e., free memory)
  • logging in with authentication has been applied (AUTH LOGIN)
  • sending attachments has been added
  • error handling has been modified
  • new headlines from MIME specifications (i.e., X-Priority) have been added
  • non-blocking mode has been added
  • exceptions have been used
  • compatibility with Linux systems has been ensured

Typical scenarios while sending emails

After successful connection to an SMTP server, our client starts the conversation with the remote SMTP server. Each line sent by the client ought to be finished by "\r\n". If you want to know more details, check the References: [2], [3], [4], [5], [6], [7], [8], and [9]. In [2] is described the original SMTP protocol (1982), in [4] is discussed the SMTP extensions for authentication, and the MIME specification is improved in [5]-[9]. Below there are shown typical scenarios while sending e-mails. Example 3 fails because no TLS procedures were implemented in the CSmtp class. If you want to add TLS, see OpenSSL. I have introduced the following notation: S is a remote server, C is our client, and xxx means information censured.

Example 1 - Connecting to smtp.wp.pl and using an incorrect login or password:

S: 220 smtp.wp.pl ESMTP
C: EHLO: mydomain.com
S: 250-smtp.wp.pl
   250-PIPELINING
   250-AUTH=LOGIN PLAIN
   250-AUTH LOGIN PLAIN
   250-STARTTLS
   250-SIZE
   250-X-RCPTLIMIT 100
   250-8BITMIME
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: Kioq
S: 334 UGFzc3dvcmQ6
C: Kioq
S: 535 blad autoryzacji, niepoprawny login lub haslo / auth failure

Example 2 - Connecting to smtp.wp.pl and using the correct login and password:

S: 220 smtp.wp.pl ESMTP
C: EHLO: mydomain.com
S: 250-smtp.wp.pl
   250-PIPELINING
   250-AUTH=LOGIN PLAIN
   250-AUTH LOGIN PLAIN
   250-STARTTLS
   250-SIZE
   250-X-RCPTLIMIT 100
   250-8BITMIME
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: xxx
S: 334 UGFzc3dvcmQ6
C: xxx
S: 235 go ahead
C: MAIL FROM:<me@mydomain.com>
S: 250 ok
C: RCPR TO:<friend@domain.com>
S: 250 ok
C: DATA
S: 234 go ahead
C: Date: Sun, 24 Aug 2008 22:43:45
   From: JP<mail@domain.com>
   X-Mailer: The Bat! (v3.02) Professional
   Replay-to:mail@domain.com
   X-Priority: 3 (Normal)
   To:<friend@domain.com>
   Subject: The message
   MIME Version 1.0
   Content-Type: multipart/mixed; boundary="__MESSAGE__ID__54yg6f6h6y456345"

   
   --__MESSAGE__ID__54yg6f6h6y456345
   Content-type: text/plain; charset=US-ASCII
   Content-Transfer-Encoding: 7bit

   This is my message.

   --__MESSAGE__ID__54yg6f6h6y456345
   Content-Type: application/x-msdownload; name="test.exe"
   Content-Transfer-Encoding: base64
   Content-Disposition: attachment; filename="test.exe"
   
   TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
   (...)
   SU5HWFhQQURESU5HUEFERElOR1hYUEFERElOR1BBRERJTkdYWFBBRERJTkdQQURESU5HWA==
   --__MESSAGE__ID__54yg6f6h6y456345
   Content-Type: application/x-msdownload; name="test2.jpg"
   Content-Transfer-Encoding: base64
   Content-Disposition: attachment; filename="test2.jpg"
   
   /9j/4Sv+RXhpZgAASUkqAAgAAAAJAA8BAgAGAAAAegAAABABAgAWAAAAgAAAABIBAwABAAAA
   (...)
   A6YxR5YJJ5zUu6ZW4+NjC24E4q5Dcox5I+lRI0iWAAV9aay+lTctoYTjrml+9irRmz//2Q==
   
   --__MESSAGE__ID__54yg6f6h6y456345--
   
   .
   
S: 250 ok xxx qp xxx
C: QUIT
S: 221 smtp.wp.pl

Example 3 - Connecting to smtp.gmail.com:

S: 220 mx.google.com ESMTP
   w28sm1561195uge.4
C: EHLO: mydomain.com
S: 250-mx.google.com at your service [xxx.xxx.xxx.xxx],
   250-SIZE 28311552
   250-8BITMIME
   250-STARTTLS
   250 ENHANCEDSTATUSCODES
C: AUTH LOGIN
S: 530 5.7.0 Must issue a STARTTLS command first. w28sm1561195uge.4

Example 4 - Connecting to smtp.bizmail.yahoo.com and using an incorrect login or password:

S: 220 smtp103.biz.mail.re2.yahoo.com ESMTP
C: EHLO: mydomain.com
S: 250-smtp103.biz.mail.re2.yahoo.com
   250-AUTH LOGIN PLAIN XYMCOOKIE
   250-PIPELINING
   250-8BITMIME
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: dG9t
S: 334 UGFzc3dvcmQ6
C: bmVyb24xMg==
S: 535 authorization failed (#5.7.0)

Implementation of the CSmtp class

Implementation of the CSmtp class is very similar for Windows and Linux OSs. There is nothing surprising in this, because Windows uses the generally accepted Berkeley sockets application programming interface (API) [10]. The differences are shown in Table 1 (applies only to the CSmtp class implementation).

Table 1. Differences in implementation.
Windows Linux
Winsock initialization needed No Winsock initialization
Uses function closesocket Uses function close
Uses function ioctlsocket Uses function ioctl
Defined helpful type aliases; i.e., SOCKET, SOCKADDR_IN, LPHOSTENT, LPSERVENT, LPIN_ADDR, LPSOCKADDR Additional types aliase should be defined

Below there are shown steps to be taken when connecting to a remote SMTP server.

  1. In Windows only, initialize Winsock2 (function: WSAStartup).
  2. Get socket descriptor on the local machine (function: socket).
  3. Convert port value (i.e., 25) to TCP/IP byte order (function: htons).
  4. Obtain Internet address of the remote machine (functions: inet_addr, gethostbyname).
  5. If non-blocking mode is used, set socket parameters (function: ioctl/ioctlsocket). Check necessarily what returns each function which will be called after ioctl/ioctlsocket (see next section - Using non-blocking mode).
  6. Connect to the remote server (function: connect).
  7. Introduce yourself - EHLO <SP> <domain> <CRLF>.
  8. Send AUTH LOGIN <CRLF> and another command described in the section "Typical scenarios while sending the email" (functions: send, recv).
  9. Finish the conversation with QUIT <CRLF>.
  10. Close connection with remote machine (function: close/closesocket).
  11. In Windows only, free Winsock2 resources (function: WSACleanup).

Using non-blocking mode

In the latest version of the program, I have used a non-blocking connection. There are many strategies to implement the non-blocking mode (i.e., Select model, WSAAsyncSelect model, WSAEventSelect model, or Completion port I/O model). In my code, I have decided to use the Select model. It is not so complicated as other methods, and works efficiently with a basic connection - one client to one server. The advantages of using non-blocking mode are: the program does not suspend if the remote server stops responding, data can be sent in uneven and unequal portions. Disadvantage of this approach is its complexity. After placing the socket in non-blocking mode, the next API calls are immediately closed. Typically, these calls fail with a an error WSAEWOULDBLOCK (Windows) or EINPROGRESS (Linux), which means that the requested operation is not completed so far. Therefore, in non-blocking mode, a lot of attention should be devoted to analyze errors returned by the API functions. In Select model, we are using the select function [11] after calling such API functions as: send, recv, connect, accept, and others. The parameter ndfs in select is ignored in Windows, but in Linux, it is the highest-numbered file descriptor in any of the three sets (fd_set *readfds, fd_set *writefds, fd_set *exceptfds) plus 1. To illustrate the difference between blocking and non-blocking modes, presented here are two ways of connecting to the remote server. For greater legibility, I have only presented versions for Windows (preprocessor directives were omitted).

/*Connecting to the remote server in blocking mode*/
SOCKET CSmtp::ConnectRemoteServer(const char *szServer,const unsigned short nPort_)
{
    unsigned short nPort = 0;
    LPSERVENT lpServEnt;
    SOCKADDR_IN sockAddr;
    unsigned long ul = 1;
    int res = 0;

    SOCKET hSocket = INVALID_SOCKET;

    if((hSocket = socket(PF_INET, SOCK_STREAM,0)) == INVALID_SOCKET)
        throw ECSmtp(ECSmtp::WSA_INVALID_SOCKET);

    if(nPort_ != 0)
        nPort = htons(nPort_);
    else
    {
        lpServEnt = getservbyname("mail", 0);
        if (lpServEnt == NULL)
            nPort = htons(25);
        else 
            nPort = lpServEnt->s_port;
    }

    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = nPort;
    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
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::WSA_GETHOSTBY_NAME_ADDR);
        }
    }

    if(connect(hSocket,(LPSOCKADDR)&sockAddr,sizeof(sockAddr)) == SOCKET_ERROR)
    {
        closesocket(hSocket);
        throw ECSmtp(ECSmtp::WSA_CONNECT);
    }

    return hSocket;
}

/*Connecting to the remote server in non-blocking mode*/
SOCKET CSmtp::ConnectRemoteServer(const char *szServer,const unsigned short nPort_)
{
    unsigned short nPort = 0;
    LPSERVENT lpServEnt;
    SOCKADDR_IN sockAddr;
    unsigned long ul = 1;
    fd_set fdwrite,fdexcept;
    timeval timeout;
    int res = 0;

    timeout.tv_sec = TIME_IN_SEC;
    timeout.tv_usec = 0;

    SOCKET hSocket = INVALID_SOCKET;

    if((hSocket = socket(PF_INET, SOCK_STREAM,0)) == INVALID_SOCKET)
        throw ECSmtp(ECSmtp::WSA_INVALID_SOCKET);

    if(nPort_ != 0)
        nPort = htons(nPort_);
    else
    {
        lpServEnt = getservbyname("mail", 0);
        if (lpServEnt == NULL)
            nPort = htons(25);
        else 
            nPort = lpServEnt->s_port;
    }

    sockAddr.sin_family = AF_INET;
    sockAddr.sin_port = nPort;
    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
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::WSA_GETHOSTBY_NAME_ADDR);
        }
    }

    // start non-blocking mode for socket:
    if(ioctlsocket(hSocket,FIONBIO, (unsigned long*)&ul) == SOCKET_ERROR)
    {
        closesocket(hSocket);
        throw ECSmtp(ECSmtp::WSA_IOCTLSOCKET);
    }

    if(connect(hSocket,(LPSOCKADDR)&sockAddr,sizeof(sockAddr)) == SOCKET_ERROR)
    {
        if(WSAGetLastError() != WSAEWOULDBLOCK)
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::WSA_CONNECT);
        }
    }
    else
        return hSocket;

    while(true)
    {
        FD_ZERO(&fdwrite);
        FD_ZERO(&fdexcept);

        FD_SET(hSocket,&fdwrite);
        FD_SET(hSocket,&fdexcept);

        if((res = select(hSocket+1,NULL,&fdwrite,&fdexcept,&timeout)) == SOCKET_ERROR)
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::WSA_SELECT);
        }

        if(!res)
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::SELECT_TIMEOUT);
        }
        
        if(res && FD_ISSET(hSocket,&fdwrite))
            break;

        if(res && FD_ISSET(hSocket,&fdexcept))
        {
            closesocket(hSocket);
            throw ECSmtp(ECSmtp::WSA_SELECT);
        }
    } // while

    FD_CLR(hSocket,&fdwrite);
    FD_CLR(hSocket,&fdexcept);

    return hSocket;
}

Usage

#include "CSmtp.h"
#include <iostream>

int main()
{
    bool bError = false;

    try
    {
        CSmtp mail;

        mail.SetSMTPServer("smtp.domain.com",25);
        mail.SetLogin("***");
        mail.SetPassword("***");
        mail.SetSenderName("User");
        mail.SetSenderMail("user@domain.com");
        mail.SetReplyTo("user@domain.com");
        mail.SetSubject("The message");
        mail.AddRecipient("friend@domain2.com");
        mail.SetXPriority(XPRIORITY_NORMAL);
        mail.SetXMailer("The Bat! (v3.02) Professional");
        mail.AddMsgLine("Hello,");
        mail.AddMsgLine("");
        mail.AddMsgLine("How are you today?");
        mail.AddMsgLine("");
        mail.AddMsgLine("Regards");
        mail.AddMsgLine("--");
        mail.AddMsgLine("User");
        mail.AddAttachment("c:\\test.exe");
        mail.AddAttachment("c:\\test2.jpg");
    
        mail.Send();
    }
    catch(ECSmtp e)
    {
        std::cout << "Error: " << e.GetErrorText().c_str() << ".\n";
        bError = true;
    }

    if(!bError)
    {
        std::cout << "Mail was send successfully.\n";
        return 0;
    }
    else
        return 1;
}

Author's notes

  1. If you have problems sending an email, use Visual Studio's debugger and analyze the conversation between your SMTP server and the client; perhaps, your server needs a different kind of authentication or doesn't need it at all.
  2. You are not allowed to use the CSmtp class for spamming.

Bibliography

  1. CFastSmtp - Fast and easy SMTP class...
  2. Simple Mail Transfer Protocol RFC 821
  3. Standard for the Format of ARPA Internet Text Messages RFC 822
  4. SMTP Service Extension for Authentication RFC 2554
  5. MIME: Format of Internet Message Bodies RFC 2045
  6. MIME: Media Types RFC 2046
  7. MIME: Message Header Extensions for Non-ASCII Text RFC 2047
  8. MIME: Registration Procedures RFC 2048
  9. MIME: Conformance Criteria and Examples RFC 2049
  10. The Berkeley Sockets Application Programming Interface (API)
  11. select function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jakub Piwowarczyk
Engineer Technical University of Lodz
Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionError in message body : Office365 Pin
Member 1081871220-Apr-15 4:32
memberMember 1081871220-Apr-15 4:32 
QuestionResource Leaks found Pin
Theo Buys21-Jan-15 4:31
memberTheo Buys21-Jan-15 4:31 
QuestionLinux Compiling Issue Pin
Member 110961474-Jan-15 6:39
memberMember 110961474-Jan-15 6:39 
QuestionUsing CSmtp with Managed Code Pin
east757-Oct-14 3:40
membereast757-Oct-14 3:40 
QuestionUsing CSmtp without Auth Pin
Jessn27-Aug-14 3:59
memberJessn27-Aug-14 3:59 
QuestionFix for GetLocalHostName Pin
Jessn27-Aug-14 3:53
memberJessn27-Aug-14 3:53 
GeneralRe: Fix for GetLocalHostName Pin
Martin.Cheng22-Dec-14 18:32
memberMartin.Cheng22-Dec-14 18:32 
Questionhow to send html email Pin
ASERERTA@#@s7-May-14 0:52
memberASERERTA@#@s7-May-14 0:52 
QuestionE-mail with attachment failed. Pin
Member 1065573019-Mar-14 11:05
memberMember 1065573019-Mar-14 11:05 
AnswerRe: E-mail with attachment failed. Pin
leither90820-Apr-14 18:01
memberleither90820-Apr-14 18:01 
Questionexclamation mark (0x21) always at position 0x03b2 in received email Pin
Member 1061636221-Feb-14 21:07
memberMember 1061636221-Feb-14 21:07 
AnswerRe: exclamation mark (0x21) always at position 0x03b2 in received email Pin
Member 1061636225-Feb-14 9:53
memberMember 1061636225-Feb-14 9:53 
QuestionAttachments Pin
Member 105615573-Feb-14 0:17
memberMember 105615573-Feb-14 0:17 
QuestionAttachment error Pin
Member 105615571-Feb-14 9:24
memberMember 105615571-Feb-14 9:24 
SuggestionNon-ASCII charset patch Pin
S Haubenthal11-Nov-13 2:02
memberS Haubenthal11-Nov-13 2:02 
QuestionError: Undefined error id. Pin
Robert Hegner29-Aug-13 22:38
memberRobert Hegner29-Aug-13 22:38 
GeneralMy vote of 5 Pin
sspkmnd8-Aug-13 11:35
membersspkmnd8-Aug-13 11:35 
QuestionFile is still opened after sending e-mail Pin
Member 1008208111-Jul-13 4:28
memberMember 1008208111-Jul-13 4:28 
GeneralMy vote of 5 Pin
Vivekanandm7-Jul-13 21:51
memberVivekanandm7-Jul-13 21:51 
Questionhow to send an email via a proxy? Pin
ASERERTA@#@s8-May-13 8:00
memberASERERTA@#@s8-May-13 8:00 
Questionnot able to send mail without username and password. Pin
rohitnegi26-Feb-13 21:47
memberrohitnegi26-Feb-13 21:47 
QuestionGot an Error Pin
Sonal Dave26-Feb-13 17:58
memberSonal Dave26-Feb-13 17:58 
QuestionSome Questions Pin
xx041723-Jan-13 22:34
memberxx041723-Jan-13 22:34 
BugNot working on Vista and windows 7 Pin
trokfox6-Jan-13 10:56
membertrokfox6-Jan-13 10:56 
GeneralRe: Not working on Vista and windows 7 Pin
beyond197714-Feb-14 20:01
memberbeyond197714-Feb-14 20:01 
QuestionNo password? Pin
Member 86882605-Dec-12 11:11
memberMember 86882605-Dec-12 11:11 
GeneralMy vote of 5 Pin
gndnet12-Oct-12 8:55
membergndnet12-Oct-12 8:55 
Questionportability bug w/ attachment name Pin
Spike!4-Sep-12 11:08
memberSpike!4-Sep-12 11:08 
AnswerRe: portability bug w/ attachment name Pin
David Johns3-Nov-12 16:32
memberDavid Johns3-Nov-12 16:32 
Great point. Is there better solution than:

		for(FileId=0;FileId<Attachments.size();FileId++)
		{
			strcpy(FileName,Attachments[FileId].c_str());
 
			sprintf(SendBuf,"--%s\r\n",BOUNDARY_TEXT);
			strcat(SendBuf,"Content-Type: application/x-msdownload; name=\"");
#ifndef LINUX
			strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]);
#else
			strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("/") + 1]);
#endif
			strcat(SendBuf,"\"\r\n");
			strcat(SendBuf,"Content-Transfer-Encoding: base64\r\n");
			strcat(SendBuf,"Content-Disposition: attachment; filename=\"");
#ifndef LINUX
			strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("\\") + 1]);
#else
			strcat(SendBuf,&FileName[Attachments[FileId].find_last_of("/") + 1]);
#endif
			strcat(SendBuf,"\"\r\n");
			strcat(SendBuf,"\r\n");
			.
			.
			.

Questionportability bugs w/ std::string and exceptions Pin
Spike!4-Sep-12 8:01
memberSpike!4-Sep-12 8:01 
AnswerRe: portability bugs w/ std::string and exceptions Pin
David Johns3-Nov-12 16:26
memberDavid Johns3-Nov-12 16:26 
GeneralMy vote of 5 Pin
Evren Daglioglu28-Jun-12 0:45
memberEvren Daglioglu28-Jun-12 0:45 
GeneralMy vote of 4 Pin
xComaWhitex22-Apr-12 14:05
memberxComaWhitex22-Apr-12 14:05 
QuestionServer returned error after sending MAIL FROM Pin
namsaray26-Jan-12 8:41
membernamsaray26-Jan-12 8:41 
Questionbug fixes GetLocalHostName(), Send() Pin
jerko30-Nov-11 23:14
memberjerko30-Nov-11 23:14 
AnswerRe: bug fixes GetLocalHostName(), Send() Pin
Alan P Brown1-Dec-11 22:06
memberAlan P Brown1-Dec-11 22:06 
AnswerRe: bug fixes GetLocalHostName(), Send() Pin
Spike!4-Sep-12 8:12
memberSpike!4-Sep-12 8:12 
GeneralError Pin
min_2_max12-May-11 4:57
membermin_2_max12-May-11 4:57 
GeneralRe: Error Pin
min_2_max17-May-11 18:12
membermin_2_max17-May-11 18:12 
GeneralRe: Error Pin
highersky16-Aug-12 21:32
memberhighersky16-Aug-12 21:32 
GeneralMy vote of 3 Pin
orighost16-Apr-11 15:43
memberorighost16-Apr-11 15:43 
GeneralRe: My vote of 3 Pin
Jakub Piwowarczyk10-Dec-11 6:22
memberJakub Piwowarczyk10-Dec-11 6:22 
QuestionHTML body Pin
nj9626-Jan-11 10:53
membernj9626-Jan-11 10:53 
AnswerRe: HTML body Pin
Jakub Piwowarczyk27-Jan-11 6:58
memberJakub Piwowarczyk27-Jan-11 6:58 
GeneralRe: HTML body Pin
Robert Valentino26-Jan-12 14:12
memberRobert Valentino26-Jan-12 14:12 
GeneralError:Server returned error after sending EHLO Pin
caixin29-Nov-10 1:33
membercaixin29-Nov-10 1:33 
GeneralRe: Error:Server returned error after sending EHLO Pin
tmxq5610-Jan-11 12:14
membertmxq5610-Jan-11 12:14 
AnswerRe: Error:Server returned error after sending EHLO Pin
Jakub Piwowarczyk11-Jan-11 8:25
memberJakub Piwowarczyk11-Jan-11 8:25 
GeneralRe: Error:Server returned error after sending EHLO Pin
svyeo30-Jan-11 23:44
membersvyeo30-Jan-11 23:44 
GeneralRe: Error:Server returned error after sending EHLO Pin
min_2_max17-May-11 18:12
membermin_2_max17-May-11 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 16 Jul 2010
Article Copyright 2008 by Jakub Piwowarczyk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid