Qt 使用SMTP服务发送邮件

一、开启QQ邮箱的SMTP服务

1、进入邮箱后,在顶部找到设置,点击进入

2、点击账号选项卡

3、点击开启服务

4、如果没有绑定手机,会有提示,要先绑定手机。绑定手机后生成授权码。保存好授权码,之后需要使用。

5、生成后可以返回页面,点击管理服务

6、进去之后可以在授权码管理中对已经生成的授权码进行停用,如果忘记了之前的授权码,也可以点击生成授权码生成新的授权码。

7、生成授权码的时候需要发送一个短信,可以使用微信扫描二维码然后发送短信

二、下载SmtpClient-for-Qt库

GitHub地址:GitHub - bluetiger9/SmtpClient-for-Qt: An SMTP Client writen in C++ for Qt. Allows applications to send emails (MIME with text, html, attachments, inline files, etc.) via SMTP. Supports SSL and SMTP authentication.

三、编译SmtpClient-for-Qt库

1、打开工程

解压下载好的压缩包,使用自己的Qt编译器打开工程(我使用的是Qt5.9.2),工程在SmtpClient-for-Qt-2.0\src下

2、选择自己需要的环境,我这里选的是MSVC2015 32bit

3、在项目名称上右键,点击重新构建

4、在Qt5.9.2上编译报错如下,因为QRandomGenerator类是在Qt5.10之后才新增的,所以需要修改下这个随机数的代码

5、打开mimemultipart.cpp文件,把

#include <QRandomGenerator>

修改为

#include <random>

6、新增函数generate64

quint64 generate64()
{
    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<quint64> dis;

    return dis(gen);
}

7、把MimeMultiPart函数里边的

md5.addData(QByteArray().append(QRandomGenerator::global()->generate64()));

修改为

 md5.addData(QByteArray().append(generate64()));

8、修改前后对比

修改前:

修改后:

9、mimemultipart.cpp修改后的代码

/*
  Copyright (c) 2011-2012 - Tőkés Attila

  This file is part of SmtpClient for Qt.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  See the LICENSE file for more details.
*/

#include "mimemultipart.h"
#include <QIODevice>
#include <QTime>
#include <QCryptographicHash>
#include <random>

const QString MULTI_PART_NAMES[] = {
    "multipart/mixed",         //    Mixed
    "multipart/digest",        //    Digest
    "multipart/alternative",   //    Alternative
    "multipart/related",       //    Related
    "multipart/report",        //    Report
    "multipart/signed",        //    Signed
    "multipart/encrypted"      //    Encrypted
};

quint64 generate64()
{
    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<quint64> dis;

    return dis(gen);
}

MimeMultiPart::MimeMultiPart(MultiPartType type)
{
    this->type = type;
    this->cType = MULTI_PART_NAMES[this->type];
    this->cEncoding = _8Bit;

    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(QByteArray().append(generate64()));
    cBoundary = md5.result().toHex();
}

MimeMultiPart::~MimeMultiPart() {
    foreach (MimePart *part, parts) {
        delete part;
    }
}

void MimeMultiPart::addPart(MimePart *part) {
    parts.append(part);
}

const QList<MimePart*> & MimeMultiPart::getParts() const {
    return parts;
}

void MimeMultiPart::writeContent(QIODevice &device) const {
    QList<MimePart*>::const_iterator it;

    for (it = parts.constBegin(); it != parts.constEnd(); it++) {
        device.write("--" );
        device.write(cBoundary.toLatin1());
        device.write("\r\n");
        (*it)->writeToDevice(device);
    };

    device.write("--");
    device.write(cBoundary.toLatin1());
    device.write("--\r\n");
}


void MimeMultiPart::setMimeType(const MultiPartType type) {
    this->type = type;
    this->cType = MULTI_PART_NAMES[type];
}

MimeMultiPart::MultiPartType MimeMultiPart::getMimeType() const {
    return type;
}

10、重新编译之后会生成对应的dll和lib文件,Debug和Release模式都编译一次,生成的lib文件是一样的,dll分为Debug和Release模式

四、自带demo

1、库里边自带了四个demo可以参考里边的代码

2、使用demo的时候注意库的路径,需要对应修改成自己的路径,否则无法使用

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值