《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

源码下载

相关推荐
Wish3D35 分钟前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云
凤年徐35 分钟前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表
oioihoii38 分钟前
C++11 右值引用:从入门到精通
开发语言·c++
阿阳微客3 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
朝新_4 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir4 小时前
Calendar类日期设置进位问题
java·开发语言
木子.李3475 小时前
排序算法总结(C++)
c++·算法·排序算法
风逸hhh5 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮5 小时前
Python训练第四十三天
开发语言·python
freyazzr6 小时前
C++八股 | Day2 | atom/函数指针/指针函数/struct、Class/静态局部变量、局部变量、全局变量/强制类型转换
c++