?? maillip.c
字號(hào):
/***************************************************************文件名:maillip.c 功能:采集數(shù)據(jù)的傳輸完成日期:2004.7.18 **************************************************************/#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <time.h>#include <sys/time.h>#include <stdlib.h>#include <stdarg.h> #include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <netdb.h>#include <errno.h>#include <malloc.h>#include "mm.h"/***************************************************************base64編碼程序***************************************************************/unsigned char *encode(unsigned char *src,int srclen){ int n,buflen,i,j; int pading=0; unsigned char *buf; unsigned char *dst; buf=src; buflen=n=srclen; if(n%3!=0) /* pad with '=' by using a temp buffer */ { pading=1; buflen=n+3-n%3; buf=malloc(buflen+1); memset(buf,0,(buflen+1)); memcpy(buf,src,n); for(i=0;i<3-n%3;i++) buf[n+i]='='; } dst=malloc(buflen*4/3+1); memset(dst,0,(buflen*4/3+1)); for(i=0,j=0;i<buflen;i+=3,j+=4) { dst[j]=(buf[i]&0xFC)>>2; dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4); dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6); dst[j+3]=buf[i+2]&0x3F; } for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */ dst[i]=ch64[dst[i]]; if(pading) free(buf); return dst;}/***************************************************************base64解碼程序***************************************************************/unsigned char *decode(unsigned char *src){ int n; int i=0; int j=0; int k=0; int length; unsigned char *p; unsigned char *dst; length=strlen(ch64); n=strlen(src); for(i=0;i<n;i++) { p=strchr(ch64,src[i]); if(!p) { break; } for(k=0;k<length;k++) { if(src[i]==ch64[k]) { src[i]=k; break; } } } dst=malloc(n*3/4+1); memset(dst,0,(n*3/4+1)); for(i=0,j=0;i<n;i+=4,j+=3) { dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4); dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2); dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3]; } return dst;}/***************************************************************郵件地址空間分配及賦值***************************************************************/char * smtp_fill_in_addresses(char * source_string) { char * retval; retval = (char *)malloc(strlen(source_string)+1); if (retval != NULL) { //copy source into variable space! strcpy(retval,source_string); } return(retval); } /***************************************************************清楚郵件信息***************************************************************/ void smtp_clear(SMTP * smtp) { if (smtp == NULL) return; smtp->strSmtpServer = ""; smtp->strMessageBody = ""; smtp->strSubject = ""; //this is the e-mail address of the sender smtp->strSenderUserId = ""; smtp->strFullSenderUserId = ""; //Desitination addresses smtp->strDestUserIds = ""; smtp->strFullDestUserIds = ""; smtp->strRplyPathUserId = ""; //this is who the return receipt goes back to smtp->strRrcptUserId = ""; //override the name of the mailing function with this field smtp->strMailerName = ""; //add a comment here if necessary smtp->strMsgComment = ""; } /***************************************************************輸出郵件的相關(guān)信息***************************************************************/ void smtp_print(SMTP * smtp) { smtp->strSmtpServer; smtp->strMessageBody; smtp->strSubject; printf("SenderUserId: %s\n\r",smtp->strSenderUserId); printf("DestUserIds: %s \n\r",smtp->strDestUserIds); smtp->strRrcptUserId; }/***************************************************************郵件信息的格式和發(fā)送、發(fā)送郵件前郵件信息空間地址分配及賦值發(fā)送成功返回1,失敗返回0***************************************************************/int smtp_send_mail (SMTP *smtp, int show_progress) { int iCnt,x,retval; char strRetBuff[513]; char *strRcptUserIds; /* The following tells the mail server who to send it to. */ iCnt = 0; strRcptUserIds = (char *) malloc (strlen (smtp->strDestUserIds) +1); if (strRcptUserIds == NULL) { return(-100); } //concatenate the destuserids, the ccuserids, and the bccuserids sprintf (strRcptUserIds, "%s", smtp->strDestUserIds); while (1) { getstrfld (strRcptUserIds, iCnt++, 0, ",;", strRetBuff); if (*strRetBuff) { if (show_progress) printf("Recipient %d %s\n\r",iCnt,strRetBuff); } else break; } iCnt--; free (strRcptUserIds); for (x = 1;x<=iCnt;x++) { //send a message for each recipient... retval = smtp_send_mail_func(smtp,x,show_progress); //調(diào)用發(fā)送函數(shù) if (retval) break; } return (retval); } /***************************************************************發(fā)送函數(shù)***************************************************************/ int smtp_send_mail_func (SMTP *smtp,int recipient_index, int show_progress) { int iCnt; int iSocket; char strOut[514], strRetBuff[513]; char computer[256]; char *strRcptUserIds; int rply; iSocket = smtp_connect(smtp); if (iSocket <0) return (-1); if (getreply (iSocket, smtp) > 400 || iSocket < 1) return -1; /* Format a SMTP meassage header. */ /* Just say hello to the mail server. */ gethostname(&computer[0],255); //get back the name of this computer xstrcpy (strOut, "HELO ", computer, "\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket, smtp) > 400) return -2; /* Tell the mail server who the message is from. */ xstrcpy (strOut, "MAIL FROM:<", smtp->strSenderUserId, ">\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket, smtp) > 400) return -3; strRcptUserIds = (char *) malloc (strlen (smtp->strDestUserIds) +1); if (strRcptUserIds == NULL) { return(-100); } //concatenate the destuserids, the ccuserids, and the bccuserids sprintf (strRcptUserIds, "%s", smtp->strDestUserIds); /* The following tells the mail server who to send it to. */ //Loop for each recipient iCnt = 0; while (1) { getstrfld (strRcptUserIds, iCnt++, 0, ",;", strRetBuff); if (*strRetBuff) { if (recipient_index == iCnt) { xstrcpy (strOut, "RCPT TO:<", strRetBuff, ">\r\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket, smtp) > 400) return -4; } //end if matching recipient index } else break; } free (strRcptUserIds); /* Now give it the Subject and the message to send. */ smtp_send_data (iSocket, "DATA\r\n"); if (getreply (iSocket, smtp) > 400) return -5; /* Set the date and time of the message. */ xstrcpy ( strOut, "Date: ", encode_mime_time (date_now (), time_now ()), " \r\n", NULL ); smtp_send_data (iSocket, strOut); /* The following shows all who it was sent to. */ if ( smtp->strFullDestUserIds && *smtp->strFullDestUserIds ) { replacechrswith (smtp->strFullDestUserIds, ";", ','); xstrcpy (strOut, "To: ", smtp->strFullDestUserIds, "\r\n", NULL); } else { replacechrswith (smtp->strDestUserIds, ";", ','); xstrcpy (strOut, "To: ", smtp->strDestUserIds, "\r\n", NULL); } //end if FullDestUserIds not Null // Set up the Reply-To path. //If there is no setting for the reply-to, stick in the e-mail address of //the sender. if (!smtp->strRplyPathUserId || !*smtp->strRplyPathUserId) smtp->strRplyPathUserId = smtp->strSenderUserId; //if the reply address is not surrounded by <>, add them. if ( (strstr( smtp->strRplyPathUserId, "<" ) != (char *)NULL) && (strstr( smtp->strRplyPathUserId, ">" ) != (char *)NULL) ) { xstrcat (strOut, "Reply-To:", smtp->strRplyPathUserId, "\r\n", NULL); } else { xstrcat (strOut, "Reply-To:<", smtp->strRplyPathUserId, ">\r\n", NULL); } //end if RplyPathUserId has <> //indicate the sender of the message. //If we have a FullSenderUserID, us it, otherwise use the SenderUserId if ( smtp->strFullSenderUserId && *smtp->strFullSenderUserId ) { xstrcat (strOut, "Sender:", smtp->strFullSenderUserId, "\r\n", NULL); xstrcat (strOut, "From:", smtp->strFullSenderUserId, "\r\n", NULL); } else { xstrcat (strOut, "Sender:", smtp->strSenderUserId, "\r\n", NULL); xstrcat (strOut, "From:", smtp->strSenderUserId, "\r\n", NULL); } smtp_send_data (iSocket, strOut); *strOut = '\0'; if (smtp->strRrcptUserId && *smtp->strRrcptUserId) xstrcat (strOut, "Return-Receipt-To:", smtp->strRrcptUserId, ">\r\n", NULL); //indicate the mailing function. //If the caller is overriding, use the name supplied, otherwise //send the name of this function. if (smtp->strMailerName && *smtp->strMailerName) xstrcat (strOut, "X-Mailer: ", smtp->strMailerName, "\r\n", NULL); else xstrcat (strOut, "X-Mailer: ", SMTP_MAILER_NAME, "\r\n", NULL); /* Set the mime version. */ strcat (strOut, "MIME-Version: 1.0\r\n"); strcat (strOut, "Content-Type: Multipart/Mixed; boundary=\"Message-Boundary-21132\"\r\n"); smtp_send_data (iSocket, strOut); /* Write out any message comment included. */ xstrcpy (strOut, "Comments: ", smtp->strMsgComment, "\r\n", NULL); /* Send the subject and message body. */ xstrcat (strOut, "Subject:", smtp->strSubject, "\r\n", NULL); smtp_send_data (iSocket, strOut); //reset strOut *strOut = '\0'; /* Keep rfc822 in mind with all the sections. */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -