当前位置:   article > 正文

QT5实现发送邮件功能

qt5实现发送邮件
#include <QCoreApplication>
#include <QTcpSocket>
#include <QSslSocket>
#include <QDebug>

void sendEmail()
{
    QString email = "接受邮箱账号";
    QString subject = "邮件标题";
    QString body = "邮件正文";
    QString smtpServer = "邮箱服务器";
    int smtpPort = 25; //端口号
    QString senderEmail = "发送邮箱账号";
    QString senderPassword = "发送邮箱密码";

    // Connect to the SMTP server
    QTcpSocket socket;	//连接方式。这里选用TCP方式,也可以选择SSL加密方式
    socket.connectToHost(smtpServer, smtpPort);
    if (!socket.waitForConnected())
    {
        qDebug() << "Failed to connect to SMTP server.";
        return;
    }
    else
        qDebug() << "Success to connect to SMTP server.";

    // Read the welcome message from the server
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive welcome message from SMTP server.";
        return;
    }
    QByteArray response = socket.readAll();
    qDebug() << response;

    // Send the EHLO command
    socket.write("EHLO localhost\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send EHLO command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive EHLO command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the authentication login command and credentials
    socket.write("AUTH LOGIN\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send AUTH LOGIN command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive AUTH LOGIN command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    QByteArray encodedEmail = senderEmail.toUtf8().toBase64();
    QByteArray encodedPassword = senderPassword.toUtf8().toBase64();

    socket.write(encodedEmail + "\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send encoded email to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive email authentication response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    socket.write(encodedPassword + "\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send encoded password to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive password authentication response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the MAIL FROM command
    socket.write(QString("MAIL FROM:<%1>\r\n").arg(senderEmail).toUtf8());
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send MAIL FROM command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive MAIL FROM command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the RCPT TO command
    socket.write(QString("RCPT TO:<%1>\r\n").arg(email).toUtf8());
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send RCPT TO command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive RCPT TO command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the DATA command
    socket.write("DATA\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send DATA command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive DATA command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the message headers and body
    QString message = "To: " + email + "\r\n";
    message += "From: " + senderEmail + "\r\n";
    message += "Subject: " + subject + "\r\n";
    message += "Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n";
    message += body + "\r\n";

    socket.write(message.toUtf8());
    socket.write(".\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send message to SMTP server.";
        return;
    }
    qDebug() << "Message sent successfully.";

    // Quit the session
    socket.write("QUIT\r\n");
    socket.waitForBytesWritten();

    // Close the connection
    socket.close();
}


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

    sendEmail();

    return a.exec();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

注意,使用网络模块,需要在.pro文件中添加

QT += network
  • 1

之后简单封装了一下,在以后的项目中可以直接使用:

#ifndef VDLEMAIL_H
#define VDLEMAIL_H

#include <QTcpSocket>
#include <QSslSocket>
#include <QDebug>

class VDLEmail
{
public:
    VDLEmail();
    void sendEmail(QString SmtpServer,int SmtpPort,QString SenderName,
                   QString SenderEmail,QString SenderPassword,QString ReceiveName,
                   QString ReceiveEmail,QString SubjectTitle,QString Content);
};

#endif // VDLEMAIL_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
#include "vdlemail.h"

VDLEmail::VDLEmail()
{

}

void VDLEmail::sendEmail(QString SmtpServer,int SmtpPort,QString SenderName,
                         QString SenderEmail,QString SenderPassword,QString ReceiveName,
                         QString ReceiveEmail,QString SubjectTitle,QString Content)
{

    QString toName = ReceiveName;
    QString email = ReceiveEmail;
    QString subject = SubjectTitle;
    QString body = Content;

    QString smtpServer = SmtpServer;
    int smtpPort = SmtpPort;

    QString fromName = SenderName;
    QString senderEmail = SenderEmail;
    QString senderPassword = SenderPassword;


    // Connect to the SMTP server
    QTcpSocket socket;
    socket.connectToHost(smtpServer, smtpPort);
    if (!socket.waitForConnected())
    {
        qDebug() << "Failed to connect to SMTP server.";
        return;
    }
    else
        qDebug() << "Success to connect to SMTP server.";

    // Read the welcome message from the server
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive welcome message from SMTP server.";
        return;
    }
    QByteArray response = socket.readAll();
    qDebug() << response;

    // Send the EHLO command
    socket.write("EHLO localhost\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send EHLO command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive EHLO command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the authentication login command and credentials
    socket.write("AUTH LOGIN\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send AUTH LOGIN command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive AUTH LOGIN command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    QByteArray encodedEmail = senderEmail.toUtf8().toBase64();
    QByteArray encodedPassword = senderPassword.toUtf8().toBase64();

    socket.write(encodedEmail + "\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send encoded email to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive email authentication response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    socket.write(encodedPassword + "\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send encoded password to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive password authentication response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the MAIL FROM command
    socket.write(QString("MAIL FROM:<%1>\r\n").arg(senderEmail).toUtf8());
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send MAIL FROM command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive MAIL FROM command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the RCPT TO command
    socket.write(QString("RCPT TO:<%1>\r\n").arg(email).toUtf8());
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send RCPT TO command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive RCPT TO command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the DATA command
    socket.write("DATA\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send DATA command to SMTP server.";
        return;
    }
    if (!socket.waitForReadyRead())
    {
        qDebug() << "Failed to receive DATA command response from SMTP server.";
        return;
    }
    response = socket.readAll();
    qDebug() << response;

    // Send the message headers and body
    QString message = "To: \"" + toName + "\"<" + email + ">\r\n";
    message += "From: \"" + fromName + "\"<" + senderEmail + ">\r\n";
    message += "Subject: " + subject + "\r\n";
    message += "Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n";
    message += body + "\r\n";

    socket.write(message.toUtf8());
    socket.write(".\r\n");
    if (!socket.waitForBytesWritten())
    {
        qDebug() << "Failed to send message to SMTP server.";
        return;
    }
    qDebug() << "Message sent successfully.";

    // Quit the session
    socket.write("QUIT\r\n");
    socket.waitForBytesWritten();

    // Close the connection
    socket.close();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/282907
推荐阅读
相关标签
  

闽ICP备14008679号