赞
踩
使用sendemailapi编写邮件发送比较方便,之后就是创建界面调用接口,将调用方法贴出:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
smtp = new SmtpClient(ui->lineEdit_3->text(), 25,SmtpClient::TcpConnection);
}
void MainWindow::on_pushButton_clicked()//发送
{
smtp->setUser(ui->lineEdit->text());
smtp->setPassword(ui->lineEdit_2->text());
MimeMessage message;
message.setSender(new EmailAddress(ui->lineEdit->text()));
//逐个添加收件人
QStringList receiver = ui->lineEdit_4->text().split(";");
for(int i = 0; i< receiver.count(); i++)
{
message.addRecipient(new EmailAddress(receiver.at(i)));
}
//构建邮件标题
message.setSubject(ui->lineEdit_5->text());
//构建邮件正文
MimeHtml text;
text.setHtml(ui->textEdit->toHtml());
message.addPart(&text);
//附件
for(int i = 0; i < filelist.count(); i++)
{
QString name = filelist.at(i);
QFile *file=new QFile(name);
if (file->exists()){
message.addPart(new MimeAttachment(file));
}
}
if(!smtp->connectToHost())
{
qDebug()<<"connect error ------";
return;
}
if(!smtp->login())
{
qDebug()<<"login error ------";
return;
}
if(!smtp->sendMail(message))
{
qDebug()<<"send Mail error -----";
return;
} else{
qDebug()<<"send Mail success -----";
}
smtp->quit();
}
void MainWindow::on_pushButton_2_clicked()//添加图片
{
filelist.clear();
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setWindowTitle(tr("Open Images"));
fileDialog->setDirectory("");
fileDialog->setNameFilter(tr("Images(*.jpg )"));
fileDialog->setFileMode(QFileDialog::ExistingFiles);
fileDialog->setViewMode(QFileDialog::Detail);
if(fileDialog->exec())
{
filelist = fileDialog->selectedFiles();
}
}
代码下载:https://download.csdn.net/download/u010304326/10692860
需求是发送文字和图片,我自己写的可以发送纯文本,但是无法发送图片,使用sendemailapi可以发送文字和附件图片,如果有实现一段文字一幅图片的欢迎交流讨论
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。