int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // First create the SmtpClient object and set the user and the password. Sender smtp(QLatin1String("smtp.gmail.com"), 465, Sender::SslConnection); smtp.setUser(QLatin1String("*****@*****.**")); smtp.setPassword(QLatin1String("your_password")); // Create a MimeMessage MimeMessage message; EmailAddress sender(QLatin1String("*****@*****.**"), QLatin1String("Your Name")); message.setSender(sender); EmailAddress to(QLatin1String("*****@*****.**"), QLatin1String("Recipient's Name")); message.addTo(to); message.setSubject(QLatin1String("SmtpClient for Qt - Example 4 - Html email with images")); // Now we need to create a MimeHtml object for HTML content MimeHtml html; html.setHtml(QLatin1String("<h1> Hello! </h1>" "<h2> This is the first image </h2>" "<img src='cid:image1' />" "<h2> This is the second image </h2>" "<img src='cid:image2' />")); // Create a MimeInlineFile object for each image MimeInlineFile image1 (new QFile(QLatin1String("image1.jpg"))); // An unique content id must be setted image1.setContentId(QByteArrayLiteral("image1")); image1.setContentType(QByteArrayLiteral("image/jpg")); MimeInlineFile image2 (new QFile(QLatin1String("image2.jpg"))); image2.setContentId(QByteArrayLiteral("image2")); image2.setContentType(QByteArrayLiteral("image/jpg")); message.addPart(&html); message.addPart(&image1); message.addPart(&image2); // Now the email can be sended if (!smtp.sendMail(message)) { qDebug() << "Failed to send mail!" << smtp.lastError(); return -3; } smtp.quit(); }
QByteArray ViewEmail::render(Context *c) const { Q_D(const ViewEmail); QVariantHash email = c->stash(d->stashKey).toHash(); if (email.isEmpty()) { c->error(QStringLiteral("Cannot render template, template name or template stash key not defined")); return QByteArray(); } MimeMessage message; QVariant value; value = email.value(QStringLiteral("to")); if (value.type() == QVariant::String && !value.toString().isEmpty()) { message.addTo(value.toString()); } value = email.value(QStringLiteral("cc")); if (value.type() == QVariant::String && !value.toString().isEmpty()) { message.addCc(value.toString()); } value = email.value(QStringLiteral("from")); if (value.type() == QVariant::String && !value.toString().isEmpty()) { message.setSender(value.toString()); } value = email.value(QStringLiteral("subject")); if (value.type() == QVariant::String && !value.toString().isEmpty()) { message.setSubject(value.toString()); } QVariant body = email.value(QStringLiteral("body")); QVariant parts = email.value(QStringLiteral("parts")); if (body.isNull() && parts.isNull()) { c->error(QStringLiteral("Can't send email without parts or body, check stash")); return QByteArray(); } if (!parts.isNull()) { const QVariantList partsVariant = parts.toList(); Q_FOREACH (const QVariant &part, partsVariant) { MimePart *mime = part.value<MimePart*>(); if (mime) { message.addPart(mime); } else { qCCritical(CUTELYST_VIEW_EMAIL) << "Failed to cast MimePart"; } } auto contentTypeIt = email.constFind(QStringLiteral("content_type")); if (contentTypeIt != email.constEnd() && !contentTypeIt.value().isNull() && !contentTypeIt.value().toString().isEmpty()) { const QByteArray contentType = contentTypeIt.value().toString().toLatin1(); qCDebug(CUTELYST_VIEW_EMAIL) << "Using specified content_type" << contentType; message.getContent().setContentType(contentType); } else if (!d->defaultContentType.isEmpty()) { qCDebug(CUTELYST_VIEW_EMAIL) << "Using default content_type" << d->defaultContentType; message.getContent().setContentType(d->defaultContentType); } } else {