How to implement the Qt Mail sending tool
This article mainly explains "how to realize Qt mail sending tool". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to realize Qt mail sending tool" together.
I. Foreword
Main functions:
Hold mainstream mailbox such as 163 mailbox QQ mailbox sent.
Support rich text sending, you can send text with HTML format.
Support multiple attachments, attachments can be pictures.
void EmailThread::run(){ while (! stopped) { int count = contents.count(); if (count > 0) { mutex.lock(); QString content = contents.takeFirst(); QString fileName = fileNames.takeFirst(); mutex.unlock(); QString result; QStringList list = sendEmailAddr.split("@"); QString tempSMTP = list.at(1).split(". ").at(0); int tempPort = 25; //QQ mailbox port number is 465, SSL protocol must be enabled. if (tempSMTP.toUpper() == "QQ") { tempPort = 465; } SmtpClient smtp(QString("smtp.% 1.com").arg(tempSMTP), tempPort, tempPort == 25 ? SmtpClient::TcpConnection : SmtpClient::SslConnection); smtp.setUser(sendEmailAddr); smtp.setPassword(sendEmailPwd); //Build a mail subject that includes sender and recipient attachments, etc. MimeMessage message; message.setSender(new EmailAddress(sendEmailAddr)); //Add recipients one by one QStringList receiver = receiveEmailAddr.split(';'); for (int i = 0; i
< receiver.size(); i++) { message.addRecipient(new EmailAddress(receiver.at(i))); } //构建邮件标题 message.setSubject(emialTitle); //构建邮件正文 MimeHtml text; text.setHtml(content); message.addPart(&text); //构建附件-报警图像 if (fileName.length() >0) { QStringList attas = fileName.split(";"); foreach (QString tempAtta, attas) { QFile *file = new QFile(tempAtta); if (file->exists()) { message.addPart(new MimeAttachment(file)); } } } if (! smtp.connectToHost()) { result = "Mail server connection failed"; } else { if (! smtp.login()) { result = "Mail user login failed"; } else { if (! smtp.sendMail(message)) { result = "Message sending failed"; } else { result = "Email sent successfully"; } } } smtp.quit(); if (! result.isEmpty()) { emit receiveEmailResult(result); } msleep(1000); } msleep(100); } stopped = false;} 3. Effect diagram
Thank you for reading, the above is "Qt mail sending tool how to achieve" the content, after the study of this article, I believe we have a deeper understanding of how to achieve this problem Qt mail sending tool, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!