From 328a551517c611699ecb2234c8193092a1144617 Mon Sep 17 00:00:00 2001 From: Daniel Wendt Date: Wed, 3 Jul 2024 16:08:01 +0200 Subject: [PATCH] add smime demo --- demos/CMakeLists.txt | 3 ++ demos/demo5/CMakeLists.txt | 20 ++++++++ demos/demo5/demo5.cpp | 95 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 demos/demo5/CMakeLists.txt create mode 100644 demos/demo5/demo5.cpp diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt index cac7c2f..4c93284 100644 --- a/demos/CMakeLists.txt +++ b/demos/CMakeLists.txt @@ -2,4 +2,7 @@ add_subdirectory(demo1) add_subdirectory(demo2) add_subdirectory(demo3) add_subdirectory(demo4) +if(BUILD_WITH_SMIME) +add_subdirectory(demo5) +endif() add_subdirectory(async1) diff --git a/demos/demo5/CMakeLists.txt b/demos/demo5/CMakeLists.txt new file mode 100644 index 0000000..785fb85 --- /dev/null +++ b/demos/demo5/CMakeLists.txt @@ -0,0 +1,20 @@ +include_directories( + +) + +set(demo_SRCS + demo5.cpp +) + +add_definitions(-DWITH_SMIME) + +add_executable(demo5 + ${demo_SRCS} +) + +target_link_libraries(demo5 + SimpleMail::Core + Qt${QT_VERSION_MAJOR}::Core + OpenSSL::SSL + OpenSSL::Crypto +) diff --git a/demos/demo5/demo5.cpp b/demos/demo5/demo5.cpp new file mode 100644 index 0000000..9389aca --- /dev/null +++ b/demos/demo5/demo5.cpp @@ -0,0 +1,95 @@ +#include "../../src/SimpleMail" + +#include + +using namespace SimpleMail; + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + // This is a demo that shows you how to sign and/or encrypt an email + + // First we need to create an SmtpClient object + // We will use the Gmail's smtp server (smtp.gmail.com, port 465, ssl) + + Server server; + server.setHost(QLatin1String("smtp.gmail.com")); + server.setPort(465); + server.setConnectionType(Server::SslConnection); + + // We need to set the username (your email address) and password + // for smtp authentification. + + server.setUsername(QLatin1String("your_email_address@host.com")); + server.setPassword(QLatin1String("your_password")); + + // Now we create a MimeMessage object. This is the email. + + MimeMessage message; + + EmailAddress sender(QLatin1String("your_email_address@host.com"), QLatin1String("Your Name")); + message.setSender(sender); + + EmailAddress to(QLatin1String("recipient@host.com"), QLatin1String("Recipient's Name")); + message.addTo(to); + + message.setSubject(QLatin1String("SmtpClient for Qt - Demo")); + + // Now add some text to the email. + // First we create a MimeText object. + + auto text = std::make_shared(); + + text->setText(QLatin1String("Hi,\nThis is a simple email message.\n")); + + // Now add it to the mail + + message.addPart(text); + + // Add an attachment + + auto document = + std::make_shared(std::make_shared(QLatin1String("document.pdf"))); + message.addPart(document); + + // Now create an SMime object + + auto smime = new SimpleMail::SMime(&message); + + // Setup private and public key/certificate in PKCS#12 format + + smime->setKeyFile(QLatin1String("your_private_key.p12"), QLatin1String("your_private_key_password")); + smime->setPublicKey(QLatin1String("recipient_public_key.cert")); + + // Sign the message. Only your private key is required. + // if(!smime->sign()) { + // qDebug() << "Failed to create signed email"; + // delete smime; + // return -3; + // } + + // Encrypt the message. Only the recipient's public key/certificate is required. + // if(!smime->encrypt()) { + // qDebug() << "Failed to create encrypted email"; + // delete smime; + // return -3; + // } + + // Sign and encrypt the message + if (!smime->signAndEncrypt()) { + qDebug() << "Failed to create signed and encrypted email"; + delete smime; + return -3; + } + + // Now we can send the mail + ServerReply *reply = server.sendMail(message); + QObject::connect(reply, &ServerReply::finished, [=] { + qDebug() << "ServerReply finished" << reply->error() << reply->responseText(); + reply->deleteLater(); + qApp->exit(reply->error() ? -3 : 0); + }); + + app.exec(); +}