Qt发送邮件

首先下载支持库
https://download.csdn.net/download/zhangxuechao_/10598108

#ifndef MAIL_H
#define MAIL_H

#include <QString>

class Mail
{
public:
    Mail();
    void sendMail(QString user, QString fileName);
};

#endif // MAIL_H
#include "mail.h"
#include "sendemailapi/smtpmime.h"

Mail::Mail()
{

}

void Mail::sendMail(QString user, QString fileName)
{
    SmtpClient smtp("smtp.163.com", 25, SmtpClient::TcpConnection);
    smtp.setUser("test@163.com");
    smtp.setPassword("123456");

    //构建邮件主题,包含发件人收件人附件等.
    MimeMessage message;
    message.setSender(new EmailAddress("test@163.com"));

    //逐个添加收件人
    message.addRecipient(new EmailAddress("test@qq.com"));

    //构建邮件标题
    message.setSubject(QStringLiteral("title"));

    //构建邮件正文
    MimeText text;
    text.setText(QStringLiteral("hello furong"));
    message.addPart(&text);

    if (!smtp.connectToHost()){
        return;
    }
    if (!smtp.login()){
        return;
    }
    if (!smtp.sendMail(message)){
        return;
    }

    smtp.quit();
}
posted @ 2018-08-23 15:13  thomas_blog  阅读(526)  评论(0编辑  收藏  举报