高三计算机课件 网络课程设计报告利用C实现SMTP协Word文档格式.docx
- 文档编号:3295894
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:70
- 大小:123.01KB
高三计算机课件 网络课程设计报告利用C实现SMTP协Word文档格式.docx
《高三计算机课件 网络课程设计报告利用C实现SMTP协Word文档格式.docx》由会员分享,可在线阅读,更多相关《高三计算机课件 网络课程设计报告利用C实现SMTP协Word文档格式.docx(70页珍藏版)》请在冰点文库上搜索。
SMTP协议工作原理
SMTP是工作在两种情况下:
一是电子邮件从客户机传输到服务器:
二是从某一个服务器传输到另一个服务器。
SMTP也是个请求/响应协议,命令和响应都是基于ASCⅡ文本,并以CR和LF符结束。
响应包括一个表示返回状态的三位数字代码。
SMTP在TCP协议25号端口监听连续请求。
连接和发送过程如下:
(1)建立TCP连接。
(2)客户端发送HELO命令以标识发件人自己的身份,然后客户端发送MAIL命令;
服务器端正希望以OK作为响应,表明准备接收。
(3)客户端发送RCPT命令,以标识该电子邮件的计划接收人,可以有多个RCPT行;
服务器端则表示是否愿意为收件人接收邮件。
(4)协商结束,发送邮件,用命令DATA发送。
(5)以“.”号表示结束输入内容一起发送出去,结束此次发送,用QUIT命令退出。
3.程序核心代码
3.1服务器端相关代码:
1)、相关核心代码如下:
//SMTPSeverDlg.cpp:
implementationfile
#include"
stdafx.h"
SMTPSever.h"
SMTPSeverDlg.h"
Picture.h"
#include<
fstream>
//CSMTPSeverDlgmessagehandlers
BOOLCSMTPSeverDlg:
:
OnInitDialog()
{
CDialog:
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
//TODO:
Addextrainitializationhere
IsDataContent=FALSE;
IsShow=FALSE;
m_Listener.SetParent(this);
m_Server.SetParent(this);
returnTRUE;
//returnTRUEunlessyousetthefocustoacontrol
}
voidCSMTPSeverDlg:
OnSysCommand(UINTnID,LPARAMlParam)
if((nID&
0xFFF0)==IDM_ABOUTBOX)
CAboutDlgdlgAbout;
dlgAbout.DoModal();
else
CDialog:
OnSysCommand(nID,lParam);
OnAccept()
CStringstr;
str.Format("
***收到连接请求"
);
m_List_Smtp.InsertString(-1,str);
if(m_Listener.Accept(m_Server))
str.Format("
***建立连接"
m_List_Smtp.InsertString(-1,str);
str=_T("
220SimpleMailSeverReadyforMail\r\n"
m_Server.Send((LPCTSTR)str,str.GetLength());
S:
"
)+str;
m_Server.AsyncSelect(FD_READ);
m_Server.Close();
OnReceive()
charbuff[65536];
charlocal_host[80];
intnRead;
memset(buff,0,65536);
//清空缓冲区接收数据
nRead=m_Server.Receive(buff,65536);
//根据读到的长度
switch(nRead)
case0:
break;
caseSOCKET_ERROR:
if(GetLastError()!
=WSAEWOULDBLOCK)
AfxMessageBox("
Erroroccurred"
default:
buff[nRead]=0;
//terminatethestring
CStringszTemp;
if(IsDataContent)
{
while(strstr(buff,"
\r\n.\r\n"
)==NULL)
str+=buff;
memset(buff,0,65536);
nRead=m_Server.Receive(buff,65536);
str+=buff;
CStringTemp;
Temp.Format("
%s\r\n\r\n"
(LPCTSTR)str.c_str());
m_Edit_Con.SetWindowText(Temp);
Base64_decode(Temp);
szTemp=_T("
250Messageacceptedfordelivery\r\n"
m_Server.Send(szTemp,szTemp.GetLength());
)+szTemp;
m_List_Smtp.InsertString(-1,szTemp);
szTemp.Empty();
IsDataContent=FALSE;
if(strnicmp(buff,"
HELO"
4)==0)
szTemp=buff;
szTemp=_T("
C:
//m_List_Smtp.InsertString(-1,(LPCTSTR)buff);
m_List_Smtp.InsertString(-1,(LPCTSTR)szTemp);
gethostname(local_host,80);
szTemp.Format(_T("
250OK%s\r\n"
),local_host);
m_Server.Send((LPCTSTR)szTemp,szTemp.GetLength());
m_List_Smtp.InsertString(-1,szTemp);
szTemp.Empty();
MAILFROM:
"
10)==0)
250SenderOK\r\n"
m_Server.Send(szTemp,szTemp.GetLength());
RCPTTO:
8)==0)
//m_List_Smtp.InsertString(-1,(LPCTSTR)buff);
250ReceiverOK\r\n"
DATA"
szTemp=_T("
354Goahead.Endwith<
CRLF.CRLF>
\r\n"
IsDataContent=TRUE;
QUIT"
221Quit,Goodbye!
IsShow=TRUE;
if(strnicmp(buff,"
AUTHLOGIN"
334dXNlcm5hbWU6\r\n"
m_Server.Send(szTemp,szTemp.GetLength());
bWFu"
334UGFzc3dvcmQ6\r\n"
bGFp"
235Authenticationsuccessful\r\n"
}
DestroyWindow()
Addyourspecializedcodehereand/orcallthebaseclass
m_Listener.Close();
returnCDialog:
DestroyWindow();
OnClose()
Listeningonport%d"
25);
OnButtonClose()
Addyourcontrolnotificationhandlercodehere
m_List_Smtp.InsertString(-1,"
服务器关闭成功"
OnButtonOpen()
BOOLbFlag=m_Listener.Create(25,SOCK_STREAM);
if(!
bFlag)
if(GetLastError()!
=WSAEWOULDBLOCK)
TCHARszError[256];
wsprintf(szError,"
Socket建立失败:
%d"
GetLastError());
m_Listener.Close();
AfxMessageBox(szError);
AfxMessageBox("
wrong"
return;
m_Listener.Listen
(1))
监听失败:
*****服务器准备好*****"
************************"
OnButtonQuit()
//Base64.cpp:
implementationoftheBase64class.
Base64.h"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
Base64:
Base64(){}
~Base64(){}
staticconststd:
stringbase64_chars="
ABCDEFGHIJKLMNOPQRSTUVWXYZ"
abcdefghijklmnopqrstuvwxyz"
0123456789+/"
;
std:
stringBase64:
base64_encode(conststd:
string&
s)
{
returnbase64_encode((constunsignedchar*)s.c_str(),s.length());
///////////////////////////////////////////解码函数的实现//////////////////////////////////////////
base64_decode(unsignedcharconst*encoded_string,unsignedintin_len)
inti=0;
intj=0;
intin_=0;
unsignedcharchar_array_4[4],char_array_3[3];
std:
stringret;
while(in_len--&
&
(encoded_string[in_]!
='
='
)&
is_base64(encoded_string[in_]))
char_array_4[i++]=encoded_string[in_];
in_++;
if(i==4)
{
for(i=0;
i<
4;
i++)
char_array_4[i]=base64_chars.find(char_array_4[i]);
char_array_3[0]=(char_array_4[0]<
<
2)+((ch
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高三计算机课件 网络课程设计报告利用C 实现SMTP协 计算机 课件 网络 课程设计 报告 利用 实现 SMTP