我正在尝试使用SMTP协议发送电子邮件并增强库 但由于某种原因,我无法连接到hotmail smtp服务器并向我发送电子邮件..
这是我的代码: 我做错了什么?
DNS_RECORD* ppQueryResultsSet = NULL;
// dns mx lookup
DnsQuery(L"hotmail.com", DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &ppQueryResultsSet, NULL);
if (ppQueryResultsSet == NULL)
return false;
// i am getting the mx servers (i.e: mx3.hotmail.com)
wcout << "MX Host name: " << ppQueryResultsSet->Data.MX.pNameExchange << endl;
DnsRecordListFree(ppQueryResultsSet, DnsFreeRecordList);
string host_name = String_Convertion::WStringToString(ppQueryResultsSet->Data.MX.pNameExchange);
// setting up socket connection
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(host_name, "25");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query), end;
tcp::socket socket(io_service);
// connecting to the server
boost::system::error_code error_code_connect;
boost::asio::connect(socket, endpoint_iterator, end, error_code_connect);
if (error_code_connect)
{
cout << "Error connecting to SMTP Server" << endl;
return false;
}
boost::system::error_code error;
std::string temp_buf;
boost::array<char, 128> buf;
size_t len;
// settings the packets to send
list<string> packets;
packets.push_back("HELO");
packets.push_back("MAIL FROM: xxx@xxx.xxx");
packets.push_back("VRFY xxx@xxx.xxx");
packets.push_back("RCPT TO: x@hotmail.com");
packets.push_back("DATA");
packets.push_back("Subject: subject!");
packets.push_back("hi how are you doing?.");
packets.push_back("QUIT");
list<string>::iterator it;
// iterating to send the packets
for (it = packets.begin(); it != packets.end(); it++)
{
socket.write_some(boost::asio::buffer(*it+"\r\n"));
len = socket.read_some(boost::asio::buffer(buf), error);
temp_buf = buf.data();
temp_buf = temp_buf.substr(0, len);
cout << temp_buf << endl;
}
答案 0 :(得分:1)
我认为您需要了解boost:asio
的工作原理。基本上,您正在创建服务,但从不调用run()
。查看聊天示例here,并注意它专门生成一个线程来执行工作:
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
答案 1 :(得分:0)
尽管可能存在其他问题,但您需要在邮件标题和正文之间额外添加\r\n
。
您还需要通过发送包含DATA
.\r\n
部分