laitimes

QT Development - Mail Sending Tool

author:QT Tutorials

I have always wanted to use QT to implement an email sending program that calls the lowest socket communication to implement, previously written in C#, Microsoft has encapsulated, I don't know how the bottom layer is implemented, only know how to call the method, this time implemented in C++, refer to many online examples.

In fact, to put it bluntly, it is nothing more than the use of the SMTP protocol, which is an international standard, so QQ mailbox and 163 mailbox can be used, can be sent to each other, from Baidu Encyclopedia explanation: SMTP is currently the de facto standard for transmitting E-Mail on the Internet, is a relatively simple text-based protocol. On top of it, one or more recipients of a message are specified (in most cases determined to be present), and then the message text is transmitted. See http://baike.baidu.com/link?url=UAahhbpfOatRRHfUpG6jUyrgYgF3Kj7ll2WkJ6104RiAETv-jh-f-Xm1fwWMotrP

Project name: Mail sending tool

Development environment: XP+QT4.7+QT CREATOR2.8+MINGW

Compiled through test platforms: XP, Win7, ubuntu, tiny210, TE6410

Technical implementation: Implement SMTP protocol through socket communication, parse the protocol according to the reply and process the email

Basic features:

1: Support sending mainstream mailboxes such as 163 mailboxes QQ mailboxes.

2: Support rich text sending, can send text with HTML formatting.

3: Support multiple attachments to send, attachments can be pictures.

Note: 163 mailbox and 126 mailbox, the sending port is 25, do not use SSL protocol, while QQ mailbox must use SSL protocol, port is 465. If it is sent by QQ mailbox, the premise is to open the SMTP protocol in the QQ mailbox settings, otherwise the sending is unsuccessful, I am stuck here for half an hour, and I receive an email from QQ mailbox, the content is as follows:

Your sister, the default QQ mailbox does not have SMTP service enabled.

QT Development - Mail Sending Tool

Run screenshot:

QT Development - Mail Sending Tool
QT Development - Mail Sending Tool

Rough steps:

Step 1: Lay out the interface, name the controls, and use Pascal nomenclature.

Step 2: Prepare the sendemailapi folder to store the publicly available third-party support for sending attachments and rich text MIME protocol files. Add to the project.

Step 3: Instantiate the email sending object

SmtpClient smtp(ui->cboxServer->currentText(),
ui->cboxPort->currentText().toInt(),
ui->ckSSL->isChecked()?SmtpClient::SslConnection:SmtpClient::TcpConnection);
smtp.setUser(ui->txtSender->text());
smtp.setPassword(ui->txtSenderPwd->text());           

Step 4: Build a message subject with sender recipient attachments, etc

MimeMessage message;
message.setSender(new EmailAddress(ui->txtSenderAddr->text()));
//逐个添加收件人
QStringList receiver = ui->txtReceiverAddr->text().split(';');
for (int i = 0; i < receiver.size(); i++){
message.addRecipient(new EmailAddress(receiver.at(i)));
}
//构建邮件标题
message.setSubject(ui->txtTitle->text());
//构建邮件正文
MimeHtml text;
text.setHtml(ui->txtContent->toHtml());
// //如果直接发送简单文本,使用以下方法
// MimeText text;
// text.setText("测试简单文本发送!");
message.addPart(&text);
//构建附件
QString atta=ui->txtAtta->text();
if (atta!=""){
QStringList attas=atta.split(";");
foreach (QString tempAtta, attas) {
QFile *file=new QFile(tempAtta);
if (file->exists()){
message.addPart(new MimeAttachment(file));
}
}
}           

Step 5: Log in to the server to send emails

if (!smtp.connectToHost()){
QMessageBox::critical(this,"错误","服务器连接失败!");
return;
}
if (!smtp.login()){
QMessageBox::critical(this,"错误","用户登录失败!");
return;
}
if (!smtp.sendMail(message)){
QMessageBox::critical(this,"错误","邮件发送失败!");
return;
}else{
QMessageBox::information(this,"错误","邮件发送成功!");
}
smtp.quit();
           

[Get the QT development tutorial learning materials, click the link below to receive the fee ↓↓, live first and don't get lost~]

Click here: "Link"