当前位置:网站首页>qt send mail program
qt send mail program
2022-08-09 05:53:00 【come on, dude】
通过qt实现发送邮件,使用smtp进行发送.But can't send attachments.(functional research)
First we need to open the mailboxsmtp协议,以163邮箱为例
点击设置按钮
选择pop3选项
Then we need to turn on our computertelnet功能.
首先打开电脑的控制面板
点击程序.
Choose to enable or disable the feature.
将telentCheck this service and click OK.
测试telent是否可用.打开cmd命令窗口,输入telnet
If this window pops up, it means it is available
Turn on these services,And add an authorization code,The authorization code is what you passsmtp登录邮箱的密码.
Preliminary work is ready.
The code part is the passsmtpLog in to your mailbox and send emails.直接看代码.
smtp.h
#ifndef SMTP_H
#define SMTP_H
#include<QByteArray>
#include<QString>
#include<QTcpSocket>
class Smtp
{
public:
Smtp(QByteArray username,QByteArray password);
~Smtp();
void SendData(QByteArray sendIp,QByteArray s_Title,QByteArray s_Content);
QString WaitAndReadData();
private:
QByteArray m_UserName="";
QByteArray m_Password="";
QTcpSocket * m_pSocket=nullptr;
QString m_ReceiverData="";
};
#endif // SMTP_H
smtp.cpp
#include "smtp.h"
#include<QDebug>
Smtp::Smtp(QByteArray username,QByteArray password)
{
if(username.contains("@163"))
{
m_UserName= username;
m_Password = password;
}
else
{
qDebug()<<"Error";
}
}
void Smtp::SendData(QByteArray sendIp, QByteArray s_Title, QByteArray s_Content)
{
m_pSocket=new QTcpSocket();
m_pSocket->connectToHost("smtp.163.com",25,QTcpSocket::ReadWrite); //连接163邮箱
m_pSocket->waitForConnected(1000);
WaitAndReadData();
m_pSocket->write("helo localhost\r\n");
WaitAndReadData();
m_pSocket->write("auth login\r\n");
WaitAndReadData();
m_pSocket->write(m_UserName.toBase64()+"\r\n"); //写入用户名
WaitAndReadData();
m_pSocket->write(m_Password.toBase64()+"\r\n"); //写入密码
WaitAndReadData();
m_pSocket->write("mail from: <"+m_UserName+">\r\n"); //发送的邮箱
WaitAndReadData();
m_pSocket->write("rcpt to: <"+sendIp+">\r\n"); //接收的邮箱
WaitAndReadData();
m_pSocket->write("data\r\n"); //开始写入
WaitAndReadData();
m_pSocket->write("from:<"+m_UserName+">\r\n"); //发送名称
WaitAndReadData();
m_pSocket->write("to:<"+sendIp+">"); //接受名称
WaitAndReadData();
m_pSocket->write("data\r\n");
WaitAndReadData();
m_pSocket->write("Subject:"+s_Title+"\r\n"); //标题
m_pSocket->write("\r\n");
m_pSocket->write(s_Content.append("\r\n")); //内容
m_pSocket->write(".\r\n");
WaitAndReadData();
m_pSocket->write("quit\r\n");
m_pSocket->disconnect();
}
QString Smtp::WaitAndReadData()
{
m_pSocket->waitForReadyRead(1000);
m_ReceiverData = m_pSocket->readAll();
return m_ReceiverData;
}
Smtp::~Smtp()
{
delete m_pSocket;
}
好了,Then test our program.
Smtp smtp("邮箱名称","授权码"); //Use your own email and password //Note the authorization code,Not your password to log in to your email
smtp.SendData("[email protected]","你好","这是一个测试程序");
Email received.
边栏推荐
猜你喜欢
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
5G对物流行业的积极影响
MYSQL高级篇-----查询截取分析,锁机制,主从复制
STM32系列单片机使用心得
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
Distributed timing task framework xxl-job source code analysis
RNN-T
打开数字时代的门槛,元宇宙NFT商城开发解决方案
牛客每日刷题之链表
【微信小程序】5分钟实现微信小程序海报
随机推荐
如何让Win11两个屏幕任务栏都显示时间?
How to pass a two-dimensional array to a function in C language?
面向6G的大规模MIMO通信感知一体化:现状与展望
Chapter 3 Search and Graph Theory (1)
打开数字时代的门槛,元宇宙NFT商城开发解决方案
shell函数、数组
金仓数据库能否设置事务自动提交
Webgis地图框架的对比
74HC595 的使用
弄潮 Web3 欧易OKX全球「抢人」
直播电商平台开发,点击查看更多显示所有内容
【Word】Word 正文中同时加上下标
51 serial communication (on)
【微信小程序】5分钟实现微信小程序海报
STM32学习笔记(PWM输出)
untiy 倒计时
分布式定时任务框架 xxl-job 源码解析
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
Shell:数组和函数用法
kubernetes apparmor入门