由 小码哥发布于 2019-03-08 标签:c++unixsmtpclient

c++ unix smtp client

我正在 unix 机器上使用 C++ 制作一个 smtp 客户端。我能够从 运行 程序接收电子邮件,但在从服务器接收的消息之间缺少来自客户端的输出。我真的需要一些帮助才能阅读这些消息。

我正在传递 in:the 服务器名称、发件人电子邮件、目标电子邮件、主题和消息。

    write (socketNO, "HELO ", 5);
    write (socketNO, argv[1], strlen(argv[1]));
    write (socketNO, " ", 1);
    write (socketNO, "\n", 1);

    /* Address the mail is coming from */
    write (socketNO, "MAIL FROM:<", 11);
    write (socketNO, argv[2], strlen(argv[2]));
    write (socketNO, ">", 1);
    write (socketNO, "\n", 1);

    /* Address the mail is going to */
    write (socketNO, "RCPT TO:<", 9);
    write (socketNO, argv[3], strlen(argv[3]));
    write (socketNO, ">", 1);
    write (socketNO, "\n", 1);  

    /* Says you are about to start the message & waits for servers OK */
    write (socketNO, "DATA", 5);
    write (socketNO, "\n", 1);

    /* Writes the subject and the message */
    write (socketNO, "SUBJECT: ", 9);
    write (socketNO, argv[4], strlen(argv[4]));
    write (socketNO, "\n", 1);
    write (socketNO, "\n", 1);
    write (socketNO, argv[5], strlen(argv[5]));
    write (socketNO, "\n", 1);
    write (socketNO, ".", 1);
    write (socketNO, "\n", 1);

    /* Quits the connection */
    write (socketNO, "QUIT", 4);

    int response = read (socketNO, fromServer, 512);
    close(socketNO);


    cout << response << " " << fromServer << endl;

这是我的输出

~$ client smtp.xxx.xxx fromEmail@xxx toEmail@xxx hi hello
hostname is  smtp.xxx.xxx
hello

311 220 smtp.xxx.xxx ESMTP Sendmail 8.54.4/8.14.4; Fri, 13 Nov 2015 10:46:23 -0500
250 smtp.xxx.xxx Hello unix.xxx.xxx [128.16.3.12], pleased to meet you
250 2.1.0 <fromEmail@xxx>... Sender ok
250 2.1.5 <toEmail@xxx>... Recipient ok
354 Enter mail, end with "." on a line by itself

外观应该如何

~$ client smtp.xxx.xxx fromEmail@xxx toEmail@xxx hi hello
    hostname is  smtp.xxx.xxx
    hello

    311 220 smtp.xxx.xxx ESMTP Sendmail 8.54.4/8.14.4; Fri, 13 Nov 2015 10:46:23 -0500
    HELO smtp.xxx.xxx
    250 smtp.xxx.xxx Hello unix.xxx.xxx [128.16.3.12], pleased to meet you
    MAIL FROM:<fromEmail@xxx>
    250 2.1.0 <fromEmail@xxx>... Sender ok
    RCPT TO:<toEmail@xxx>
    250 2.1.5 <toEmail@xxx>... Recipient ok
    DATA
    354 Enter mail, end with "." on a line by itself
    SUBJECT: hi
    hello
    .
    QUIT

自己写函数,例如:

ssize_t write_and_echo(int fd, const void *buf, size_t count)
{
    printf("%.*s", count, buf);
    return write(fd, buf, count);
}