0

我正在尝试使用 Poco 库来发送电子邮件。但是,当我尝试使用该库时,出现未定义的错误。我认为这与我用于 Poco 的编译器有关,但我不确定我应该使用哪个编译器,以便 QT 和 Poco 使用相同的东西进行编译。

Mass_Mailer.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Mass_Mailer
TEMPLATE = app
SOURCES += main.cpp \
    mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
INCLUDEPATH += /usr/local/include/

主文件

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/MailRecipient.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/NetException.h>

using namespace std;
using namespace Poco::Net;
using namespace Poco;

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

int sendmail()
{
string host = "mail.domain.com";
UInt16 port = 25;
string user = "xxx";
string password = "xxx";
string to = "xxx@domain.com";
string from = "xxx@domain.com";
string subject = "Your first e-mail message sent using Poco Libraries";
subject = MailMessage::encodeWord(subject.c_str(), "UTF-8");
string content = "Well done! You've successfully sent your first message using Poco SMTPClientSession";
MailMessage message;
message.setSender(from);
message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, to));
message.setSubject(subject);
message.setContentType("text/plain; charset=UTF-8");
message.setContent(content, MailMessage::ENCODING_8BIT);
try {
    SMTPClientSession session(host, port);
    session.open();
    try {
        session.login(SMTPClientSession::AUTH_LOGIN, user, password);
        session.sendMessage(message);
        cout << "Message successfully sent" << endl;
        session.close();
    } catch (SMTPException &e) {
        cerr << e.displayText() << endl;
        session.close();
        return 0;
    }
} catch (NetException &e) {
    cerr << e.displayText() << endl;
    return 0;
}
return 0;
}

错误

main.o: In function `sendmail()':
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:31: undefined reference to `Poco::Net::MailMessage::encodeWord(std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:34: undefined reference to `Poco::Net::MailMessage::setSender(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::MailRecipient(Poco::Net::MailRecipient::RecipientType, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailMessage::addRecipient(Poco::Net::MailRecipient const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:36: undefined reference to `Poco::Net::MailMessage::setSubject(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:37: undefined reference to `Poco::Net::MailMessage::setContentType(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:38: undefined reference to `Poco::Net::MailMessage::setContent(std::string const&, Poco::Net::MailMessage::ContentTransferEncoding)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:40: undefined reference to `Poco::Net::SMTPClientSession::SMTPClientSession(std::string const&, unsigned short)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:41: undefined reference to `Poco::Net::SMTPClientSession::open()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:43: undefined reference to `Poco::Net::SMTPClientSession::login(Poco::Net::SMTPClientSession::LoginMethod, std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:44: undefined reference to `Poco::Net::SMTPClientSession::sendMessage(Poco::Net::MailMessage const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::~MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:48: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:49: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:51: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:53: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33:     undefined reference to `Poco::Net::MailMessage::~MailMessage()'
main.o:(.gcc_except_table+0x120): undefined reference to `typeinfo for Poco::Net::SMTPException'
main.o:(.gcc_except_table+0x124): undefined reference to `typeinfo for Poco::Net::NetException'
collect2: error: ld returned 1 exit status
make: *** [Mass_Mailer] Error 1
11:45:01: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Mass_Mailer (kit: Desktop)
When executing step 'Make'11:45:01: Elapsed time: 00:02.
4

1 回答 1

1

您需要将 Poco 库链接到您的项目。在项目文件中,添加如下内容:

LIBS += -lPocoNet -lPocoFoundation
于 2013-09-25T18:17:58.443 回答