⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.cpp

📁 邮件内容呼,邮件内容提取至寻呼机上,数据库操作,邮件内容提取
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "pop3.h"
#include "mail.h"
#include "CCustom.h"
#include "stdio.h"

#include <afxdb.h>

BOOL g_bExited = TRUE;
BOOL g_bTerminate = FALSE;

char Base64[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0,
0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0};


Mail::Mail()
{   
	m_nState = STATE_DISCONNECT;
	CString strConnect;
	char dbName[20],dbUser[20],dbPswd[20];
	GetPrivateProfileString("database","DSN","",dbName,20,".//trans.ini");
    GetPrivateProfileString("database","UID","",dbUser,20,".//trans.ini");
    GetPrivateProfileString("database","PWD","",dbPswd,20,".//trans.ini");
    
	strConnect.Format("DSN=%s;UID=%s;PWd=%s",dbName,dbUser,dbPswd);
	if(m_db.OpenEx(strConnect,CDatabase::noOdbcDialog))
	{
		if (!m_accountinfo.Create(m_db.m_hdbc,"s_email_addr"))
		{
			AfxMessageBox("打开s_email_addr.dbf失败! ");
			exit(0);
		}
		if (!m_filterinfo.Create(m_db.m_hdbc,"s_email_filter"))
		{
			AfxMessageBox("打开s_email_filter.dbf失败!");
			exit(0);
		}
		if (!m_switchinfo.Create(m_db.m_hdbc,"s_email"))
		{
			AfxMessageBox("打开s_email.dbf失败! ");
			exit(0);
		}
		if (!m_mailinfo.Create(m_db.m_hdbc,"angbaocall"))
		{
			AfxMessageBox("打开angbaocall.dbf失败!");
			exit(0);
		}
	}
	else
	{
		AfxMessageBox("连接数据库失败!");
		exit(0);
	}

}


Mail::~Mail()
{
	Close();
}

void Mail::Create(char *strServer, char *strUser, char *strPass)
{
	memset(m_strServer, 0, sizeof(m_strServer));
	strcpy(m_strServer, strServer);
	memset(m_strUser, 0, sizeof(m_strUser));
	strcpy(m_strUser, strUser);
    memset(m_strPass, 0, sizeof(m_strPass));
    strcpy(m_strPass, strPass);
}

void Mail::Close()
{
	//关闭数据库和表
	m_accountinfo.Close();
	m_mailinfo.Close();
	m_switchinfo.Close();
	m_filterinfo.Close();
	m_db.Close();
	//断开pop3连接,释放socket
}


void Mail::DecodeBase64(char *des, char *src)
{
	BYTE cTempHigh, cTempLow, cFirst6Bits, cSecond6Bits, cThird6Bits, cFourth6Bits;
	int i = 0;
	int j = 0;
	int length = strlen(src);
	for(i = 0, j = 0; i < length;j = j + 3)
	{
		cFirst6Bits = Base64[(int)(src[i++])];
		cSecond6Bits = Base64[(int)(src[i++])];
		cThird6Bits = Base64[(int)(src[i++])];
		cFourth6Bits = Base64[(int)(src[i++])];
		cTempHigh = cFirst6Bits << 2;
		cTempLow = cSecond6Bits >> 4;
		des[j] = cTempHigh | cTempLow;
		cTempHigh = cSecond6Bits << 4;
		cTempLow = cThird6Bits >> 2;
		des[j + 1] = cTempHigh | cTempLow;
		cTempHigh = cThird6Bits << 6;
		des[j + 2] = cTempHigh | cFourth6Bits;
	}
	des[j] = 0;
}

void Mail::DecodeQP(char *des, char *src)
{
	int length;
	int flag1 = 0;
	int flag2 = 0;
	int i, j;
	char gg[3];
	length = strlen(src);
	for(i = 0, j = 0; i < length; i++)
	{
		if(src[i] == '=')
		{
			flag1 = 1;
			continue;
		}
		if(flag1 == 1)
		{
			gg[0] = src[i];
			flag1 = 0;
			flag2 = 1;
			continue;
		}
		if(flag2 == 1)
		{
			gg[1] = src[i];
			gg[2] = 0;
			flag1 = 0;
			flag2 = 0;
			sscanf(gg, "%02x", des + j);
			j++;
			continue;
		}
		des[j++] = src[i];
	}
}


BOOL Mail::DecodeSub(char *des, const char *src)
{
	char buffer[256];
	char field[256];
	char *temptr;
//	char *ptr;
	int i = 0;
	memset(buffer, 0, 256);
	strcpy(buffer, src);
	// gb2312
	if((temptr = strstr(buffer, "B?")) || (temptr = strstr(buffer,"b?")))
	{
		if(temptr = strstr(buffer, "=?"))
		    strncpy(des, buffer + 8, temptr - buffer - 8);
		temptr = buffer;
		for(i = 1; i < strlen(buffer); i++)
		{
			  if((buffer[i - 1] == 'B' && buffer[i] == '?') || (buffer[i - 1] == 'b' && buffer[i] == '?'))
			  {
				 temptr = temptr + 2;
				 break;
			  }
			  else
			  	  temptr = temptr + 1;
		}
		for(i = 0; i < strlen(temptr); i++)
		{
			if(temptr[i] == '?' && temptr[i + 1] == '=')
			{
				temptr[i] = 0;
				break;
			}
		}
		memset(field, 0, sizeof(field));
		DecodeBase64(field, temptr);
	}
			//ISO-8859-1
	else if((temptr = strstr(buffer, "Q?")) || (temptr = strstr(buffer,"q?")))
	{
		if(temptr = strstr(buffer, "=?"))
		    strncpy(des, buffer + 8, temptr - buffer - 8);
		temptr = buffer;
		int n = strlen(buffer);
		for(i = 1; i < strlen(buffer); i++)
		{
			if((buffer[i - 1] == 'Q' && buffer[i] == '?') || (buffer[i - 1] == 'q' && buffer[i] == '?'))
			{
				temptr = temptr + 2;
				break;
			}
			else
				temptr = temptr + 1;
		}
		n = strlen(temptr);
		for(i = 0; i < strlen(temptr); i++)
		{
			if(temptr[i] == '?' && temptr[i + 1] == '=')
			{
				temptr[i] = 0;
				break;
			}
		}
		memset(field, 0, sizeof(field));
		DecodeQP(field, temptr);
	}
	else
	{
		memset(field, 0, sizeof(field));
		strcpy(field, buffer + 8);
	}
	strcat(des,field);
	return TRUE;
}

BOOL Mail::DecodeLetter(char* m_message)
{
//	FILE *fp;
//	fp = fopen("c:\\temp\\mail.txt", "ab");

	char *ptr;
	char *beforeptr;
	char *tmpptr;
	char attach[32];
	char eachline[256];
	char buffer[256];
	char tempcontent[512];
	char boundary[256];
	int ctype = 0;
	int ctransfer;
	int nTmplen;
	BOOL isattach = FALSE;
	BOOL flag = TRUE;
    BOOL isbody = FALSE;
	BOOL haveattach = FALSE;
	int m_maxContent;
	int m_maxSubject;
	memset(tempcontent, 0, 512);
	m_maxContent = GetPrivateProfileInt("maxlength","content",300,".//trans.ini");
	m_maxSubject = GetPrivateProfileInt("maxlength","subject",100,".//trans.ini");
    if(m_maxContent > 300)
		m_maxContent = 300;
	if(m_maxSubject > 100)
		m_maxSubject = 100;
	memset(attach, 0, 32);

	beforeptr = m_message;
	if(beforeptr == NULL)
		return FALSE;
	ptr = strstr(beforeptr, "\r\n");
	if(!ptr)
		return FALSE;
    memset(eachline, '\0', 256);
	nTmplen = ptr - beforeptr;
	strncpy(eachline, beforeptr, nTmplen);
	beforeptr = ptr + 2;
    if(eachline == NULL)
		return FALSE;
	strcpy(letterto, eachline + 5);
	tmpptr = strstr(letterto, "@");
	if(tmpptr == NULL)
		return FALSE;
	tmpptr[0] = '\0';
	strcat(letterto, "@gnetpri.com");

//	strcpy(letterto, "wlj@gnetpri.com");
	if (!m_accountinfo.GetBP(letterto))
	{
		strcpy(content, "该用户没有设置该服务!");
		return TRUE;
	}
	
	if (!m_switchinfo.GetSwitch(m_accountinfo.szUid))
	{
		strcpy(content, "没有该用户信息!");
		return TRUE;
	}

	if (!m_switchinfo.m_mailswitch) {
		strcpy(content, "该用户设置该服务关闭!");
		return TRUE;
	}

	if (m_switchinfo.m_mailfilter) {
		strcpy(content, "该用户设置该地址过滤!");
		if (!m_filterinfo.GetFilter(m_accountinfo.szUid,letterfrom))
			return FALSE;
		if (m_filterinfo.m_filter) {
			return TRUE;
		}
	}

	if (m_switchinfo.m_format==0)  {		//数字机
			GetPrivateProfileString("type","digital","",content,10,".//trans.ini");
	    	if(!m_mailinfo.InsertInfo((char*)m_accountinfo.szUid,m_switchinfo.m_mailservice,m_switchinfo.m_station,m_switchinfo.m_zone,content))
				return FALSE;
			return TRUE;
		}
		
	// 邮件到达通知
	
	// 邮件内容呼

	if ((m_switchinfo.m_mailservice==0)||(m_switchinfo.m_mailservice==1)) 
	{
		char *tmp;
		//处理头信息
		ctype = 1;
		ctransfer = 0;
		while(TRUE)
		{

			if(beforeptr == NULL)
				return TRUE;
			if(beforeptr[0] == '\0')
				break;
			if((beforeptr[0] == '\r') && (beforeptr[1] == '\n'))
			{
				beforeptr = beforeptr + 2;
				if(ctype == 1)
					isbody = TRUE;
				break;
			}
			ptr = strstr(beforeptr, "\r\n");
			if(!ptr)
				return TRUE;
			memset(eachline, '\0', 256);
			nTmplen = ptr - beforeptr;
			if(nTmplen >= 256)
				nTmplen = 255;
			strncpy(eachline, beforeptr, nTmplen);
			beforeptr = ptr + 2;
			if(eachline == NULL)
				return TRUE;
			if(strstr(eachline, "Content-Type"))
			{
				char *tmp = strlwr(eachline);
				if(strstr(tmp, "text/plain"))
				    ctype = 1;
				else if(strstr(tmp, "text/html"))
					ctype = 2;
				else if(strstr(tmp, "multipart/"))
				{
					if(strstr(tmp, "multipart/mixed"))
						haveattach = TRUE;
					ctype = 3;
				}
				else ctype = 4;
			}
			if(strstr(eachline, "Content-Transfer-Encoding"))
			{
				char *tmp = strlwr(eachline);
				if(strstr(tmp, "base64"))
					ctransfer = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -