《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

源码下载

相关推荐
Flash.kkl1 分钟前
Python基础语法
开发语言·python
十五年专注C++开发3 分钟前
CMake进阶:find_package使用总结
开发语言·c++·cmake·跨平台编译
lxw18449125144 分钟前
PHP凉了?岗位缩水50%+,开发者该何去何从?
开发语言·php
Clarence Liu12 分钟前
用 Go 从 100 亿个数中找到最小的 100 个数 —— 实战与原理
开发语言·后端·golang
Hcoco_me17 分钟前
大模型面试题23:对比学习原理-从通俗理解到核心逻辑(通用AI视角)
人工智能·rnn·深度学习·学习·自然语言处理·word2vec
闻缺陷则喜何志丹17 分钟前
【计算几何】平面凸包
c++·数学·扫描线·凸包·单调性·上凸包·下凸包
xiaowu08018 分钟前
IEnumerable、IEnumerator接口与yield return关键字的相关知识
java·开发语言·算法
csbysj202023 分钟前
Perl 目录操作指南
开发语言
-To be number.wan24 分钟前
C++ 运算符重载入门:让“+”也能为自定义类型服务!
开发语言·c++
未来之窗软件服务24 分钟前
幽冥大陆(七十九)Python 水果识别训练视频识别 —东方仙盟练气期
开发语言·人工智能·python·水果识别·仙盟创梦ide·东方仙盟