《QT实用小工具·十二》邮件批量发送工具

1、概述
源码放在文章末尾

该项目实现了邮件的批量发送,如下图所示:

项目部分代码如下所示:

cpp 复制代码
#ifndef SMTPCLIENT_H
#define SMTPCLIENT_H

#include <QtGui>
#include <QtNetwork>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#include "mimemessage.h"

class SmtpClient : public QObject
{
	Q_OBJECT
public:
	enum AuthMethod {
		AuthPlain,
		AuthLogin
	};

	enum SmtpError {
		ConnectionTimeoutError,
		ResponseTimeoutError,
		AuthenticationFailedError,
		ServerError,    // 4xx smtp error
		ClientError     // 5xx smtp error
	};

	enum ConnectionType {
		TcpConnection,
		SslConnection,
		TlsConnection       // STARTTLS
	};

	SmtpClient(const QString &host = "locahost", int port = 25, ConnectionType ct = TcpConnection);

	~SmtpClient();

	const QString &getHost() const;
	void setHost(QString &host);

	int getPort() const;
	void setPort(int port);

	const QString &getName() const;
	void setName(const QString &name);

	ConnectionType getConnectionType() const;
	void setConnectionType(ConnectionType ct);

	const QString &getUser() const;
	void setUser(const QString &host);

	const QString &getPassword() const;
	void setPassword(const QString &password);

	SmtpClient::AuthMethod getAuthMethod() const;
	void setAuthMethod(AuthMethod method);

	const QString &getResponseText() const;
	int getResponseCode() const;

	int getConnectionTimeout() const;
	void setConnectionTimeout(int msec);

	int getResponseTimeout() const;
	void setResponseTimeout(int msec);

	QTcpSocket *getSocket();

	bool connectToHost();
	bool login();
	bool login(const QString &user, const QString &password, AuthMethod method = AuthLogin);

	bool sendMail(MimeMessage &email);
	void quit();

protected:
	QTcpSocket *socket;
	QString host;
	int port;
	ConnectionType connectionType;
	QString name;

	QString user;
	QString password;
	AuthMethod authMethod;

	int connectionTimeout;
	int responseTimeout;

	QString responseText;
	int responseCode;

	class ResponseTimeoutException {};
    void waitForResponse();
	void sendMessage(const QString &text);

signals:
	void smtpError(SmtpError e);

};

#endif // SMTPCLIENT_H

源码下载

相关推荐
dudly22 分钟前
Python类的力量:第五篇:魔法方法与协议——让类拥有Python的“超能力”
开发语言·python
ghost14333 分钟前
C#学习第22天:网络编程
开发语言·学习·c#
zhengddzz38 分钟前
从卡顿到丝滑:JavaScript性能优化实战秘籍
开发语言·javascript·性能优化
范纹杉想快点毕业39 分钟前
以项目的方式学QT开发(三)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·qt·mysql·算法·命令模式
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 13课题、智能指针
开发语言·青少年编程·rust·编程与数学
攻城狮7号1 小时前
Python爬虫第21节- 基础图形验证码识别实战
开发语言·爬虫·python·图形验证码识别
敲代码的瓦龙1 小时前
STL?list!!!
c语言·开发语言·数据结构·c++·windows·list
程序员莫小特1 小时前
【GESP真题解析】第 20 集 GESP 二级 2025 年 3 月编程题 1:等差矩阵
c语言·数据结构·c++·算法·青少年编程·矩阵
每次的天空1 小时前
Android学习总结之kotlin篇(二)
android·学习·kotlin