Qt5基于smtp服务发送电子邮件

1、设置邮箱

先登录163邮箱,然后在邮箱界面找到设置,在里面开通smtp服务。

这一步比较关键,要开通smtp服务,在开通的过程中会让你输入一个邮箱客户端授权码,这个才是你后面要用到的密码,而不是你登陆邮箱的密码;

Qt5基于smtp服务发送电子邮件

 2. telnet命令发送邮件

打开cmd.

1. 输入 telnet smtp.163.com 25 回车

我试验了好几个邮箱服务器,大都有严厉的反垃圾邮件系统,导致邮件发不出去,幸好163的邮箱竟然可以.TX威武呀.smtp的协议端口为25.

2. 输入 HELO smtp.163.com 回车

我搜集的一些资料中既有使用helo又有使用ehlo的,结果还使我纠结了好久,后来又google才知道,原来都可以.这句应该是与服务器打招呼.如果返回250表示服务器准备就绪,可以进行下一步了.

3. 输入 AUTH LOGIN 回车

这是指定登录的模式,这句应该是一般模式.返回334就可以进行下一步了.

4. 输入 经过base64加密的你的账户名,比如你的邮箱账号号是xxxxxx@163.com,就去百度下 base64加密这个邮箱账号(是加密整个邮箱账号),copy到命令行回车返回334

5.输入对应的授权码(base64加密这个授权码),注意是163邮箱的授权码,不是密码。回复235 Authentication successful表示验证通过,可以进行下一步了.

6. 输入 mail from:<xxxxxx@163.com> 这里写你的163邮箱,服务器返回250.

7. 输入 rcpt to:<xxxxxx@qq.com> 这里写你要发送给的qq邮箱,服务器返回250,下一步

8. 输入 data,表示开始传送数据,服务器返回354

9. 输入 subject:你的主题此处服务器无回显

10. 输入 此处什么也不用写,直接回车换行,作为主题与正文的分界此处服务器无回显

11. 输入 在这里写你的正文就可以了此处服务器也无回显

12. 输入 .对,就是一个英文句号,表示数据传送完毕,服务器返回250

13. 输入 quit服务器返回221,就安了,完成了,看看邮件收到了没.

整个过程入下图所示:

Qt5基于smtp服务发送电子邮件

 

 qt代码如下:

mailtest.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-07-08T10:30:13
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = mailtest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        mainwindow.cpp \
    smth.cpp

HEADERS  += mainwindow.h \
    smth.h

FORMS    += mainwindow.ui
QT += network

smtp.h
#ifndef SMTH_H
#define SMTH_H


#include<QByteArray>
#include<QString>
#include<QTcpSocket>

class Smtp
{
public:
    Smtp(QByteArray username,QByteArray password);
    ~Smtp();
    void send(QByteArray recvaddr,QString subject,QString content);
private:
    QTcpSocket * clientsocket;
    QByteArray username;
    QByteArray password;
    QByteArray recvaddr;
    QByteArray mailfrom = "mail from:<";
    QByteArray rcptto = "rcpt to:<";
    QByteArray prefrom = "from:";
    QByteArray preto = "to:";
    QByteArray presubject ="subject:";
    QString subject;                //主题
    QString content;                //发送内容
    QByteArray recvdata;            //接收到的数据
};

#endif // SMTH_H

smtp.cpp
#include "smth.h"

#include<qDebug>

Smtp::Smtp(QByteArray username,QByteArray password)
{
    if(username.contains("@163"))
    {
        this->username = username;
        this->password = password;
    }
    else
        qDebug()<<"NOT 163";
}

void Smtp::send(QByteArray recvaddr,QString subject,QString content)
{
    this->recvaddr = recvaddr;
    this->subject = subject;
    this->content = content;
    QByteArray usernametmp = this->username;
    QByteArray recvaddrtmp = this->recvaddr;

    clientsocket=new QTcpSocket();
    this->clientsocket->connectToHost("smtp.163.com",25,QTcpSocket::ReadWrite);
    this->clientsocket->waitForConnected(1000);
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;


    this->clientsocket->write("HELO smtp.163.com\r\n");
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;

    this->clientsocket->write("AUTH LOGIN\r\n");
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;

    qDebug()<<"username:"<<username;
    this->clientsocket->write(username.toBase64().append("\r\n"));
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;

    qDebug()<<"password:"<<password;
    this->clientsocket->write(password.toBase64().append("\r\n"));
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;

    this->clientsocket->write(mailfrom.append(usernametmp.append(">\r\n")));
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;
    //发送邮箱
    //qDebug()<<"mail from:"<<mailfrom.append(usernametmp.append(">\r\n"));

    this->clientsocket->write(rcptto.append(recvaddrtmp.append(">\r\n")));
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;
    //接收邮箱
    //qDebug()<<"rcp to:"<<rcptto.append(recvaddrtmp.append(">\r\n"));

    //data表示开始传输数据
    this->clientsocket->write("data\r\n");
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;


    usernametmp = this->username;
    recvaddrtmp = this->recvaddr;

    this->clientsocket->write(prefrom.append(usernametmp.append("\r\n")));
    this->clientsocket->write(preto.append(recvaddrtmp.append("\r\n")));
    this->clientsocket->write(presubject.append(subject.toLocal8Bit().append("\r\n")));
    this->clientsocket->write("\r\n");
    this->clientsocket->write(content.toLocal8Bit().append("\r\n"));
    this->clientsocket->write(".\r\n");
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;

    this->clientsocket->write("quit\r\n");
    this->clientsocket->waitForReadyRead(1000);
    recvdata = clientsocket->readAll();
    qDebug()<<recvdata;
}

Smtp::~Smtp()
{
    delete this->clientsocket;
}

main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "smth.h"

int main(int argc, char *argv[])
{

    Smtp smtp("feigenchen@163.com","pj7049");
    smtp.send("774367088@qq.com","测试smpt","这个是我写的代码给你发的邮件哈哈哈哈哈哈哈哈哈^_^");
    return 0;
}

希望对各位有所帮助
参考网址:https://www.cnblogs.com/chunfeng0060/p/6135089.html
参考网址:https://blog.csdn.net/wangdeyu1994/article/details/78693427


上一篇:C++Qt5+win10+MSVC2015 64bit构建程序,Release编译成功后使用windeployqt.exe发布程序使用于win7出现缺少MSVCP140.dll!!!


下一篇:c – 删除对象后触发的QTimer :: SingleShot