实验环境为 Redhet Linux
自动发送邮件的C语言程序 send.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdbool.h>
#include <linux/tcp.h>
#define SMTPSERVPORT 25
#define MAXDATASIZE 4096
#define POP3SERVPORT 110
#define TRUE 1
main(int argc, char *argv[])
{
int sockfd;
struct hostent *host;
struct sockaddr_in serv_addr;
char *SMTPMessage[]={
"helo\r\n",
"mail from:<zjh@venusxk6214.com>\r\n",
"rcpt to:<la@venusxk6214.com>\r\n",
"data\r\n",
"This is the fourth group, and the members of the group are Zhang Jinghe, Liu Ang, Hou Jiangzhou, Li Tengrui, Nan Xiteng, Song Yunlong, and Shi Fukuan\r\nThis is the programming Send Mail item\r\n.\r\n",
"QUIT\r\n",
NULL};
int iLength=0;
int iMsg=0;
char buf[MAXDATASIZE];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket error");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SMTPSERVPORT);
serv_addr.sin_addr.s_addr = inet_addr("169.254.74.209");
bzero(&(serv_addr.sin_zero),8); /*置指针变量
(&(serv_addr.sin_zero)中前 8 个变量的值为零*/
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1){
perror("connect error");
exit(1);
}
iLength=recv(sockfd,buf,sizeof(buf),0);
buf[iLength]='\0';
printf("received:%s\n",buf);
//依次发送 SMPT 命令,发送邮件
do {
bool bNodelay=TRUE; /*bool 型变量只有两个值:false 和
true,是 0 和 1 的区别*/
setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(const char*)&bNodelay,sizeof(bool));
send(sockfd,SMTPMessage[iMsg],strlen(SMTPMessage[iMsg]),0);
printf("have sent: %s",SMTPMessage[iMsg]);
iLength=recv(sockfd,buf,sizeof(buf),0);
buf[iLength]='\0';
iMsg++;
printf("received: %s,%d\n",buf,iMsg);
}
while (SMTPMessage[iMsg]);
close(sockfd);
}
自动接收邮件的C语言程序 recieve.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdbool.h>
#include <linux/tcp.h>
#define SMTPSERVPORT 25
#define MAXDATASIZE 4096
#define TRUE 1
main(int argc, char *argv[])
{
int sockfd;
struct hostent *host;
struct sockaddr_in serv_addr;
char *POPMessage[]={
"USER la\r\n",
"PASS 123456\r\n",
"STAT\r\n",
"LIST\r\n",
"RETR 1\r\n",
"QUIT\r\n",
NULL};
int iLength=0;
int iMsg=0;
char buf[MAXDATASIZE];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket error");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(110);
serv_addr.sin_addr.s_addr = inet_addr("169.254.74.209");
bzero(&(serv_addr.sin_zero),8); /*置指针变量
(&(serv_addr.sin_zero)中前 8 个变量的值为零*/
if (connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(struct sockaddr))==-1){
perror("connect error");
exit(1);
}
iLength=recv(sockfd,buf,sizeof(buf),0);
buf[iLength]='\0';
printf("received:%s\n",buf);
do{
send(sockfd,POPMessage[iMsg],strlen(POPMessage[iMsg]),0);
printf("have sent: %s",POPMessage[iMsg]);
iLength=recv(sockfd,buf,sizeof(buf),0);
buf[iLength]='\0';
printf("received: %s,%d\n",buf,iMsg);
buf[iLength]='\0';
printf("%s\n",buf);
iMsg++;
}
while(POPMessage[iMsg]);
close(sockfd);
}
编译命令
gcc [filename] -o [executable_program_name]
执行程序
./[executable_program_name]
运行效果
发送邮件
接收邮件