求教关于SMTP 本人要写一个邮件发送程序(不通过SMTP服务器直接发送到接受方服务器)的程序, 如果我获得邮箱域名的MX记录后,开始建立连接,对方服务器是不是要验证发信人的地址呢? 也就是说:SMTP服务器之间是以怎样的格式传送邮件的。他们之间需要验证吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SMTP SMTP(Simple Mail Transfer Protocol)通常是发送Email的协议。正式详细的描述你可以查阅RCF 821协议。这里我将提供一个SMTP协议类。我没有提供 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 7class 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_FILEstatic 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 ServerBOOL CSMTP::Connect(){return Connect(m_Host,m_From);}// Connect to the SMTP ServerBOOL 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;elsereturn TRUE;return TRUE;}}// Setting the Host Stringvoid CSMTP::SetHost(CString Host){m_Host = Host;}// Returing the Host StringCString CSMTP::GetHost(){return m_Host;}// Sending the "QUIT" command to the SMTP ServerBOOL CSMTP::Disconnect(){char buf[256];wsprintf (buf, "QUIT \r\n");m_SMTPServer.Send(buf, strlen (buf));if (CheckResponse(QUIT_CHECK)==FALSE)return FALSE;elsereturn TRUE;}// Sending the "MAIL" command to the SMTP ServerBOOL 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;elsereturn TRUE;}// Setting the From stringvoid CSMTP::SetFrom(CString from){m_From = from;}// Returing the From stringCString CSMTP::GetFrom(){return m_From;}// Setting the TO stringBOOL CSMTP::SetTo(CString to){char buf[256];m_To.Add(to); // Saving vale of towsprintf (buf, "RCPT TO:<%s>\r\n", (LPCSTR) to);m_SMTPServer.Send(buf, strlen (buf));if (CheckResponse(RCPT_CHECK)==FALSE)return FALSE;elsereturn TRUE;}// Returing the TO stringCString CSMTP::GetTo(){if(m_To.GetSize()>=m_NoOfTo){m_NoOfTo++;return m_To[m_NoOfTo-1];}elsereturn _T("No more To available");}// Sending the "DATA" command to the SMTP ServerBOOL 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;}}// This methods checks the response from the// ServerBOOL CSMTP::CheckResponse(int Type){char buf [1000];char temp[3];for (int i=0;i < 512;i++)buf[i]='\0';// Receive the data from Serverm_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;}// Returing the Error MessageCString CSMTP::GetErrorMessage(){return m_ErrorMessage;}// Preparing the Error Message according to// Error NumberCString 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 methodBOOL CSMTP::Mail(){return Mail(m_From);}简单用法: CSMTP m_smtp;m_smtp.Connect("khi.compol.com","[email protected]");m_smtp.Mail("[email protected]");m_smtp.SetTo("[email protected]");m_smtp.Data("test message","This is a test message ... ");m_smtp.Disconnect(); 谢谢!不过我想知道的是一个带有SMTP服务认证扩展的SMTP服务器。我想直接向他发送邮件而不是通过其他SMTP服务器一中继的方式发送。那么我还需要验证用户吗?如果需要,通过SMTP服务器转发是怎么通过。 UPhttp://www.csdn.net/Develop/Read_Article.asp?Id=9219 为什么得不到MAC地址? 辞职散分 CBitmap怎么用serialize序列化? 自绘按钮控件遇到的问题 关于Wpcap网络编程的问题? 如何在COM中执行一个存储过程,返回的是游标?高手请进,来者有分 各位大虾,给我介绍几本C++的名著好吗 拨号网络拨上后,会在托盘区显示“连接到8888”,这是怎么做的啊? 我装的MSDN在索引区什么都看不见,为什么? 菜鸟问题,怎么给一个基于对话框的程序在它的上面加上最小化和最大化按钮? 是不是Access不支持alter table中的modify子句?在线 关于图标
细的描述你可以查阅RCF 821协议。这里我将提供一个SMTP协议类。我没有提供
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;
}// Sending the "QUIT" command to the SMTP Server
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;
}// Sending the "MAIL" command to the SMTP Server
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 towsprintf (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");
}// Sending the "DATA" command to the SMTP Server
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;
}
}// This methods checks the response from the
// Server
BOOL CSMTP::CheckResponse(int Type)
{
char buf [1000];
char temp[3];for (int i=0;i < 512;i++)
buf[i]='\0';// Receive the data from 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;
}// Returing the Error Message
CString CSMTP::GetErrorMessage()
{
return m_ErrorMessage;
}// Preparing the Error Message according to
// 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 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","[email protected]");
m_smtp.Mail("[email protected]");
m_smtp.SetTo("[email protected]");
m_smtp.Data("test message","This is a test message ... ");
m_smtp.Disconnect();
不过我想知道的是一个带有SMTP服务认证扩展的SMTP服务器。
我想直接向他发送邮件而不是通过其他SMTP服务器一中继的方式发送。
那么我还需要验证用户吗?
如果需要,通过SMTP服务器转发是怎么通过。
http://www.csdn.net/Develop/Read_Article.asp?Id=9219