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

📄 smtp.c

📁 在三星44B0的板子上实现基于GPRS的SMTP邮件发送
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************************
file  Name  :  	smtp.c
Author      : 	Wenmin 13480112545 wmhnzz@163.com
Time        :	2007-08-06 11:22:20
Compiler    : 	CVEditor 2.1.2.503
Description : 
**********************************************************************************/

#include <cvpos.h> 
#include <GPRSGSMDefine.h>
#include "smtp.h"

void set_smtp_parameters(unsigned char *smtp_server_ip,
                         unsigned char *smtp_server_port,
			 unsigned char *mail_sender,
			 unsigned char *mail_password,
			 unsigned char *mail_reciever,
			 unsigned char *subject_name,
			 unsigned char *file_name)
{
     unsigned char *p;
     unsigned char *str1;
     int i;
     mail_message.ready=1;
//     strcpy(date_str," Wed, 25 Jun 2008 09:21:32 +0800");      
     mail_message.smtp_server_name=&smtp_server_name[0];
     p=&smtp_server_name[0];
     strcpy(p,"gmail");
     mail_message.smtp_server_ip=&smtp_server_ip[0];
     p=&smtp_server_ip[0];
     str1=smtp_server_ip;
     strcpy(p,str1);                                //"220.181.12.15");        
     mail_message.smtp_port=&smtp_port[0];
     p=&smtp_port[0];
     str1=smtp_server_port;
     strcpy(p,str1);                                //"25");     
     mail_message.sender_name=&sender_name[0];
     p=&sender_name[0];
     strcpy(p,"CV7XX0");     
     mail_message.sender_mail_counter=&sender_mail_counter[0];
     p=&sender_mail_counter[0];
     str1=mail_sender;
     strcpy(p,str1);                                 //"wmhnzz@163.com");     
     mail_message.sender_mail_password=&sender_mail_password[0];     
     p=&sender_mail_password[0];
     str1=mail_password;
     strcpy(p,str1);                                 //"******");     
     mail_message.reciever_name=&reciever_name[0];
     p=&reciever_name[0];
     strcpy(p,"Center");  
     mail_message.reciever_mail_counter=&reciever_mail_counter[0];
     p=&reciever_mail_counter[0];
     str1=mail_reciever;
//     strcpy(p,"wmhnzz@163.com");
     strcpy(p,str1);                                 //"wmhnzz@21cn.com");     
     mail_message.subject_name=&subject_name[0];
     p=&subject_name[0];
     str1=subject_name;
     strcpy(p,str1);                                 //"it's a test mail.");
     
         
     mail_message.filename=filename;
     p=filename;
     str1=file_name;
     strcpy(p,str1);                                 //"Test.txt");    
}                    	

//==============================================================
int  ReadData1(unsigned long  TimeOut,
              unsigned char  *pBuffer,
              int            *nLength,
              int command_series)
{
    int readLength=0;
    int i,r_status;
    char j;
    char str1[20];
    unsigned char *p;
    int length;
    p=str1;    
    switch (command_series)
    {
      case ehlo_command_no:
       strcpy(p,"HELP");
       break;
      case auth_login_no:
       strcpy(p,"334 ");
       break;
      case send_username_no:
       strcpy(p,"334 ");      
       break;
      case send_password_no:
       strcpy(p,"successful");
       break;      
      case send_mailfrom_no:
       strcpy(p,"250 ");
       break;
      case send_mailto_no:
       strcpy(p,"250 ");
       break;
      case send_data_no:
       strcpy(p,">.<CR");      
       break;
      case send_mailhead_no:
       strcpy(p,"SEND OK");
       break;
      case send_mailbody_no:
       strcpy(p,"SEND OK");
       break;      
      case send_quit_no:
       strcpy(p,"SEND OK");      
       break;
      case send_ate:
       strcpy(p,"OK");      
       break;
      case r_send_endflag:
       strcpy(p,"250");      
       break;      
                         
    }
    length=strlen(p);
    
    r_status=0;
    while(1)
    {                           
        for(i=0;i<TimeOut;i++)
        {
     	     if(rUTRSTAT1&0x1)   //为了提高接受速度
     	     {
     		j=rURXH1;  //为了提高接受速度
                *pBuffer =j;
				pBuffer++;
                readLength++;
                i=0;
                if (j==str1[r_status])
                 {
                  r_status++;
                  if (r_status==length)
                   {
                     *nLength = readLength;                    
                     return 0;                   
                   }
                 }
                else
                 { 
                  if (j==str1[0]) 
                   r_status=1;
                  else
                   r_status=0;
                 }
     	     }
        }
        return 1;        
    }
}
//================================================================
//实际只做了单字节转换
void inttohex(unsigned char datain,u8 *out)
{
 u8 *s1="0123456789ABCDEF";
 *out=s1[datain/16];
 out++;
 *out=s1[datain&0x0f];
}


//==============================================================
void reverse(char *s) 
{
	char *c;
	int i;
	
	c = s + strlen(s) - 1;
	while(s < c) 
	{
		i = *s;
    	*s++ = *c;
    	*c-- = i;
  	}
}
//===============================================================
char *itoa(int n, char *s) 
{
	int sign;
  	char *ptr;
  	ptr = s;
  	if ((sign = n) < 0) n = -n;
    do 
	{
    	*ptr++ = n % 10 + '0';
  	} 
	while ((n = n / 10) > 0);
  	if (sign < 0) 
		*ptr++ = '-';
  	*ptr = '\0';
  	reverse(s);
  	return s;
}

//==============================================================
//base64 encode program, 3 byte -->4 byte
//conversion regulate: 
//			             byte1 bits                          byte2 bits                     byte3  bits 
//                       ___/               \__              ___/              \___         __/                \__
//                      /                          \            /                          \       /                          \
//                      7  6  5  4  3  2     1 0         7  6  5  4     3  2  1  0       7  6    5  4  3  2  1  0
//                      |                 |      |                      |     |                      |    |                 |
//                      \                 /       \                     /       \                     /     \                /
//                        new byte1              new byte2                new byte3             new byte4
//according to the newbytes,locate the encode code series in base64table
void base64encode(u8 *data_buff,int length)
{
u8 *out;
u8 fragment;
		
	out=base64_databuff;
	for (; length >= 3; length-= 3)
	{
		*out++ = base64table[data_buff[0] >> 2];
		*out++ = base64table[((data_buff[0] << 4) & 0x30) | (data_buff[1] >> 4)];
		*out++ = base64table[((data_buff[1] << 2) & 0x3c) | (data_buff[2] >> 6)];
		*out++ = base64table[data_buff[2] & 0x3f];
		data_buff += 3;
	} 
  	if (length > 0)
	{
		*out++ = base64table[data_buff[0] >> 2];
		fragment = (data_buff[0] << 4) & 0x30;
		if (length > 1)
			fragment |= data_buff[1] >> 4;
		*out++ = base64table[fragment];
		*out++ = (length < 2) ? '=' : base64table[(data_buff[1] << 2) & 0x3c];
		*out++ = '=';
	}			  
	*out = '\0';
}

//==============================================================
//send BPS of communication as bps_bps_data
//mode =0, disenable =1 enable
int send_bps(int bps_data)
{
	unsigned char command_str[50]={"AT+IPR="};
	unsigned int command_length;
	unsigned char converter[50];
         
//	strcpy(command_str,"AT+IPR=");
	strcat(command_str,itoa(bps_data,converter));
	strcat(command_str,enter);
	command_length = strlen(command_str);
	return smtp_senddata(command_str,command_length,send_ate);
}

//==============================================================
//send ate0,enable or disenable command send back 
//mode =0, disenable =1 enable
int send_atecmd(int mode)
{
	unsigned char command_str[50];
	unsigned int command_length;

	strcpy(command_str,"ATE");
	strcat(command_str,mode==0? "0":"1");
	strcat(command_str,enter);
	command_length = strlen(command_str);
	return smtp_command(command_str,command_length,send_ate);
}
//=================================================================
int close_smtp(void)
{
	unsigned char command_str[50];
	unsigned int command_length;
	
	strcpy(command_str,"AT+CIPCLOSE");
	strcat(command_str,enter);
	command_length = strlen(command_str);
	return smtp_command(command_str,command_length,send_ate);	
}

   
//=================================================================
//esmtp protocal,for   return  0 OK , 
int send_ehlo(void)
{
	unsigned char command_str[50];
	unsigned int command_length;
//        unsigned char *p;
        
	strcpy(command_str,mail_field[0]);
	strcat(command_str,mail_message.reciever_name);
	strcat(command_str,enter);
	command_length = strlen(command_str);
	return smtp_senddata(command_str,command_length,ehlo_command_no);
}


//=================================================================
//login emstp server,  if success program can carry out the next step,next step is to verify the username
int auth_login(void)
{
	
	unsigned char command_str[50];
	unsigned int command_length;
 
	strcpy(command_str,mail_field[1]);
	strcat(command_str,enter);
	command_length = strlen(command_str);
	return smtp_senddata(command_str,command_length,auth_login_no);
}


//=============================================================
//verify the counter name,if success pro can carry out  next step,next step is to verify the password
int send_username(unsigned char *user_name)
{
 	unsigned char command_str[50];
 	unsigned int command_length;
 	u8 *p;
 	p=base64_databuff; 
	strcpy(command_str,user_name);
 	command_length = strlen(user_name);
	base64encode(command_str,command_length);
	strcat(p,enter);
 	return smtp_senddata(base64_databuff,strlen(p),send_username_no); 
}


//==============================================================
//verify the count name,if success pro can carry out  next step
int send_password(unsigned char *user_password)
{
 	unsigned char command_str[50];
 	unsigned int command_length;
 	u8 *p;
 	p=base64_databuff; 
 	strcpy(command_str,user_password);
 	command_length = strlen(user_password);
 	base64encode(command_str,command_length);
	strcat(p,enter);          	
 	return smtp_senddata(base64_databuff,strlen(p),send_password_no); 
}


// =============================================================    
//pro:send_mailfrom
//FIELD  FORMAT
//MAIL FROM:<sender's counter name> 
int send_mailfrom(void)
{
	unsigned char command_str[50];
	unsigned int command_length;	
	
	strcpy(command_str,mail_field[2]);
	strcat(command_str,mail_message.sender_mail_counter);
	strcat(command_str,">");
	strcat(command_str,enter);	
	command_length = strlen(command_str);
	return smtp_senddata(command_str,command_length,send_mailfrom_no); 
}


//==============================================================
//send reciever's counter name
int send_rcpt(void)
{
	unsigned char command_str[50];
	unsigned int command_length;	
	
	strcpy(command_str,mail_field[3]);
	strcat(command_str,mail_message.reciever_mail_counter);
	strcat(command_str,">");
	strcat(command_str,enter);	
	command_length = strlen(command_str);
	return smtp_senddata(command_str,command_length,send_mailto_no); 
}


//==============================================================
//send data command ,indicate that following datas are mail's content
int send_datacommand(void)
{
	unsigned char command_str[50];	
	unsigned int command_length;
		
	strcpy(command_str,mail_field[4]);
	strcat(command_str,enter);
	command_length = strlen(command_str);

	return smtp_senddata(command_str,command_length,send_data_no); 	

}

//==============================================================
int send_rst()
{
}

int send_databody(void)
{
	u8 send_tempbuff[500];
	u8 *p;
	int head_length;
	int data_length;
	p=send_tempbuff;
	strcpy(p,"Date: Wed, 18 Jun 2008 09:21:32 +0800");
        strcat(p,enter);
        strcat(p,"From: \"=?GB2312?B?zsTD9A==?=\" <wen@cv-it.com>");
        strcat(p,enter);
        strcat(p,"To: \"wmhnzz\" <wmhnzz@163.com>");
        strcat(p,enter);
        strcat(p,"Subject: abc");
        strcat(p,enter);
        strcat(p,"Message-ID: <200806180921319065572@cv-it.com>");
        strcat(p,enter);
        strcat(p,"Mime-Version: 1.0");
        strcat(p,enter);
        strcat(p,"Content-Type: multipart/mixed;");
        strcat(p,enter);
        strcat(p,"boundary=\"=====001_Dragon661100546705_=====\"");
        strcat(p,enter);
        strcat(p,enter);
    	strcat(p,"This is a multi-part message in MIME format.");
        strcat(p,enter);
        strcat(p,enter);
        strcat(p,"--=====001_Dragon661100546705_=====");
        strcat(p,enter);
        strcat(p,"Content-Type: application/octet-stream;");
        strcat(p,enter);
        strcat(p,"	name=\"test.txt\"");
        strcat(p,enter);
        strcat(p,"Content-Transfer-Encoding: base64");
        strcat(p,enter);
        strcat(p,"Content-Disposition: attachment;");
        strcat(p,enter);

⌨️ 快捷键说明

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