bluetiger9 / SmtpClient-for-Qt

An SMTP Client writen in C++ for Qt. Allows applications to send emails (MIME with text, html, attachments, inline files, etc.) via SMTP. Supports SSL and SMTP authentication.
https://github.com/bluetiger9/SmtpClient-for-Qt/wiki
GNU Lesser General Public License v2.1
456 stars 228 forks source link

which step is the slowest? why don't u make it ASync? #87

Closed opengpu closed 2 years ago

opengpu commented 5 years ago

which step is the slowest? why don't u make it ASync?

JanKovis commented 2 years ago

It is up to anyone according to usage mode, for me following worked well ` void SmtpNotifier::notify(QString message) { // do not block main thread sending the mail auto result = QtConcurrent::run(sendMail, this, m_smtpServer, m_smtpPort, m_recipients, message); Q_UNUSED(result); }

void SmtpNotifier::sendMail(SmtpNotifier *notifier, QString server, uint port, QStringList recipients, QString message) { SmtpClient smtp(server, port, SmtpClient::TcpConnection);

connect(&smtp, &SmtpClient::smtpError, notifier, &SmtpNotifier::handleSmtpError);

const auto theSender = QString{"sender@mydomain.com"};

smtp.setUser(theSender);
smtp.setConnectionTimeout(std::chrono::seconds(30));
smtp.setResponseTimeout(std::chrono::seconds(30));
smtp.setAuthMethod(SmtpClient::AuthLogin);
// smtp.setPassword(); // no password when inside of companmy network

MimeMessage mimeMsg;
mimeMsg.setSender(EmailAddress(theSender, tr("my application")));
for (const auto &recipientAddress : recipients)
{
    mimeMsg.addRecipient(EmailAddress(recipientAddress));
}
mimeMsg.setSubject("Value threshold exceeded");

MimeText text;
text.setText(message + "\r\n\r\n" + tr("Your application"));

mimeMsg.addPart(&text);

smtp.connectToHost();
smtp.login();
smtp.sendMail(mimeMsg);
smtp.quit();

} `

bluetiger9 commented 2 years ago

Slowest part is usually(!) the sendMail() bit, as most of the data transfer happens here.

An Asynchronous mode is added in v2.0 of the library.