QT使用SMTP實現(xiàn)發(fā)送簡單中文文本郵件

使用telnet發(fā)送郵件

通過使用cmd下的telnet發(fā)送一封郵件,掌握SMTP發(fā)送郵件的過程。

1. 準備工作

  • 對發(fā)送方郵箱地址進行base64編碼。
  • 對發(fā)送方郵箱授權(quán)碼進行base64編碼。

Python進行base64編碼方法:

>>> import base64
>>> base64.b64encode(b"123456@163.com") # b表示使用ascii對字符串進行編碼
b'MTIzNDU2QDE2My5jb20='

2. 發(fā)送郵件

  1. 打開cmd

  2. 輸入telnet smtp.163.com 25

  3. 進入telnet后,依次輸入以下內(nèi)容

    helo 163.com # 打招呼
    auth login # 登錄命令
    MTIzNDU2QDE2My5jb20= # base64編碼后的發(fā)送方郵箱地址
    MTIzNDU2 # base64編碼后的發(fā)送方郵箱授權(quán)碼
    mail from: <vfyouv655@163.com> # 注明發(fā)件人
    rcpt to: <963312591@qq.com> # 注明收件人
    data # 表示接下來開始輸入數(shù)據(jù)部分
    from:vfyouv655@163.com # 注明發(fā)件人
    to:963312591@qq.com # 注明收件人
    subject:Test # 輸入郵件主題
                 # 一個空行
    i am testing to send mail # 郵件正文
                 # 一個空行
    . # 表示郵件內(nèi)容輸入結(jié)束
    
  4. 正常情況結(jié)果如下圖所示


使用QT實現(xiàn)以上內(nèi)容

記得在.pro文件中引入network模塊。下面直接上代碼。

widget.h文件內(nèi)容

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QMessageBox>
#include <QDebug>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void readyReadSlot();

private:
    //檢查連接狀態(tài)
    void checkConnectState();
    //發(fā)送helo
    void sendHelo();
    //發(fā)送auth login
    void sendAuthLogin();
    //發(fā)送用戶名
    void sendUser();
    //發(fā)送密碼
    void sendPassword();
    //發(fā)送mail from
    void sendMailFrom();
    //發(fā)送rcpt to
    void sendRcptTo();
    //發(fā)送data
    void sendData();
    //發(fā)送郵件內(nèi)容
    void sendContent();
    //發(fā)送成功后續(xù)操作
    void afterSendSuccess();

private:
    Ui::Widget *ui;

    QString senderMail; //發(fā)送方郵箱地址
    QString authCode; //發(fā)送方郵箱授權(quán)碼
    QString receiveMail; //接收方郵箱地址

    QString title; //郵件標題
    QString content; //郵件正文

    QString expectedReply; //期待收到的應(yīng)答
    void (Widget::*nextAction)(); //收到正確應(yīng)答后下一步要執(zhí)行的方法

    QTcpSocket *tcpSocket;
};

#endif // WIDGET_H

widget.cpp文件內(nèi)容

#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget),
    senderMail("這里填寫發(fā)送方郵箱地址"), authCode("這里填寫發(fā)送方郵箱授權(quán)碼"), receiveMail("這里填寫接收方郵箱地址"),
    expectedReply("220 163.com Anti-spam GT for Coremail System"), nextAction(&Widget::checkConnectState)
{
    ui->setupUi(this);

    //設(shè)置郵件的標題和正文
    this->title = "這是郵件標題";
    this->content = "這句話是郵件的正文";

    //tcpSocket初始化
    this->tcpSocket = new QTcpSocket(this);
    tcpSocket->connectToHost("smtp.163.com", 25);
    connect(tcpSocket, &QTcpSocket::readyRead,this, &Widget::readyReadSlot);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::readyReadSlot()
{
    QByteArray buffer = tcpSocket->readAll();
    qDebug()<<"收到服務(wù)器回復(fù):"<<buffer;

    if(buffer.contains(this->expectedReply.toLatin1())){
        if(nextAction) (this->*nextAction)();
    }
    else{
        QMessageBox::warning(this, "提示", "郵件發(fā)送失敗!");
    }
}

//檢查連接狀態(tài)
void Widget::checkConnectState()
{
    if(tcpSocket->state() == QAbstractSocket::ConnectedState){
        sendHelo();
    }
    else{
        QMessageBox::warning(this, "提示", "連接失敗!");
    }
}

//發(fā)送helo
void Widget::sendHelo()
{
    QString str = "helo 163.com\r\n";
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "250 OK\r\n";
    this->nextAction = &Widget::sendAuthLogin;
}

//發(fā)送auth login
void Widget::sendAuthLogin()
{
    QString str = "auth login\r\n";
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "334 dXNlcm5hbWU6\r\n";
    this->nextAction = &Widget::sendUser;
}

//發(fā)送用戶名
void Widget::sendUser()
{
    QString str = QString("\r\n").prepend(senderMail.toLatin1().toBase64());
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "334 UGFzc3dvcmQ6\r\n";
    this->nextAction = &Widget::sendPassword;
}

//發(fā)送密碼
void Widget::sendPassword()
{
    QString str = QString("\r\n").prepend(authCode.toLatin1().toBase64());
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "235 Authentication successful\r\n";
    this->nextAction = &Widget::sendMailFrom;
}

//發(fā)送mail from
void Widget::sendMailFrom()
{
    QString str = QString("mail from: <%1>\r\n").arg(senderMail);
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "250 Mail OK\r\n";
    this->nextAction = &Widget::sendRcptTo;
}

//發(fā)送rcpt to
void Widget::sendRcptTo()
{
    QString str = QString("rcpt to: <%1>\r\n").arg(receiveMail);
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "250 Mail OK\r\n";
    this->nextAction = &Widget::sendData;
}

//發(fā)送data
void Widget::sendData()
{
    QString str = "data\r\n";
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toLatin1());
    this->expectedReply = "354 End data with <CR><LF>.<CR><LF>\r\n";
    this->nextAction = &Widget::sendContent;
}

//發(fā)送郵件內(nèi)容
void Widget::sendContent()
{
    QString str = QString("from:%1\r\n"
                          "to:%2\r\n"
                          "subject:=?UTF-8?B?%3?=\r\n"
                          "\r\n"
                          "%4\r\n"
                          "\r\n"
                          ".\r\n").arg(senderMail).arg(receiveMail).arg( QString("").append(title.toUtf8().toBase64()) ).arg(content);
    qDebug()<<"向服務(wù)器發(fā)送: " + str;
    tcpSocket->write(str.toUtf8());
    this->expectedReply = "250 Mail OK queued as";
    this->nextAction = &Widget::afterSendSuccess;
}

//發(fā)送成功后續(xù)操作
void Widget::afterSendSuccess()
{
    QMessageBox::information(this, "提示", "郵件發(fā)送成功!");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容