2011-09-07 12:43:17 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2011 - Tőkés Attila
|
|
|
|
|
|
|
|
This file is part of SmtpClient for Qt.
|
|
|
|
|
|
|
|
SmtpClient for Qt is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
SmtpClient for Qt is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY.
|
|
|
|
|
|
|
|
See the LICENSE file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-10-30 20:56:39 +00:00
|
|
|
#include <QtCore>
|
|
|
|
|
|
|
|
#include "../../src/SmtpMime"
|
2011-09-07 12:43:17 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-10-30 20:56:39 +00:00
|
|
|
QCoreApplication a(argc, argv);
|
2011-09-07 12:43:17 +00:00
|
|
|
|
|
|
|
// First create the SmtpClient object and set the user and the password.
|
|
|
|
|
|
|
|
SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection);
|
|
|
|
|
2014-11-01 20:50:37 +00:00
|
|
|
smtp.setUser("your_email@host.com");
|
2011-09-07 12:43:17 +00:00
|
|
|
smtp.setPassword("your_password");
|
|
|
|
|
|
|
|
// Create a MimeMessage
|
|
|
|
|
|
|
|
MimeMessage message;
|
|
|
|
|
2014-11-01 20:50:37 +00:00
|
|
|
EmailAddress sender("your_email_address@host.com", "Your Name");
|
|
|
|
message.setSender(&sender);
|
|
|
|
|
|
|
|
EmailAddress to("recipient@host.com", "Recipient's Name");
|
|
|
|
message.addRecipient(&to);
|
|
|
|
|
2011-09-07 12:43:17 +00:00
|
|
|
message.setSubject("SmtpClient for Qt - Example 3 - Html email with images");
|
|
|
|
|
|
|
|
// Now we need to create a MimeHtml object for HTML content
|
|
|
|
MimeHtml html;
|
|
|
|
|
|
|
|
html.setHtml("<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("image1.jpg"));
|
|
|
|
|
|
|
|
// An unique content id must be setted
|
|
|
|
image1.setContentId("image1");
|
2011-11-13 17:18:58 +00:00
|
|
|
image1.setContentType("image/jpg");
|
2011-09-07 12:43:17 +00:00
|
|
|
|
|
|
|
MimeInlineFile image2 (new QFile("image2.jpg"));
|
|
|
|
image2.setContentId("image2");
|
2011-11-13 17:18:58 +00:00
|
|
|
image2.setContentType("image/jpg");
|
2011-09-07 12:43:17 +00:00
|
|
|
|
|
|
|
message.addPart(&html);
|
|
|
|
message.addPart(&image1);
|
|
|
|
message.addPart(&image2);
|
|
|
|
|
|
|
|
// Now the email can be sended
|
2014-10-30 20:56:39 +00:00
|
|
|
if (!smtp.connectToHost()) {
|
|
|
|
qDebug() << "Failed to connect to host!" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!smtp.login()) {
|
|
|
|
qDebug() << "Failed to login!" << endl;
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!smtp.sendMail(message)) {
|
|
|
|
qDebug() << "Failed to send mail!" << endl;
|
|
|
|
return -3;
|
|
|
|
}
|
2011-09-07 12:43:17 +00:00
|
|
|
|
|
|
|
smtp.quit();
|
|
|
|
|
|
|
|
}
|