简单邮件传输协议SMTP封装类

时间:2021-01-29 18:08:09
简单邮件传输协议SMTP封装类


作者:Asif Rasheed 翻译:刘建强

在Internet上,Email是最流行的传输媒体。这篇文章包括两个协议:. POP 3 协议: POP3协议(邮政传输协议)就是指从Email服务器接收信件。我已经提交了一个封装POP3协议的类。该协议官方的描述你可查阅RFC1225。 SMTP协议: SMTP (简单邮件传输协议) 指发送邮件到它的目的地。 有关SMTP 协议的细节你可参考RCF 821 .我最新地贡献是封装了SMTP协议。我不能完全实现 SMTP协议但你可用它在许多应用场合发送邮件。该类有若干方法,我选方法名同SMTP命令, 所以你很容易理解。 在该类中,我再一次使用了CSocket作为类成员,所以我能在线程中使用它. 下面是该类的编码及注释:

//////////////////////////////////////////////////////////////////////
// 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)) // 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/r/n", (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;
}

// 发送"QUIT"命令到SMTP服务器:
BOOL CSMTP::Disconnect()
{
char buf[256];

wsprintf (buf, "QUIT /r/n");
m_SMTPServer.Send(buf, strlen (buf));
if (CheckResponse(QUIT_CHECK)==FALSE)
return FALSE;
else
return TRUE;
}

// 发送"MAIL" 命令到SMTP服务器:
BOOL CSMTP::Mail(CString from)
{
char buf[256];

wsprintf (buf, "MAIL From:<%s>/r/n", (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>/r/n", (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");
}

// 发送"DATA" 命令到 SMTP 服务器:
BOOL CSMTP::Data(CString Subject, CString Body)
{
char buf[256];

wsprintf (buf, "DATA/r/n");

m_SMTPServer.Send(buf, strlen (buf));
if (CheckResponse(DATA_CHECK)==FALSE)
return FALSE;
else
{
wsprintf (buf, "SUBJECT:%s/r/n", (LPCSTR) Subject);
m_SMTPServer.Send(buf, strlen (buf));

wsprintf (buf, "%s/r/n", (LPCSTR) Body);
m_SMTPServer.Send(buf, strlen (buf));

wsprintf (buf, "./r/n");
m_SMTPServer.Send(buf, strlen (buf));

return TRUE;
}
}

//本方法检查服务器的响应:
BOOL CSMTP::CheckResponse(int Type)
{
char buf [1000];
char temp[3];

for (int i=0;i < 512;i++)
buf[i]='/0';

//从服务器接收数据
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;
}

// 返回错误消息
CString CSMTP::GetErrorMessage()
{
return m_ErrorMessage;
}

// 按照错误号准备错误信息
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 not local: will forward to forward path");
if(Response.Find("354"))
return _T("Start mail input; end with
.
");

return _T("No Error Number is matched with ")+Response;
}

// Just overloading of the Mail method
BOOL CSMTP::Mail()
{
return Mail(m_From);
} 
这是该类一个小的实现: 

CSMTP m_smtp;
m_smtp.Connect("khi.compol.com","aasif@khi.compol.com");
m_smtp.Mail("aasif@khi.compol.com");
m_smtp.SetTo("zafir@home.com");
m_smtp.Data("test message","This is a test message ... ");
m_smtp.Disconnect();