搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
繁依Fanyi0
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
机器学习python中用pd.read_excel()读取数据,数据类型是object类型,即非numeric数据类型,此时有两种方法查找非法字符_valueerror: value must be either numerical or a st
2
大型语言模(LLM)之提示词工程(四)_大语言模型 提示
3
java 手动提交事务_spring手动提交处理事务
4
Apollo新版本Beta自动驾驶技术沙龙开始招募啦!
5
C语言基础(二)—— 常量与变量、数据类型、进位制、关键字、原码反码补码、限定符、字符串格式化输入输出_int(“126”,19)的值
6
键盘输入语句_scanner_键盘输入的语法
7
Android Studio遇到 git: ‘credential-osxkeychain‘ is not a git command. 错误(Mac)_credential -cache is not a git command
8
Vue使用Google Map_vue 谷歌地图中英文
9
ORACLE数据库中EXPDP/IMPDP参数INCLUDE/EXCLUDE的使用说明_expdp exclude
10
markdown 如何输入上下标,颜色,居中_markdown上标注怎么打
当前位置:
article
> 正文
Qt 实现简单邮件发送_qt 邮件库
作者:繁依Fanyi0 | 2024-03-21 21:40:24
赞
踩
qt 邮件库
[cpp]
view plain
copy
#ifndef SMTP_H
#define SMTP_H
#include <QtCore>
#include <QCoreApplication>
#include <QObject>
#include <QTcpSocket>
#include <QString>
#include <QTextStream>
#include <QDebug>
#include <QAbstractSocket>
#include <QDateTime>
#include <QDate>
#include <QLocale>
#include <QObject>
#include <QTcpSocket>
class
Smtp :
public
QObject
{
Q_OBJECT
public
:
Smtp(QString smtphost, QString smtpusername, QString smtppass);
~Smtp();
bool
Send(
const
QString &to,
const
QString &subject,
const
QString &body );
int
linesend;
QStringList ErrorMSG;
bool
ReadLiner();
void
ErrorCloseAll();
signals:
void
status(
const
QString &);
void
SendLine();
public
slots:
bool
PutSendLine();
private
:
bool
isconnect;
QString smtphost;
QString smtpusername;
QString smtppass;
QString message;
QString output;
QString RemoteServerName;
QString mailstatus;
QTextStream *t;
QTcpSocket *smtpsocket;
QString from;
QString rcpt;
QString response;
QString SendLineAndGrab(QString senddata);
int
Timeout;
QString encodeBase64( QString xml );
QString decodeBase64( QString xml );
int
dateswap(QString form, uint unixtime );
QString TimeStampMail();
};
#endif
[cpp]
view plain
copy
#include "smtp.h"
Smtp::Smtp(QString smtphost, QString smtpusername, QString smtppass)
{
this
->smtphost = smtphost;
this
->smtpusername = smtpusername;
this
->smtppass = smtppass;
}
bool
Smtp::Send(
const
QString &to,
const
QString &subject,
const
QString &body )
{
qDebug()<<
"####"
<<Q_FUNC_INFO;
bool
res =
false
;
int
waittime = 5 * 1000;
this
->from = smtpusername;
rcpt = to;
ErrorMSG.clear();
Timeout = waittime;
linesend = 0;
isconnect =
false
;
QString tmp =
"=?utf-8?B?"
+ QByteArray().append(subject).toBase64()+
"?="
;
message.append(
"Subject:"
+ tmp +
"\n"
);
message.append(
"To: "
+ to +
"\n"
);
message.append(
"From: "
+smtpusername+
" <"
+ smtpusername +
">\n"
);
message.append(
"Content-Type: text/html; charset=UTF8;\n"
);
/* or txt */
message.append(
"Content-transfer-encoding: 7BIT\n\n\n\n"
);
message.append(body);
message.replace( tr(
"\n"
), tr(
"\r\n"
) );
message.replace( tr(
"\r\n.\r\n"
),tr(
"\r\n..\r\n"
) );
smtpsocket =
new
QTcpSocket(
this
);
connect(
this
, SIGNAL(SendLine()),
this
,SLOT(PutSendLine()));
if
(smtphost.size() > 0)
{
smtpsocket->connectToHost(smtphost,25);
}
else
{
smtpsocket->connectToHost(
"localhost"
,25);
}
if
(smtpsocket->waitForConnected(Timeout))
{
if
(smtpsocket->waitForReadyRead(Timeout))
{
isconnect =
true
;
return
ReadLiner();
}
}
else
{
ErrorCloseAll();
}
return
res;
}
bool
Smtp::ReadLiner()
{
bool
res =
false
;
if
(isconnect)
{
QTextCodec *codecx;
codecx = QTextCodec::codecForMib(106);
t =
new
QTextStream( smtpsocket );
t->setCodec(codecx);
int
loops = 0;
while
(!t->atEnd())
{
loops++;
response = t->readLine();
}
if
(response.size() > 0)
{
RemoteServerName = response;
mailstatus = response.left(3);
if
(mailstatus ==
"220"
)
{
response=
""
;
linesend = 1;
res =
true
;
}
}
else
{
ErrorCloseAll();
}
}
return
res;
}
Smtp::~Smtp()
{
delete
smtpsocket;
delete
t;
}
/* LINE SENDER */
bool
Smtp::PutSendLine()
{
static
bool
res =
true
;
int
current = linesend;
switch
(current)
{
case
1:
response = SendLineAndGrab(
"ehlo localhost"
);
if
(response.size() > 0)
{
ErrorMSG.append(response);
linesend = 2;
emit SendLine();
}
else
{
res=
false
;
}
response =
""
;
break
;
case
2:
response = SendLineAndGrab(
"AUTH LOGIN"
);
if
(response.size() > 0)
{
ErrorMSG.append(response);
linesend = 3;
emit SendLine();
}
else
{
res=
false
;
}
response =
""
;
break
;
case
3:
response = SendLineAndGrab(encodeBase64(smtpusername));
/* username send */
if
(response.size() > 0)
{
ErrorMSG.append(response);
if
(response.contains(
"334"
, Qt::CaseInsensitive))
{
linesend = 4;
emit SendLine();
}
}
else
{
res=
false
;
}
response =
""
;
break
;
case
4:
response = SendLineAndGrab(encodeBase64(smtppass));
/* pass send */
if
(response.size() > 0)
{
ErrorMSG.append(response);
if
(response.contains(
"235"
, Qt::CaseInsensitive))
{
linesend = 5;
emit SendLine();
}
else
{
res=
false
;
}
}
else
{
res=
false
;
}
response =
""
;
break
;
case
5:
response = SendLineAndGrab(tr(
"MAIL FROM: %1"
).arg(smtpusername));
if
(response.size() > 0)
{
linesend = 6;
emit SendLine();
}
break
;
case
6:
response = SendLineAndGrab(
"RCPT TO: "
+rcpt);
if
(response.size() > 0)
{
ErrorMSG.append(response);
response =
""
;
response = SendLineAndGrab(
"DATA"
);
if
(!response.contains(
"not"
, Qt::CaseInsensitive))
{
ErrorMSG.append(response);
response =
""
;
linesend = 7;
emit SendLine();
}
}
response =
""
;
break
;
case
7:
response = SendLineAndGrab(message+
"\r\n."
);
if
(response.size() && response.contains(
"ok"
, Qt::CaseInsensitive) )
{
ErrorMSG.append(response);
linesend = 8;
emit SendLine();
}
response =
""
;
break
;
case
8:
SendLineAndGrab(
"QUIT"
);
break
;
default
:
break
;
}
return
res;
}
/* SENDER AND RECIVER */
QString Smtp::SendLineAndGrab(QString senddata)
{
QString incommingData =
""
;
if
(isconnect)
{
int
current = linesend;
int
loops = 0;
*t << senddata <<
"\r\n"
;
t->flush();
if
(senddata !=
"QUIT"
) {
if
(smtpsocket->waitForReadyRead(Timeout))
{
while
(!t->atEnd())
{
loops++;
QString opera = t->readLine()+
"\n"
;
incommingData = opera + incommingData;
}
}
}
else
{
delete
smtpsocket;
delete
t;
isconnect =
false
;
return
incommingData;
}
}
else
{
ErrorCloseAll();
}
return
incommingData;
}
QString Smtp::encodeBase64( QString xml )
{
QByteArray text;
text.append(xml);
return
text.toBase64();
}
QString Smtp::decodeBase64( QString xml )
{
QByteArray xcode(
""
);;
xcode.append(xml);
QByteArray precode(QByteArray::fromBase64(xcode));
QString notetxt = precode.data();
return
notetxt;
}
int
Smtp::dateswap(QString form, uint unixtime )
{
QDateTime fromunix;
fromunix.setTime_t(unixtime);
QString numeric = fromunix.toString((
const
QString)form);
bool
ok;
return
(
int
)numeric.toFloat(&ok);
}
QString Smtp::TimeStampMail()
{
/* mail rtf Date format! http://www.faqs.org/rfcs/rfc788.html */
QDateTime timer1( QDateTime::currentDateTime() );
uint unixtime = timer1.toTime_t();
QDateTime fromunix;
fromunix.setTime_t(unixtime);
QStringList RTFdays = QStringList() <<
"giorno_NULL"
<<
"Mon"
<<
"Tue"
<<
"Wed"
<<
"Thu"
<<
"Fri"
<<
"Sat"
<<
"Sun"
;
QStringList RTFmonth = QStringList() <<
"mese_NULL"
<<
"Jan"
<<
"Feb"
<<
"Mar"
<<
"Apr"
<<
"May"
<<
"Jun"
<<
"Jul"
<<
"Aug"
<<
"Sep"
<<
"Oct"
<<
"Nov"
<<
"Dec"
;
QDate timeroad(dateswap(
"yyyy"
,unixtime),dateswap(
"M"
,unixtime),dateswap(
"d"
,unixtime));
QStringList rtfd_line;
rtfd_line.clear();
rtfd_line.append(
"Date: "
);
rtfd_line.append(RTFdays.at(timeroad.dayOfWeek()));
rtfd_line.append(
", "
);
rtfd_line.append(QString::number(dateswap(
"d"
,unixtime)));
rtfd_line.append(
" "
);
rtfd_line.append(RTFmonth.at(dateswap(
"M"
,unixtime)));
rtfd_line.append(
" "
);
rtfd_line.append(QString::number(dateswap(
"yyyy"
,unixtime)));
rtfd_line.append(
" "
);
rtfd_line.append(fromunix.toString(
"hh:mm:ss"
));
rtfd_line.append(
" +0100"
);
return
QString(rtfd_line.join(
""
));
}
void
Smtp::ErrorCloseAll()
{
delete
t;
smtpsocket->close();
}
调用示例:
[cpp]
view plain
copy
Smtp *sendmail =
new
Smtp(
"邮件服务器地址"
,
"账号"
,
"密码"
);
if
(sendmail->Send(“收件人”,
"主题"
,“内容”))
{
if
(sendmail->PutSendLine())
{
qDebug() <<
"发送成功."
;
}
else
{
qDebug() <<
"发送失败."
;
}
}
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/282930
推荐阅读
article
雷电
模拟器
adb
devices返回
127.0
.
0.1
:
5555
offline
分析和解决办法 --...
当我们运行安卓
模拟器
时,除了启动自身进程外,都会启动
adb
.exe,比如当我们启动雷电
模拟器
时,系统进程里会出现和两个进...
赞
踩
article
【
MySQL
】-
锁
的
使用
...
一般用于数据库备份,整个库只读。【
MySQL
】-
锁
的
使用
1、
锁
的
粒度分类 1、全局
锁
一般用于...
赞
踩
article
大
数据
开发
(
Hadoop
面试真题-卷八
)
...
YARN是Apache
Hadoop
生态系统中的一个集群资源管理器。它的主要目的是管理和分配集群中的资源,并为运行在Ha...
赞
踩
article
Raw
格式
怎么
转换
JPG
?这几种
转换
方法很简单_
raw
格式
怎么
转换
成jpg...
Raw
格式
图片指的是未经过任何压缩或处理的图像文件,这些文件通常是相机或设备直接从传感器获取的原始图像数据,具有更高的色...
赞
踩
article
FFMpeg
交叉
编译
Android
-SO库_
ffmpeg
-gl-
transition
ffmpeg
...
一定要注意啊
FFMpeg
和NDK的版本,版本不搭配会不成功我是用的版本为:
FFMpeg
:3.3.6http://www....
赞
踩
article
Qt
散文
一...
Qt
的事件分为普通事件和系统事件,普通事件比如用户按下键盘,系统事件比如定时器事件。事件循环的开始是从main函数的QA...
赞
踩
article
Qt
自定义
信号
强度显示控件
_
qt
做
信号
强弱...
Qt
自定义
信号
强度显示控件,适合新手学习。
_
qt
做
信号
强弱
qt
做
信号
强弱 ...
赞
踩
article
怎么将
bmp
格式
图片
转换
jpg
格式
的_
bmp
图像
转换
成
jpg
只要修改扩展名即可,对吗...
BMP是Windows操作系统中的标准图像文件
格式
,因为不会对
图片
进行压缩,所以图像还原较高,清晰度较高,但是因为不...
赞
踩
article
刷题
DAY27
|
LeetCode
39
-
组合
总和
40-
组合
总和
II 131-
分割
回文
串...
为什么 used[i - 1] == false 就是同一树层呢,因为同一树层,used[i - 1] == false...
赞
踩
article
项目
经验-
张黎敏
_
张黎敏
数据库
...
项目
一:通用
数据库
管理系统DM4
项目
简介(功能与用途): DM4是在分析了国际上主流
数据库
技术和开源系统的基础上,创新...
赞
踩
article
基于
单片机
的
智能
衣柜
设计
_基于
单片机
的
智能
衣柜
监控系统的
设计
...
随着科技的不断发展,人们对于生活品质的要求越来越高。
智能
衣柜
作为
智能
家居的一个重要组成部分,能够为用户提供便捷、个性化的...
赞
踩
article
深度
学习
框架
大战:究竟
选择
TensorFlow
还是
PyTorch
?...
【AI
框架
对决】一决高下:
TensorFlow
vs
PyTorch
,谁是
深度
学习
的王者?
深度
学习
框架
大战:究竟
选择
T...
赞
踩
article
【
自动化
】
C#
中的
全双工
通信
与
半双工
通信
:读写问题及应用实例...
【
自动化
】
C#
中的
全双工
通信
与
半双工
通信
:读写问题及应用实例【
自动化
】
C#
中的
全双工
通信
与
半双工
通信
:读写问题及应用实例...
赞
踩
article
【论文阅读】
Energy
Efficient
Real
-time Task Scheduling o...
主要工作:通过动态电压和频率缩放研究了新兴
CPU
-
GPU
混合集群的节能问题。 + 首次分析
GPU
特定的DVFS模型。 +...
赞
踩
article
程序员
第一次
相亲
,只因请
对方
吃
肯德基
而被嫌弃,
最后
被怒删!_
相亲
kfc...
今天看到一个有趣的话题,一个粉丝投稿:“我是
程序员
,今天
相亲
时被
对方
嫌弃了,
最后
她删了我”。楼主男生,
第一次
见面时请
对方
...
赞
踩
article
Qt
自定义
动画属性
QPropertyAnimation
...
简述
QPropertyAnimation
类定义了Qt的属性动画。
QPropertyAnimation
以Qt属性做差值,作...
赞
踩
article
【推荐系统论文精读系列】(十一)--
DeepFM
A
Factorization
-
Machine
b...
文章目录一、摘要二、介绍三、我们的方法3.1
DeepFM
FM ComponentDeep Component3.2 与...
赞
踩
article
计算机
视觉
:
图像
搜索
(
BOW
)_
视觉
bow
...
目录1
图像
检索简介2基于内容的
图像
检索3基于
BOW
的
图像
检索原理3.1
BOW
词袋模型原理3.2
视觉
单词4实验4.1例子图...
赞
踩
article
C
语言
实现推
箱子
小游戏
_
c
语言
推
箱子
...
推
箱子
小游戏
,
c
语言
函数,循环,分支结构,
小游戏
设计_
c
语言
推
箱子
c
语言
推
箱子
一、设计目的...
赞
踩
article
CentOS7
Firewall 防火墙配置教程_
firewall
-cmd
--
zone
=publi...
一、
firewall
介绍CentOS 7中防火墙是一个非常的强大的功能,在CentOS 6.5中在iptables防火墙...
赞
踩
相关标签
android
mysql
数据库
锁
大数据
hadoop
面试
学习
ffmpeg
android so
qt
开发语言
c++
bmp转jpg
leetcode
算法
数据结构
回溯
backtracking
产品
优化
电子政务
电子商务
oracle
单片机