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

📄 smtp.c

📁 在嵌入式linux下发送邮件的SMTP客户端程序。基于mips平台可直接使用
💻 C
📖 第 1 页 / 共 4 页
字号:
		iHour = atoi(strTime);
		
		//如果是指定星期几发送,则需要匹配星期和时间信息, 如果需要每天发送,只需要匹配时间信息	
  	
		if((IsSameWeek(strWeek, weekly) == TTRUE)||(bDay == TTRUE))
		{
			//匹配时间信息			
			if(iHour == hour)
			{
			//需要重发邮件5次,确保邮件发送成功;
			 	for(i=0; i<5; i++)
			 	{
			 		if(SendMailInfo(m_smtp) == TFALSE)
			 		{
			 			printf("Send mail fail and this is %d times!\n", i+1);
			 		}
			 		else
			 		{
			 			bSendMail = TTRUE;
			 			break;
			 		}
			 	}	
	 		}				 	 			 	
		}	
	
		//如果发送邮件成功,则继续等待最小一个小时+15分钟
		if(bSendMail == TTRUE)
		{
			sleep(60*60 + 60*15);
			bSendMail = TFALSE;
		}
		//没有邮件发送,则缺省等待2分钟
		else
		{
			sleep(60*2);
		}	
	}
}

//发送邮件信息
BOOL SendMailInfo(SMTP struMail) 
{	
	//创建套接字
	Create(); 

	//连接SMTP服务器
	if(!ConnectSMTP(struMail))
	{
		printf("Connect fail!\n");
		return TFALSE;
	}

	//发送MAIL命令
	if(!Mail(struMail))
	{
		printf("Mail fail!\n");
		return TFALSE;
	}
	
	//发送TO命令
	if(!SetTo(struMail))
	{
		printf("SetTo fail!\n");
		return TFALSE;
	}
	
	//发送DATA命令
	if(!Data(struMail))
	{
		printf("Data fail!\n");
		return TFALSE;
	}
	
	sleep(1);
	
	//发送QUIT命令
	if(!Disconnect())
	{
		printf("Disconnect fail!\n");
		return TFALSE;
	}
	
	//关闭sokcet通讯连接
	CloseSocket();
	return TTRUE;
	
}

//检查邮件信息的合法性
BOOL CheckMailInfo(SMTP struMail)
{
	BOOL bRet = TTRUE;

	//检查SMTP服务器的信息	
	if(strlen(struMail.m_strSMTPServer) <= 0)
	{
		printf("The Info of SMTP Server is invalid!\n");
		return TFALSE;
	}
	
	//检查FROM的信息
	if(strlen(struMail.m_strSendFrom ) <= 0)
	{
		printf("The Info of From  is invalid!\n");
		return TFALSE;
	}
	
	//检查用户帐号信息	
	if(strlen(struMail.m_strUSERID ) <= 0)
	{
		printf("The Info of USERID  is invalid!\n");
		return TFALSE;
	}
	
	//检查密码信息	
	if(strlen(struMail.m_strPWD) <= 0)
	{
		printf("The Info of PWD  is invalid!\n");
		return TFALSE;
	}
	
	//检查TO的信息
	if(strlen(struMail.m_strSendTo) <= 0)
	{
		printf("The Info of TO  is invalid!\n");
		return TFALSE;
	}	
	
	return bRet;
}

//对指定的字符串进行BASE64编码
void EnCodeString(char* strSoure,char* strEnCode)
{
	char*	pszBody = NULL;
	int		dwSize = 0;
	int		nBodySize = 0;
	char*	pszEncoded = NULL;
	int		nEncodedSize = 0;
	int		nInPos = 0;
	int		nOutPos = 0;
	int		nThisLineSize = 0;
	//编码
	Base64Coder Coder;
	dwSize = strlen(strSoure);
	EncodeBase(&Coder, (const unsigned char *)strSoure, dwSize);	
	
	//形成编码后的发送内容
	pszEncoded = EncodedMessage(Coder);
	nEncodedSize = EncodedMessageSize(Coder);
	nBodySize = nEncodedSize + (((nEncodedSize/76)+1)*2) + 1;
	pszBody = (char*)malloc(nBodySize);
	//检查分配内存是否成功
	if(pszBody == NULL)
	{
		return;
	}
	
	memset(pszBody, 0, nBodySize);
	--nBodySize; 	
	
	while (nInPos < nEncodedSize)
	{
		nThisLineSize = min(nEncodedSize - nInPos, SMTP_MAXLINE);
		memcpy(&pszBody[nOutPos], &pszEncoded[nInPos], nThisLineSize);
		nOutPos += nThisLineSize;
		memcpy(&pszBody[nOutPos], "\r\n", 2);
		nOutPos += 2;
		nInPos += nThisLineSize;
	}
	//以空字符串结束
	pszBody[nOutPos] = '\0'; 
	strcpy(strEnCode, pszBody);

	if(pszBody != NULL)
	{
		free(pszBody);
		pszBody = NULL;
	}
	
}

//根据Quoted printable编码规则对数据进行编码处理
void QuotedPrintableEncode(char* sText, char* strEncode)
{
    int				i = 0;
    char*			sTemp = NULL;	
	char*			sOut = NULL;	
    int				nStartLine = 0;
    int				nLen = 0;
    int				nSize  = 0;
	unsigned char	c;
	BOOL			bInWord = TTRUE;

	nSize = strlen(sText);
	sTemp = (char*)malloc(1024+nSize);
    for (i=0; i<nSize; i++)
    {
        c = (unsigned char) sText[i];
        //对字符进行编码处理
        if (((c >= 33) && (c <= 60)) || ((c >= 62) && (c <= 126)) || (c == '\r') || (c == '\n') || (c == '\t') || (c == ' '))
        {
            //sTemp += char(c);   
            strcat(sTemp, (char *)c);         
        }
        else
        {      
            strcat(sTemp, "=");
            strcat(sTemp, (const char *)(HexDigit((c & 0xF0) >> 4)));
            strcat(sTemp, (const char *)(HexDigit(c & 0x0F)));
        }
    }
    
    //插入分割信息
    i=0;    
    nStartLine = 0;
    nLen = strlen(sTemp);
	sOut = (char*)malloc(1024+nLen);
    for (i=0; i<nLen; i++)
    {
        c = (unsigned char) sTemp[i];
        
        if (c == '\n' || c == '\r' || i == (nLen-1))
        {
            strcat(sOut, substr(sTemp, nStartLine, i-nStartLine+1));
            nStartLine = i+1;
            continue;
        }
        
        if ((i - nStartLine) > SMTP_MAXLINE)
        {
            bInWord = TTRUE;
            while (bInWord)
            {
                bInWord = (BOOL)(!isspace(c) && sTemp[i-2] != '=');
                if (bInWord)
                {
                    --i;
                    c = (unsigned char) sTemp[i];
                }
                
                if (i == nStartLine)
                {
                    i = nStartLine + SMTP_MAXLINE;
                    break;
                }
            }
            
            strcat(sOut, substr(sTemp, nStartLine, i-nStartLine+1));
            strcat(sOut,  "=\r\n");
            nStartLine = i+1;
        }
    }
    
    strEncode = sOut;

	if(sTemp != NULL)
	{
		free(sTemp);
		sTemp = NULL;
	}
    
}


char HexDigit(int nDigit)
{
	if (nDigit < 10)
	{
		return (char) (nDigit + '0');
	}
	else
	{
		return (char) (nDigit - 10 + 'A');
	}
}

//对SUBJECT信息进行编码处理
char* EnSubJectCodeString(char* sText)
{
	//char* strEncode = NULL;
	const char*		strTmp = "=?";
	unsigned char	c;
	char*			sOut = NULL;
	int				nSize = 0;
	BOOL			bTranslationNeeded = TFALSE;
	int				i = 0;
	
	//检查是否需要进行编码
	for (i=0; i<nSize && !bTranslationNeeded; i++)
	{
		c = (unsigned char) sText[i];    
		if(c > 127)
		{
			bTranslationNeeded = TTRUE;    		
		} 
	}
	
	nSize = strlen(sText);
	sOut = (char*)malloc(nSize + 128);
	if(sOut == NULL)
	{
		return NULL;
	}
	memset(sOut, 0, nSize + 128);
	//需要进行编码处理
	if (bTranslationNeeded)
	{
		strcpy(sOut, strTmp);
		strcat(sOut, g_stCharset);
		strcat(sOut, "?q?");		
		QuotedPrintableEncode(sText, sText);	
		strcat(sOut, sText);
		strcat(sOut, "?=");		
	}
	//不需要进行编码处理
	else
	{   
		strcpy(sOut, sText); 
		sOut = sText;
	}
	//strcpy(strEncode, sOut); 
	return sOut;
	
	
}

//对邮件体BODY信息进行编码处理
void EnBodyCodeString(char* sText, char* strEncode)
{
	int				nSize = strlen(sText);
	BOOL			bTranslationNeeded = TFALSE;
	unsigned char	c; 
	char*			sOut = NULL;
	int				i = 0;
	
	//检查是否需要进行编码	
	for (i=0; i<nSize && !bTranslationNeeded; i++)
	{
		c = (unsigned char) sText[i];
		if(c > 127)
		{
			bTranslationNeeded = TTRUE;    		
		}     
	}
	
	
	sOut = (char*)malloc(1024);
	if(sOut == NULL)
	{
		return;
	}

	//需要进行编码处理
	if (bTranslationNeeded)
	{	   
		QuotedPrintableEncode(sText, sOut);	
	}
	//不需要进行编码处理
	else
	{ 		
		strcpy(sOut, sText);
	}
	strcpy(sOut, strEncode);
	
}


BOOL Create()
{
	//win socket 初始化信息
	g_hSocket = socket(AF_INET, SOCK_STREAM, 0);
	if(g_hSocket != -1)
	{
		printf("Create socket successfully!\n");
		return TTRUE;
	}
	else
	{
		printf("Create socket fail!\n");
		return TFALSE;
	}	
}

BOOL ConnectServer( const char* pszHostAddress, int nPort)
{
	//验证主机地址 
	//验证是否socket被创建 
	//远程服务器地址	

	struct sockaddr_in server_address;
	memset(&server_address, 0, sizeof(server_address));
	server_address.sin_family = AF_INET;
	server_address.sin_port = htons((u_short)nPort);
	server_address.sin_addr.s_addr = inet_addr(pszHostAddress);

  	if (server_address.sin_addr.s_addr == INADDR_NONE)
	{	  
		struct hostent* lphost;
		lphost = gethostbyname(pszHostAddress);
		if (lphost != NULL)
		{
			server_address.sin_addr.s_addr = ((struct in_addr*)lphost->h_addr)->s_addr;			
		}
		else
		{			 
			return TFALSE;
		}
	}

  //调用另外一个多态函数connect连接到服务器	
	return ConnectSvr((struct sockaddr *)&server_address, sizeof(server_address));
	

	
}

BOOL ConnectSvr(const struct sockaddr* lpSockAddr, int nSockAddrLen)
{
	int result = -1;
	result = connect(g_hSocket, lpSockAddr, nSockAddrLen);
	if(result == -1)
	{		
		printf("Connect socket fail!\n");
		return TFALSE;
	}
	else
	{		
		return TTRUE;
	}
}

BOOL SendDate(const char* pszBuf, int nBuf)
{
	int result = -1;
	result = send(g_hSocket, pszBuf, nBuf, 0);
	if(result == -1)
	{
		printf("Send socket fail!\n");
		return TFALSE;
	}
	else
	{
		printf("Send Date: %s\n", pszBuf);		
		return TTRUE;
	}
}

int Receive(char* pszBuf, int nBuf)
{
	int result = -1;
	result = recv(g_hSocket, pszBuf, nBuf, 0); 
	if(result == -1)
	{
		printf("Receive socket fail!\n");
		return TFALSE;
	}
	else
	{		
		printf("Reveive Date: %s\n", pszBuf);
		return TTRUE;
	}
}

void CloseSocket()
{
	if (g_hSocket != -1)
	{	
		//close(g_hSocket);
		g_hSocket = -1;
	}
}

/*
BOOL IsReadable(BOOL bReadible)
{
	mytimeval timeout = {0, 0};
	fd_set fds;
	FD_ZERO(&fds);
	FD_SET(g_hSocket, &fds);
	int nStatus = select(0, &fds, NULL, NULL, &timeout);
	if (nStatus == -1)
	{
		return TFALSE;
	}
	else
	{
		if(nStatus == 1)
		{
			bReadible = TTRUE;
		}
		else
		{
			bReadible = TFALSE;    
		}
		return TTRUE;
	}
}
*/
//*****************字符串处理函数********************//
//获取子字符串信息
char*  substr(char* pstrChar, int iBegin, int nLength)
{
	
    //数据合法性检验    
    int iLength = 0;
    char*  strTemp = NULL;
    iLength = strlen(pstrChar);    
	
    strTemp = (char*)malloc(nLength+1);
	if(strTemp == NULL)
	{
		printf("malloc function fail in substr\n!");
		return NULL;
	}
    memset(strTemp, 0, nLength+1);
    if(nLength > (iLength - iBegin) )
    {
        memcpy(strTemp, &pstrChar[iBegin], iLength - iBegin);
    }
    else
    {
        memcpy(strTemp, &pstrChar[iBegin], nLength);
    }
    
    return strTemp;
	
}

//查找指定字符串
int   find(char* pstrChar, const char* strSoure)
{
    int		npos = -1;
	int		i = 0;
    int		ilength = 0;
    int		count = 0;

	ilength = strlen(pstrChar);
    count = strlen(strSoure);
    //如果长度太长
    if( count > ilength)  
    {
        return npos;  
    }               
	
    //从左边开始比较   
    for(i = 0 ; i<ilength; i++)                                              
    { 
        //如果数据相等        
        if( strncmp(&pstrChar[i], strSoure, count) == 0 )   
        {
            return i;  
        }
    }                                                                             
    return npos;    
	
}
//查询函数
int  rfind(char* strData, const char* strSoure)
{
    int			npos = -1;
	char*		strTemp = NULL;
    int			ilength = 0;
    int			count = 0;
    int end_pos = 0;
	
	ilength = strlen(strData);
    count = strlen(strSoure);
    //如果长度太长
    if( count > ilength)  
    {
        return npos;  
    }
	
   
    strTemp = (char*)malloc(ilength +1);
	if(strTemp == NULL)
	{
		return npos; 
	}
    memset(strTemp, 0, ilength +1);
	
    //从右边的第count位开始比较
    end_pos = ilength - count - 1;                          
    
    //从右边数据开始比较
    for( ++end_pos; end_pos-- > 0; )                                              
    { 
        //如果数据相等
        memset(strTemp, 0, ilength +1);
        memcpy(strTemp, &strData[end_pos], count);
        if( strncmp(&strData[end_pos], strSoure, count) == 0 )   
        {
            return end_pos;  
        }
    }                                                                             
    return npos;             
}

⌨️ 快捷键说明

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