On the Internet, email is the most popular transmission medium. This article consists of two agreements:. POP 3 Protocol: The POP3 Protocol (Postal Transport Protocol) refers to receiving letters from an email server. I have submitted a class that encapsulates the POP3 protocol. The official description of the agreement you can refer to RFC1225. SMTP protocol: SMTP (Simple Mail Transfer Protocol) refers to sending mail to its destination. For details about the SMTP protocol you can refer to RCF 821. My latest contribution is to encapsulate the SMTP protocol. I can't fully implement the SMTP protocol but you can use it to send mail in many applications. This class has several methods, I choose the method name with the SMTP command, so you can easily understand. In this class, I use CSocket again as a class member, so I can use it in a thread. Here is the code and comment for the class:
//////////////////////////////////////////////////////////////////////
Smtp.h:interface for the Csmtp class.
//
//////////////////////////////////////////////////////////////////////
#if!defined (Afx_smtp_h__617f7e82_7f4d_11d1_88a0_00001c302581__included_)
#define Afx_smtp_h__617f7e82_7f4d_11d1_88a0_00001c302581__included_
#if _msc_ver >= 1000
#pragma once
#endif//_msc_ver >= 1000
#define Connection_check 0
#define Hello_check 1
#define Mail_check 2
#define Rcpt_check 3
#define Data_start_check 4
#define Data_end_check 5
#define Quit_check 6
#define Data_check 7
Class CSMTP
{
Public
BOOL Mail (); //
CString GetErrorMessage ();
BOOL Data (CString Subject, CString body);
CString Getto ();
BOOL Setto (CString to);
CString Getfrom ();
void Setfrom (CString from);
BOOL Mail (CString from);
BOOL Disconnect ();
CString GetHost ();
void Sethost (CString Host);
BOOL Connect (CString Host, CString Me);
BOOL Connect ();
CSMTP ();
Virtual ~csmtp ();
Private
CString GetError (CString Response);
CString M_errormessage;
BOOL checkresponse (int Type);
int m_noofto;
CStringArray m_to;
CString M_from;
CSocket M_smtpserver;
CString M_host;
};
#endif//!defined (AFX_SMTP_H__617F7E82_7F4D_11D1_88A0_00001C302581__INCLUDED_)
//////////////////////////////////////////////////////////////////////////
SMTP.cpp:implementation of the Csmtp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MailSend.h"
#include "SMTP.h"
#ifdef _DEBUG
#undef This_file
static char this_file[]=__file__;
#define NEW Debug_new
#endif
//////////////////////////////////////////////////////////////////////
Construction/destruction
//////////////////////////////////////////////////////////////////////
CSMTP::CSMTP ()
{
M_NOOFTO = 0;
M_smtpserver.create ();
}
CSMTP::~CSMTP ()
{
M_smtpserver.close ();
}
Connect to the SMTP Server
BOOL Csmtp::connect ()
{
Return Connect (M_host,m_from);
}
Connect to the SMTP Server
BOOL Csmtp::connect (CString host,cstring from)
{
if (!m_smtpserver.connect (host,25))//For SMTP Port
{
M_errormessage = _t ("Server cannot be connected");
return FALSE;
}
Else
{
if (Checkresponse (Connection_check) ==false)
return FALSE;
char buf [512];
wsprintf (buf, "HELO%s", (LPCSTR) from);
M_smtpserver.send (buf, strlen (BUF));
if (Checkresponse (Hello_check) ==false)
return FALSE;
Else
return TRUE;
return TRUE;
}
}
Setting the Host String
void Csmtp::sethost (CString Host)
{
M_host = Host;
}
Returing the Host String
CString Csmtp::gethost ()
{
return m_host;
}
Send the "QUIT" command to the SMTP server:
BOOL csmtp::D isconnect ()
{
Char buf[256];
wsprintf (buf, "QUIT");
M_smtpserver.send (buf, strlen (BUF));
if (Checkresponse (Quit_check) ==false)
return FALSE;
Else
return TRUE;
}
To send the MAIL command to an SMTP server:
BOOL Csmtp::mail (CString from)
{
Char buf[256];
wsprintf (buf, "MAIL from:<%s>", (LPCSTR) from);
M_smtpserver.send (buf, strlen (BUF));
if (Checkresponse (Mail_check) ==false)
return FALSE;
Else
return TRUE;
}
Setting the From string
void Csmtp::setfrom (CString from)
{
M_from = from;
}
Returing the From string
CString Csmtp::getfrom ()
{
return m_from;
}
Setting the To String
BOOL Csmtp::setto (CString to)
{
Char buf[256];
M_to.add (to); Saving Vale of To
wsprintf (buf, "RCPT to:<%s>", (LPCSTR) to);
M_smtpserver.send (buf, strlen (BUF));
if (Checkresponse (Rcpt_check) ==false)
return FALSE;
Else
return TRUE;
}
Returing the To String
CString Csmtp::getto ()
{
if (M_to.getsize () >=m_noofto)
{
m_noofto++;
return m_to[m_noofto-1];
}
Else
Return _t ("No more to Available");
}
Send the "DATA" command to the SMTP server:
BOOL csmtp::D ata (CString Subject, CString body)
{
Char buf[256];
wsprintf (buf, "DATA");
M_smtpserver.send (buf, strlen (BUF));
if (Checkresponse (Data_check) ==false)
return FALSE;
Else
{
wsprintf (buf, "subject:%s", (LPCSTR) SUBJECT);
M_smtpserver.send (buf, strlen (BUF));
wsprintf (buf, "%s", (LPCSTR) body);
M_smtpserver.send (buf, strlen (BUF));
wsprintf (buf, ".");
M_smtpserver.send (buf, strlen (BUF));
return TRUE;
}
}
This method checks the response of the server:
BOOL csmtp::checkresponse (int Type)
{
char buf [1000];
Char temp[3];
for (int i=0;i < 512;i++)
buf[i]= ' 0 ';
Receiving data from the server
M_smtpserver.receive (buf, sizeof (BUF));
strncpy (temp,buf,3);
int temp2 = atoi (temp);
Switch (Type)
{
Case Connection_check:
if (Temp2!= 220)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Hello_check:
if (Temp2!= 250)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Mail_check:
if (Temp2!= 250)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Rcpt_check:
if (Temp2!= 250)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Data_start_check:
if (Temp2!= 354)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Data_end_check:
if (Temp2!= 250)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Quit_check:
if (temp2!= 221)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
Case Data_check:
if (Temp2!= 354)
{
M_errormessage = GetError ((LPCTSTR) buf);
return FALSE;
}
Break
}
return TRUE;
}
return error message
CString Csmtp::geterrormessage ()
{
return m_errormessage;
}
Prepare error messages by error number
CString csmtp::geterror (CString Response)
{
if (Response.find ("211"))
Return _t ("System status or System help reply");
if (Response.find ("214"))
Return _t ("help Message");
if (Response.find ("220"))
Return _t ("Service is ready");
if (Response.find ("221"))
Return _t ("Service closing transmission channel");
if (Response.find ("250"))
Return _t ("Requested mail action Okay, completed");
if (Response.find ("251"))
Return _t ("user isn't local:will forward to forward path");
if (Response.find ("354"))
Return _t ("Start mail input; End With. ");
Return _t ("No Error number are matched with") +response;
}
Just overloading of the Mail method
BOOL Csmtp::mail ()
{
Return Mail (M_from);
}
This is a small implementation of the class: