Smtp客户端 - 来自和不发送

问题描述 投票:5回答:1

我有一个用c ++构建的smtp客户端,我可以将电子邮件发送到我的测试mailtrap帐户。标题已发送,电子邮件即将到达。

我的问题是,从/到指示的字段是空的 - 如屏幕截图所示

enter image description here

我正在发送标题

write_command("MAIL FROM: <[email protected]>");
write_command("RCPT TO: <[email protected]>");

我用我的Smtp客户端的完整代码做了一个要点

https://gist.github.com/anonymous/7bb13de7f044bcb5d07d0e6a9d991ea9

我从我的main()函数中调用它

 Smtp smtp_client = Smtp();
 smtp_client.new_connection("smtp.mailtrap.io", 25);
 smtp_client.auth_login("username", "password");
 smtp_client.sendmail();
 smtp_client.close_connection();

谢谢你的表情

c++ email smtp smtpclient
1个回答
4
投票

我设法让字段出现,编辑你的sendmail函数:

void sendmail()
{
    write_command("MAIL FROM: <[email protected]>");
    write_command("RCPT TO: <[email protected]>");

    write_command("DATA");

    std::string data;
    data.append("MIME-Version: 1.0\r\n");
    data.append("From: <[email protected]>\r\n");
    data.append("To: <[email protected]>\r\n");
    data.append("Subject: Welcome\r\n");
    data.append("Date: Fri, 29 Dec 2017 09:30:00 -0400\r\n");
    data.append("\r\n"); //this seems to matter
    data.append("This is a test");
    data.append("\r\n.");
    write_command(data);

    write_command("QUIT");
}

我将整个DATA放在一个字符串中,并在一次写入中发送。

什么(显然)很重要:

  1. 不要用空行开始数据部分;
  2. 在消息文本之前添加一个空行。

我还编辑了你的write_command,它与你的问题没有关系,但我建议你不要将字符串复制到缓冲区,而是直接使用字符串代替:

    //char command_buffer[255];
    //strcpy(command_buffer, command.c_str());
    //n = write(sockfd,command_buffer,strlen(command_buffer));
    n = write(sockfd,command.c_str(),command.length());
© www.soinside.com 2019 - 2024. All rights reserved.