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();
}

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

QT += network

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

#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

#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();
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值