2017-12-18 203 views
5

我有一個smtp客戶端生成在c + +我可以發送電子郵件到我的測試mailtrap帳戶。 標題發送和電子郵件到達很好。Smtp客戶端 - 從和不發送

我的問題是,從/到指示該字段爲空 - 所顯示的截圖

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(); 

感謝黏合看看

+0

我想我它。我編輯了答案。 –

回答

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"); 
} 

我把整個數據放入一個字符串,併發送它在一個單一的寫。

什麼(顯然)事項:

  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()); 
+0

謝謝 - 我試過這個 - 但沒有任何運氣。 – abdoe

+0

我進一步調查,我認爲這是對免費賬戶的有意限制。無論何時你進入演示收件箱,xhr都會以json格式下載消息,並且每個對象都有許多空的字段,特別是:'from_email','from_name','to_email','to_name' ......基本上, free是'text_body'和其他一些信息。 –

+0

我一開始也這麼認爲 - 但是當我用telnet做同樣的事情時 - 那些字段被填滿了。所以我想我錯過了一些東西 – abdoe