📄 mailview.cpp.bak
字号:
// MailView.cpp : 实现文件
//
#include "stdafx.h"
#include <sstream>
#include <fstream>
#include <ctype.h>
#include "biyesheji.h"
#include "MailView.h"
#include ".\mailview.h"
//using namespace std;
std::string sBase64Alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// CMailView 对话框
IMPLEMENT_DYNAMIC(CMailView, CDialog)
CMailView::CMailView(CWnd* pParent /*=NULL*/)
: CDialog(CMailView::IDD, pParent)
, m_mailData(_T(""))
, m_From(_T(""))
, m_To(_T(""))
, m_Subject(_T(""))
, m_Content(_T(""))
, m_Attachment(_T(""))
, m_NeiQian(_T(""))
{
}
CMailView::~CMailView()
{
}
void CMailView::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_DATA_EDIT, m_mailData);
DDX_Text(pDX, IDC_FROM_EDIT, m_From);
DDX_Text(pDX, IDC_TO_EDIT, m_To);
DDX_Text(pDX, IDC_SUBJECT_EDIT, m_Subject);
DDX_Text(pDX, IDC_CONTENT_EDIT, m_Content);
DDX_Text(pDX, IDC_ATTACH_EDIT, m_Attachment);
DDX_Text(pDX, IDC_NEIQIAN_EDIT, m_NeiQian);
}
void CMailView::PostNcDestroy()
{
delete this;
CDialog::PostNcDestroy();
}
BEGIN_MESSAGE_MAP(CMailView, CDialog)
ON_BN_CLICKED(IDC_OPEN_BUTTON, OnBnClickedOpenButton)
END_MESSAGE_MAP()
// CMailView 消息处理程序
void CMailView::OnBnClickedOpenButton()
{
using std::string;
using std::ifstream;
CString fname;
char path[256];
string str,body,temp,_str;
ifstream fin;
//struct content
content cont;
//default value
cont.charset="us-ascii";
cont.content_type="text/plain";
cont.encoding="7bit";
bool mime=false,in_body=false,over=false;
size_t pos,p;
int item = 1,kind,post = -1;
GetCurrentDirectory(250,path);
CFileDialog dlg(TRUE,"*.dat",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Data Files (*.dat)|*.dat|All Files (*.*)|*.*||");
dlg.m_pOFN->lpstrInitialDir = "c:";
if (dlg.DoModal() == IDOK)
{
m_mailData="";
m_From="";
m_To="";
m_Subject="";
m_Content="";
m_Attachment="";
m_NeiQian="";
fname = dlg.GetPathName();
SetCurrentDirectory(path);
fill_Maildata(fname);
fin.open(fname);
while(getline(fin,str,'\n'))
{
switch(item)
{
case 1://get mail head
{
if (str.empty())
item += kind;
if (isspace(str[0]))
{
if (post == 1)
m_Subject += str.c_str();
if (post == 0)
m_To += str.c_str();
//if (post == 2)
// cc+=str;
//if (post == 3)
// recvv+=str;
}
/*if (!(pos = str.find("Received:")))
{
recvv = str;
post = 3;
}*/
/*if (!(pos = str.find("Message-ID:")))
{
msgID = str;
}*/
if (!(pos = str.find("From:")))
{
temp = str.substr(5,str.length()-5);
if(isspace(temp[0]))
{
temp = temp.substr(1,temp.length()-1);
m_From = temp.c_str();
}
}
if (!(pos = str.find("To:")))
{
temp = str.substr(3,str.length()-3);
if(isspace(temp[0]))
{
temp = temp.substr(1,temp.length()-1);
m_To = temp.c_str();
post = 0;
}
}
/*if (!(pos = str.find("Cc:")))
{
cc = str;
post = 2;
}*/
if (!(pos = str.find("Subject:")))
{
temp = str.substr(8,str.length()-8);
if(isspace(temp[0]))
{
temp = temp.substr(1,temp.length()-1);
if ((p = temp.find("=?")) == 0)
{
p = temp.find('?',2);
if (temp[p + 1] == 'b' || temp[p + 1] == 'B')
temp = temp.substr(p + 3,temp.length()-p-5);
DecodeBase64(temp,_str);
}
m_Subject = _str.c_str();
post = 1;
}
}
/*if (!(pos = str.find("Date:")))
{
date = str;
}*/
if (!(pos = str.find("MIME-Version:")))
{
mime = true;
}
if (!(pos = str.find("Content-Type:")))
{
p = str.find(';');
temp = str.substr(13,p-13);
if(isspace(temp[0]))
{
cont.content_type = temp.substr(1,temp.length()-1);
str_tolower(cont.content_type);
if ((pos = (cont.content_type).find("multipart"))!=string::npos)
kind = 1;
else
kind = 2;
post = -1;
}
//MessageBox(cont.content_type.c_str());
}
if((pos=str.find("boundary="))!=string::npos)
{
temp = str.substr(pos+9,str.length()-pos-9);
if(temp[0] == 0x22)
cont.boundary = temp.substr(1,temp.length()-2);
}
if ((pos = str.find("charset="))!=string::npos)
{
temp = str.substr(pos+8,str.length()-pos-8);
if(temp[0] == 0x22)
{
cont.charset = temp.substr(1,temp.length()-2);
str_tolower(cont.charset);
}
}
if (!(pos = str.find("Content-Transfer-Encoding:")))
{
temp = str.substr(pos+26,str.length()-26);
if (isspace(temp[0]))
{
cont.encoding = temp.substr(1,temp.length()-1);
str_tolower(cont.encoding);
}
}
}
break;
case 2://multipart
{
if(str == "--"+cont.boundary)
in_body = true;
if (str == "--"+cont.boundary+"--")
{
over = true;
body += str;
}
if (in_body && !over)
{
body += str;
body += '\n';
}
}
break;
case 3://text
{
if (str == ".")
over = true;
//add to body
if (!over)
{
if (cont.encoding == "base64")
{
DecodeBase64(str,temp);
body += temp;
}
else
body += temp;
}
}
break;
}
}
if (item == 2)
m_Content = mail_body(body,cont).c_str();
else
m_Content = body.c_str();
UpdateData(FALSE);
}
}
void CMailView::str_tolower(std::string &str)
{
for (int i = 0;i < str.length();i ++)
if (isupper(str[i]))
str[i] = tolower(str[i]);
}
bool CMailView::myfind(const std::string &src,const std::string &temp_str)
{
using std::string;
size_t pos;
string temp = src;
str_tolower(temp);
pos = temp.find(temp_str);
return pos;
}
void CMailView::DecodeBase64(const std::string& src,std::string& dst)
{
unsigned char ssrc[4];
unsigned char ddst[3];
int i,j;
dst = "";
if (src.length() < 4)
return;
for (i = 0;i < src.length()-4;)
{
for (j = 0;j < 4;j++)
ssrc[j] = sBase64Alphabet.find(src[i++]);
ddst[0] = (ssrc[0] << 2) + ((ssrc[1] & 0x30) >> 4);
ddst[1] = ((ssrc[1] & 0xf) << 4) + ((ssrc[2] & 0x3c) >> 2);
ddst[2] = ((ssrc[2] & 0x3) << 6) + ssrc[3];
dst += ddst[0];
dst += ddst[1];
dst += ddst[2];
}
ssrc[0] = sBase64Alphabet.find(src[i]);
ssrc[1] = sBase64Alphabet.find(src[i+1]);
if (src[i+2] == '=')
{
ddst[0] = (ssrc[0] << 2) + ((ssrc[1] & 0x30) >> 4);
dst += ddst[0];
}
else if (src[i+3] == '=')
{
ssrc[2] = sBase64Alphabet.find(src[i+2]);
ddst[0] = (ssrc[0] << 2) + ((ssrc[1] & 0x30) >> 4);
ddst[1] = ((ssrc[1] & 0xf) << 4) + ((ssrc[2] & 0x3c) >> 2);
dst += ddst[0];
dst += ddst[1];
}
else
{
ssrc[2] = sBase64Alphabet.find(src[i+2]);
ssrc[3] = sBase64Alphabet.find(src[i+3]);
ddst[0] = (ssrc[0] << 2) + ((ssrc[1] & 0x30) >> 4);
ddst[1] = ((ssrc[1] & 0xf) << 4) + ((ssrc[2] & 0x3c) >> 2);
ddst[2] = ((ssrc[2] & 0x3) << 6) + ssrc[3];
dst += ddst[0];
dst += ddst[1];
dst += ddst[2];
}
}
std::string CMailView::mail_body(const std::string& body,const content& cn)
{
using std::string;
using std::iostream;
using std::stringstream;
char path[256];
string result,_result,temp,str,_str,sbody;
string _contentType,_charset,name,type,_contentID,_Disposition,_filename;
bool inbody = false,over = false ,_over = false;
size_t pos,_pos;
content ct;
ct.content_type = "text/plain";
ct.encoding = "7bit";
ct.charset = "us-ascii";
int item = 1,kind;
FILE *fp;//ofstream _out;
GetCurrentDirectory(250,path);
stringstream os;
os << body;
iostream in(os.rdbuf());
while (getline(in,temp,'\n'))
{
switch(item)
{
case 1:
{
if (temp.empty())
{
item += kind;
_result = "";
}
if ((pos = temp.find("Content-Type:"))!=string::npos)
{
_pos = temp.find(';',pos);
ct.content_type = temp.substr(pos,_pos-pos);
str_tolower(ct.content_type);
if ((pos = (ct.content_type).find("multipart"))!=string::npos)
kind = 2;
if ((pos = (ct.content_type).find("text"))!=string::npos)
kind = 1;
}
if ((pos = temp.find("name=")) != string::npos)
{
_str = temp.substr(pos+5,temp.length()-pos-5);
if (_str[0] = 0x22)
name = _str.substr(1,_str.length()-2);
//name="=?gb2312?B?x+fAyrGzvrAuSlBH?="
if ((pos = name.find("=?")) == 0)
{
_pos = name.find('?',2);
if (name[_pos + 1] == 'b' || name[_pos + 1] == 'B')
_str = name.substr(_pos + 3,name.length()-_pos-5);
DecodeBase64(_str,name);
}
_pos = name.rfind('.');
type = name.substr(_pos,name.length()-_pos);
}
if ((pos = temp.find("Content-ID:")) != string::npos)
{
_str = temp.substr(pos+11,temp.length()-pos-11);
if (isspace(_str[0]))
_contentID = _str.substr(2,_str.length()-3);
if (!_contentID.empty())
kind = 3;
}
if ((pos = temp.find("Content-Disposition:")) != string::npos)
{
_pos = temp.find(';',pos);
_Disposition = temp.substr(pos+20,_pos-20-pos);
str_tolower(_Disposition);
if ((pos = _Disposition.find("attachment")) != string::npos)
kind = 4;
}
if ((pos = temp.find("filename=")) != string::npos)
{
_str = temp.substr(pos+9,temp.length()-pos-9);
if (_str[0] == 0x22)
_filename = _str.substr(1,_str.length()-2);
//filename="=?gb2312?B?tuC0rr/azajQxbXE1LTC6y56aXA=?="
if ((pos = _filename.find("=?")) == 0)
{
_pos = _filename.find('?',2);
if (_filename[_pos + 1] == 'b' || _filename[_pos + 1] == 'B')
_str = _filename.substr(_pos + 3,_filename.length()-_pos-5);
DecodeBase64(_str,_filename);
}
}
if ((pos = temp.find("boundary="))!=string::npos)
{
_str = temp.substr(pos+9,temp.length()-pos-9);
if (_str[0] == 0x22)
ct.boundary = _str.substr(1,_str.length()-2);
}
if ((pos = temp.find("charset="))!=string::npos)
{
_str = temp.substr(pos+8,temp.length()-pos-8);
if (_str[0] == 0x22)
{
ct.charset = _str.substr(1,_str.length()-2);
str_tolower(ct.charset);
}
}
if ((pos = temp.find("Content-Transfer-Encoding:"))!=string::npos)
{
_str = temp.substr(pos+26,temp.length()-26-pos);
if (isspace(_str[0]))
{
ct.encoding = _str.substr(1,_str.length()-1);
str_tolower(ct.encoding);
}
}
}
break;
case 2:
{
if (temp == "--" + cn.boundary + "--")
_over = true;
if (temp == "--" + cn.boundary || _over)
item = 1;
else if (ct.encoding == "base64")
{
DecodeBase64(temp,str);
result += str;
}
else
result += temp;
}
break;
case 3:
{
if (temp == "--" + cn.boundary)
item = 1;
if (temp == "--"+ct.boundary)
inbody = true;
if (temp == "--"+ct.boundary+"--")
{
result += mail_body(sbody + temp,ct);
over = true;
}
if (inbody && !over)
{
sbody += temp;
sbody += '\n';
}
}
break;
case 4:
{
if (temp == "--" + cn.boundary + "--")
_over = true;
if (temp == "--" + cn.boundary || _over)
{
/*_out.open((_str = "d:\\" + _contentID + type).c_str());
if (_out)
{
_out.write(_result.c_str(),_result.length());
_out.close();
cout << "content:" << _contentID <<_result.length()<< endl;
}
else
cout << "create file is failed!" << endl;*/
if ((fp = fopen((_str = _contentID + type).c_str(),"wb")) == NULL)
{
MessageBox("file cannot");
exit(1);
}
for (int i = 0;i < _result.length(); i++)
fputc(_result[i],fp);
fclose(fp);
m_NeiQian = (path + ("\\" + _contentID + type)).c_str();
item = 1;
}
else if (ct.encoding == "base64")
{
DecodeBase64(temp,str);
_result += str;
}
else
result += temp;
}
break;
case 5:
{
if (temp == "--" + cn.boundary + "--")
_over = true;
if (temp == "--" + cn.boundary || _over)
{
/*_out.open((_str = "d:\\" + _filename).c_str());
if (_out)
{
_out.write(_result.c_str(),_result.length()) ;
_out.close();
cout << _filename << _str <<endl;
}
else
cout << "create file is failed!" << endl;*/
if ((fp = fopen( _filename.c_str(),"wb")) == NULL)
{
MessageBox("file cannot");
exit(1);
}
for (int i = 0;i < _result.length(); i++)
fputc(_result[i],fp);
fclose(fp);
m_Attachment = (path + ("\\" + _filename)).c_str();
item = 1;
}
else if (ct.encoding == "base64")
{
DecodeBase64(temp,str);
_result += str;
}
else
result += temp;
}
break;
}
}
UpdateData(FALSE);
return result;
}
void CMailView::fill_Maildata(CString &fname)
{
using std::ifstream;
using std::string;
string str;
ifstream fin;
fin.open(fname);
while (getline(fin,str,'\n'))
{
str += "\r\n";
m_mailData += str.c_str();
}
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -