【Pop3】【SMTP】编写发送和接收邮件的C语言程序

实验环境为 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]

在这里插入图片描述

运行效果

发送邮件

在这里插入图片描述
接收邮件

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a9c93f2300

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值