LINUX.ORG.RU

Не получается отправить письмо

 , , , ,


0

1

Нашёл такой код отправки письма https://codingways.blogspot.ru/2012/08/send-email-in-c-smtp-client-in-c.html

У меня в итоге он выглядит так

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>

using boost::asio::ip::tcp;
using namespace boost::archive::iterators;
typedef base64_from_binary<transform_width<const char *, 6, 8> > base64_text;

class SMTPClient
{
public:
    enum AUTH_STATUS { AUTH_NONE, AUTHENTICATING, AUTHENTICATED, AUTHENTICATIONFAILED } mauth_status;
    SMTPClient(std::string pServer, unsigned int pPort, std::string pUser, std::string pPassword) :
        mServer(pServer), mPort(pPort), mUserName(pUser), mPassword(pPassword), mSocket(mIOService), mResolver(mIOService)
    {
        mauth_status = AUTH_NONE;
    }
    void Authenticate()
    {
        mauth_status = AUTHENTICATING;
        tcp::resolver::query qry(mServer, boost::lexical_cast<std::string>(mPort));
        mResolver.async_resolve(qry, boost::bind(&SMTPClient::handleResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
        mIOService.run();
    }
    void Quit()
    {
        writeLine("QUIT");
        mIOService.stop();
    }
    bool Send(std::string pFrom, std::string pTo, std::string pSubject, std::string pMessage)
    {
        if (mauth_status != AUTHENTICATED || mHasError) {
            std::cout << "not authenticated ('" << mErrorMsg << "'). returning...";
            return false; // has error
        }
        std::cout << "SENDING ....";
        writeLine("MAIL FROM: \"ProWeb Alert\"<" + pFrom + ">");
        writeLine("RCPT TO:" + pTo);
        writeLine("DATA");
        writeLine("FROM:<" + pFrom + ">");
        writeLine("SUBJECT:" + pSubject);
        writeLine("");
        writeLine(pMessage);
        writeLine("");
        writeLine(".");
        return true;//TODO: smtp server response must be checked.
    }

private:
    std::string encodeBase64(std::string pData)
    {
        std::stringstream os;
        size_t sz = pData.size();
        std::copy(base64_text(pData.c_str()), base64_text(pData.c_str() + sz), ostream_iterator<char>(os));
        return os.str();
    }
    void handleResolve(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator)
    {
        if (!err) {
            std::cout << " resolving done\n";
            tcp::endpoint endpoint = *endpoint_iterator;
            mSocket.async_connect(endpoint, boost::bind(&SMTPClient::handleConnect, this,
                boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else {
            std::cout << " can't resolve\n";
            mHasError = true;
            mErrorMsg = err.message();
        }
    }
    void writeLine(std::string pData)
    {
        std::ostream req_strm(&mRequest);
        req_strm << pData << "\r\n";
        //std::cout << pData << "\r\n";
        boost::asio::write(mSocket, mRequest);
        req_strm.clear();
    }
    void handleConnect(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator)
    {
        if (!err) {
            std::cout << " connected \n";
            // The connection was successful. Send the request.
            mHasError = false;
            std::ostream req_strm(&mRequest);
            writeLine("EHLO " + mServer);
            writeLine("AUTH LOGIN");
            writeLine(encodeBase64(mUserName));
            writeLine(encodeBase64(mPassword));
            mauth_status = AUTHENTICATED;
        }
        else {
            std::cout << " can not connect\n";
            mauth_status = AUTHENTICATIONFAILED;
            mHasError = true;
            mErrorMsg = err.message();
        }
    }

    std::string mServer;
    std::string mUserName;
    std::string mPassword;
    unsigned int mPort;
    boost::asio::io_service mIOService;
    tcp::resolver mResolver;
    tcp::socket mSocket;
    boost::asio::streambuf mRequest;
    boost::asio::streambuf mResponse;
    bool mHasError;
    std::string mErrorMsg;
    std::ostringstream mResponseData;
    std::ostringstream mResponseHeader;
};

int main()
{
    SMTPClient mailc("smtp.mail.ru", 25, "LOGIN", "PASSWORD");
    mailc.Authenticate();
    mailc.Send("LOGIN@mail.ru", "LOGIN@mail.ru", "subject", "Hello from C++ SMTP Client!");
}

Когда доходит до writeLine(«FROM:<» + pFrom + «>»); Получается исключение boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >

Что надо поправить?


Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.