gpt4 book ai didi

c++ - 使用 SMTP 协议(protocol)和 boost 库发送电子邮件

转载 作者:可可西里 更新时间:2023-11-01 02:43:04 25 4
gpt4 key购买 nike

我正在尝试使用 SMTP 协议(protocol)和 boost 库发送电子邮件但出于某种原因,我无法连接到 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;
}

最佳答案

我认为您需要了解一下 boost:asio 的工作原理。本质上,您正在创建一个服务,但从不调用 run()。看聊天样本here ,并注意它生成一个专门用于执行工作的线程:

boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

关于c++ - 使用 SMTP 协议(protocol)和 boost 库发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8446949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com