C语言编写smtp用户代理之代码综合和管理

前两章已经写好了必要的函数,现在写一个主函数调用就可以了。

#include<unistd.h>
#include"renyj_sock.h"
#include"smtp.h"
#include"mutils.h"
int main(int argc, char *argv[])
{
    int opt = -1;
    char from[100]  = "";
    char to[100]    = "";
    char server[100]= "";
    char port[5]    = "";
    char user[100]  = "";
    char pass[100]  = "";
    char subject[100]="";
    char msgline[100]="";

    while (-1 != (opt = getopt(argc,argv,"f:t:s:p:u:m:z:x:h")))
    {
        switch(opt)
        {
            case 'f':
                strncpy(from,optarg,100);
                if (strlen(from)<=0)
                {
                    debug("invalid from ");
                    return -1;
                }
                break;
            case 't':
                strncpy(to,optarg,100);
                if (strlen(to) <= 0)
                {
                    debug("invalid to");
                    return -1;
                }
                break;
            case 's':
                strncpy(server,optarg,100);
                if (strlen(server) <= 0)
                {
                    debug("invalid server");
                    return -1;
                }
                break;
            case 'p':
                strncpy(port,optarg,5);
                if (strlen(port) <= 0)
                {
                    debug("invalid port");
                    return -1;
                }
                break;
            case 'u':
                strncpy(user,optarg,100);
                if (strlen(user) <= 0)
                {
                    debug("invalid user");
                    return -1;
                }
                break;
            case 'm':
                strncpy(pass,optarg,100);
                if (strlen(pass) <= 0)  
                {
                    debug("invalid pass");
                    return -1;
                }
                break;
            case 'z':
                strncpy(subject,optarg,100);
                if (strlen(subject) <= 0)
                {
                    debug("invalid subject");
                    return -1;
                }
                break;
            case 'x':
                strncpy(msgline,optarg,100);
                if (strlen(msgline) <= 0)
                {
                    debug("need one line"); 
                }
                break;
            case '?':
                printf("unknow option %c\n",optopt);
            case ':':
                printf("use -h for help\n");
                return -1;  
            case 'h':
                printf("-f  mail from\n");
                printf("-t  mail to\n");
                printf("-s  mail server\n");
                printf("-p  mail serverport\n");
                printf("-u  mail user\n");
                printf("-m  mail pass\n");
                printf("-z  mail subject\n");
                printf("-x  mail one line msg\n");
                printf("All info should be less than 100 bytes,port should less than 5!!!\n");
                return -1;  
        }
    }
    send_the_mail(from,to,server,atoi(port),user,pass,subject,msgline,10);  
    return 0;
}

这些都有了之后,写一个简单的Makefile。我使能了ssl,所以要加上openssl的库,需要 加上 -lssl 和 -lcrypto

obj = renyj_sock.c smtp.c test.c
CC = gcc
MUTILIB=./libmutils/libmutils.a
LIBS= $(MUTILIB) -lssl -lcrypto 
INCLUDE= libmutils
CFLAGS= -DDEBUG -DHAS_SSL -g -I$(INCLUDE)
link:
    gcc  $(CFLAGS) $(obj) -o sock_test $(LIBS)
clean:
    rm sock_test
install:
    echo "nothing to be done"

使用如下命令进行测试

./sock_test -f xxx@qq.com -t xxx@qq.com -s smtp.qq.com -p 465 -u xxx@qq.com -m xxx -z hello -x 123

测试结果如下

Debug>>>renyj_sock.c->turn_on_ssl()->line.239:connect ...
Debug>>>renyj_sock.c->turn_on_ssl()->line.247:connect end 
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 220 smtp.qq.com Esmtp QQ Mail Server
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-smtp.qq.com
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-PIPELINING
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-SIZE 73400320
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-AUTH LOGIN PLAIN
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-AUTH=LOGIN
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250-MAILCOMPRESS
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250 8BITMIME
Debug>>>smtp.c->send_the_mail()->line.167:start AUTH
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 334 VXNlcm5hbWU6
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 334 UGFzc3dvcmQ6
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 235 Authentication successful
Debug>>>smtp.c->send_the_mail()->line.171:start FROM
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250 Ok
Debug>>>smtp.c->send_the_mail()->line.175:start TO
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250 Ok
Debug>>>smtp.c->send_the_mail()->line.179:start DATA
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 354 End data with <CR><LF>.<CR><LF>
Debug>>>smtp.c->send_the_mail()->line.193:start eom
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 250 Ok: queued as 
Debug>>>smtp.c->send_the_mail()->line.197:QUIT
Debug>>>smtp.c->smtp_read_line()->line.14:[S] -----> 221 Bye

写在后面:

因为中间用了base64的编码,可以你们自己写,也可以用我这里的源码,我也是用的别人的。
请到这个地址下载:
http://download.csdn.net/detail/renyongjian1994/9906868

这里要多说一些,使用qq邮箱,有的人的密码是不能直接用的。需要对邮箱进行设定。进入qq邮箱的网页界面,点击设置,然后到如下位置设置,设置之后可以得到授权码,然后用授权码代替密码,应该就可以使用了。

邮箱设置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值