📄 smtp.c
字号:
strcat(p," filename=\"test.txt\"");
strcat(p,enter);
strcat(p,enter);
strcat(p,"YWRzZg==");
strcat(p,enter);
strcat(p,enter);
strcat(p,"--=====001_Dragon661100546705_=====--");
strcat(p,enter);
strcat(p,enter);
strcat(p,".");
strcat(p,enter);
data_length=strlen(p);
return smtp_senddata(p,data_length,send_mailbody_no);
}
//==============================================================
//send mail's head
//include datetime,sender's name & mail_counter,reciver's name & mail_counter,subject of the mail
//mime version,some items are optional, for intance ,message-id, x-mailer,etc.
int send_mailhead()
{
// u8 send_tempbuff[500];
u8 *p;
u8 temp_date[100];
u8 *temp_getstr;
u8 date_str[30];
int head_length;
p=temp_databuff;
strcpy(p,mail_field[5]); //date
switch (getweek())
{
case 0:
strcpy(temp_date,"Sun");
break;
case 1:
strcpy(temp_date,"Mon");
break;
case 2:
strcpy(temp_date,"Tue");
break;
case 3:
strcpy(temp_date,"Wed");
break;
case 4:
strcpy(temp_date,"Thu");
break;
case 5:
strcpy(temp_date,"Fri");
break;
case 6:
strcpy(temp_date,"Sat");
break;
}
strcat(temp_date,", ");
getdate(date_str);
temp_getstr[0]=date_str[8]; //get string of day
temp_getstr[1]=date_str[9];
temp_getstr[2]=0;
strcat(temp_date,temp_getstr);
strcat(temp_date," ");
temp_getstr[0]=date_str[5]; //get string of month
temp_getstr[1]=date_str[6];
temp_getstr[2]=0;
if (strcmp(temp_getstr,"01")==0) //replace by english
strcat(temp_date,"Jan");
else if (strcmp(temp_getstr,"02")==0)
strcat(temp_date,"Feb");
else if (strcmp(temp_getstr,"03")==0)
strcat(temp_date,"Mar");
else if (strcmp(temp_getstr,"04")==0)
strcat(temp_date,"Apr");
else if (strcmp(temp_getstr,"05")==0)
strcat(temp_date,"May");
else if (strcmp(temp_getstr,"06")==0)
strcat(temp_date,"Jun");
else if (strcmp(temp_getstr,"07")==0)
strcat(temp_date,"Jul");
else if (strcmp(temp_getstr,"08")==0)
strcat(temp_date,"Aug");
else if (strcmp(temp_getstr,"09")==0)
strcat(temp_date,"Sep");
else if (strcmp(temp_getstr,"10")==0)
strcat(temp_date,"Oct");
else if (strcmp(temp_getstr,"11")==0)
strcat(temp_date,"Nov");
else if (strcmp(temp_getstr,"12")==0)
strcat(temp_date,"Dec");
strcat(temp_date," ");
date_str[4]=0; //cut the string,
strcat(temp_date,date_str); //
strcat(temp_date," ");
gettime(date_str);
date_str[2]=':';
date_str[5]=':';
date_str[8]=0;
strcat(temp_date,date_str);
strcat(temp_date," +0800");
strcat(p,temp_date);
strcat(p,enter);
strcat(p,mail_field[6]);
strcat(p," \""); //from
strcat(p,mail_message.sender_name); //sender name
strcat(p,"\" ");
strcat(p,"<");
strcat(p,mail_message.sender_mail_counter); //counter
strcat(p,">");
strcat(p,enter);
strcat(p,mail_field[7]); //to
strcat(p," \"");
strcat(p,mail_message.reciever_name); //reciever name
strcat(p,"\" ");
strcat(p,"<");
strcat(p,mail_message.reciever_mail_counter); //reciever counter
strcat(p,">");
strcat(p,enter);
strcat(p,mail_field[8]); //subject
strcat(p,mail_message.subject_name);
strcat(p,enter);
strcat(p,mail_field[9]); //x-mailer
strcat(p,enter);
strcat(p,"Message-ID: <200806180921319065572@cv-it.com>");
strcat(p,enter);
strcat(p,mail_field[11]); //mime-version
strcat(p,enter);
strcat(p,mail_field[12]); //content type
strcat(p,enter);
strcat(p,mail_field[13]); //boundary
strcat(p,enter);
strcat(p,enter);
strcat(p,mail_field[14]); //mime mail
strcat(p,enter);
strcat(p,enter);
strcat(p,mail_field[15]); //boundary
strcat(p,enter);
strcat(p,mail_field[16]); //send attachment file type
strcat(p,enter);
strcat(p,mail_field[17]); // name
strcat(p,"\"");
//test
strcat(p,mail_message.filename);
strcat(p,"\"");
strcat(p,enter);
strcat(p,mail_field[18]); //base64 encode
strcat(p,enter);
strcat(p,mail_field[19]); //attachment position
strcat(p,enter);
strcat(p,mail_field[20]); //filename
strcat(p,"\"");
strcat(p,mail_message.filename);
strcat(p,"\"");
strcat(p,enter);
strcat(p,enter);
head_length=strlen(p);
return smtp_senddata(p,head_length,send_mailhead_no); //head ok
}
//==============================================================
//send mail'body, the max length is 1000, if it be overflow ,can repeat the step
// the mailbody's data are encoded by base64 mode,
int send_mailbody(unsigned char *maildatas,int length,int end_flag)
{
unsigned char *p;
int data_length;
// int i;
data_length=length;
p=base64_databuff;
base64encode(maildatas,length); //base64 encode
if (end_flag==1)
{
strcat(p,enter); //send boundary
strcat(p,enter);
strcat(p,mail_field[15]);
strcat(p,"--");
strcat(p,enter);
strcat(p,enter); //if it's the last packet, add endflag at the end
strcat(p,".");
strcat(p,enter);
// data_length=strlen(p);
// strcat(p,enter);
}
data_length=strlen(p);
// printf("\n %d",data_length); //get the length of all data
return smtp_senddata(p,data_length,send_mailbody_no); //send out
}
/////////////////////////////////////////////////////////////////////////////////
int send_endflag(void)
{
unsigned char *p;
int data_length;
p=base64_databuff;
strcpy(p,enter);
strcat(p,enter);
strcat(p,mail_field[15]);
strcat(p,"--");
strcat(p,enter);
strcat(p,enter); //if it's the last packet, add endflag at the end
strcat(p,".");
strcat(p,enter);
data_length=strlen(p);
// printf("\n %d",data_length); //get the length of all data
return smtp_senddata(p,data_length,r_send_endflag); //send out
}
//==============================================================
int send_quit(void)
{
unsigned char command_str[50];
int length;
strcpy(command_str,mail_field[21]);
strcat(command_str,enter);
length = strlen(command_str);
return smtp_senddata(command_str,length,send_quit_no);
}
int check_login(void)
{
int i;
unsigned char p[6];
strcpy(p,"LOGIN");
if (m_RLen<5)
return 1;
m_RData[m_RLen]=0;
if (strstr(m_RData,p))
return 0;
else
return 1;
}
//==============================================================
int send_cmds_heads(void)
{
int i;
if (send_ehlo())
return 1;
//for(i=0;i<m_RLen;i++)
// m_RData[150]=0;
// printf(m_RData);
i=check_login();
if (i)
goto mail_from_send;
if (auth_login()!=0)
return 5; //
if (send_username(mail_message.sender_mail_counter))
return 6;
if (send_password(mail_message.sender_mail_password))
return 7;
mail_from_send:
if (send_mailfrom())
return 8;
if (send_rcpt())
return 9;
if (send_datacommand())
return 10;
if (send_mailhead())
return 11;
return 0;
}
//==============================================================
//char *SendData 发送数据,[注:数据长度小于等于1024 BYTE]
//command_series
//=1 ehlo =2 auth login =3 username =4 sendpassword =5
//smtp_senddata1
int smtp_senddata(char *SendData,int length,int command_series)
{
unsigned char AT_CIPSEND[] = {"AT+CIPSEND"}; //..发送短信
int WLen= strlen(AT_CIPSEND);
int i;
//unsigned SendDataLength = strlen(SendData);
// if(length>1000)
// return 1;
InitRD();
WriteData(AT_CIPSEND,WLen);
Write_spicial_char(0x0d); //<<0>>
// Write_spicial_char(0x0a); //<<0>>
ReadData(20*TIMEOUT,m_RData,&m_RLen);
if(AnalyseReturn(7)==0)
{
InitRD();
WriteData(SendData,length);//..
Write_spicial_char(0x1a); //<<0>>
i=100*TIMEOUT;
// if (command_series==send_mailbody_no)
// {
return(ReadData1(1200*TIMEOUT,m_RData,&m_RLen,command_series));
}
else
return 1;
}
//==============================================================
//send_command
int smtp_command(char *SendData,int length,int command_series)
{
InitRD();
WriteData(SendData,length);//..
ReadData(100*TIMEOUT,m_RData,&m_RLen);
return mail_return(command_series);
}
int smtp_connect(void)
{
// u8 *command_str="AT+CIPSTART=\"TCP\",\"";
u8 *command_str="AT+CIPSTART=\"TCP\",";
u8 *p;
int i;
int length;
p=temp_databuff;
strcpy(p,command_str);
strcat(p,"\"");
strcat(p,mail_message.smtp_server_ip);
strcat(p,"\",");
strcat(p,"\"25\"\n");
InitRD();
length=strlen(p);
WriteData(p,length);
ReadData(20*TIMEOUT,m_RData,&m_RLen);
if (find_ackdata(strOK)==0)
{
ReadDataT2(1000*TIMEOUT,TIMEOUT,m_RData,&m_RLen);
return mail_return(command_connect);
}
}
//==============================================================
//After send a command ,find corresponding string in ackdata,
int find_ackdata(unsigned char *p)
{
unsigned char *ptr;
InitRDC();
ptr = strstr(m_RData, p);
if(ptr!=0)
return 0;
else
return 1;
}
//==============================================================
//after sending the smtp command,deal with the return data to analyse wheather the commands
//have been executed correctly
//command_series is the identifier of the commands,
int mail_return(int command_series)
{
unsigned char tmpdata[20];
unsigned char *p;
p=tmpdata;
switch (command_series)
{
case 1: //ehlo command
strcpy(p,str250);
return find_ackdata(p);
break;
case 2: //auth login command
strcpy(p,str334name);
return find_ackdata(p); //if ok, then re,omd to send username
break;
case 3: //send username command
strcpy(p,str334password);
return find_ackdata(p); //if ok, then re,omd to send password
break;
case 4: //send password command
strcpy(p,str235);
return find_ackdata(p); //if ok, return "235" indicate auth login success!
break;
case 5: //send mailfrom command
strcpy(p,str250);
return find_ackdata(p);
break;
case 6: //send mailto command
strcpy(p,str250);
return find_ackdata(p);
break;
case 7: //send data command
strcpy(p,str354);
return find_ackdata(p);
break;
case 8: //send mailhead, can send mailbody seriesly
break;
case 9: //send mailbody & mailend
strcpy(p,strOK);
return find_ackdata(p);
break;
case 10: //send quit
strcpy(p,str221); //check 221, here,all step is ok ,the mail has been send out,
return find_ackdata(p);
break;
case 11: //send ATE
strcpy(p,strOK); //check 221, here,all step is ok ,the mail has been send out,
return find_ackdata(p);
break;
case 12: //send ATE
strcpy(p,str220); //check 221, here,all step is ok ,the mail has been send out,
return find_ackdata(p);
break;
default:break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -