当前位置:   article > 正文

Qt 写一个邮件发送程序

Qt 写一个邮件发送程序

最近在完成一个邮箱代替的告警功能,写了一个邮件发送的demo

 

 

以下为代码:

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include<QTcpSocket>
  5. namespace Ui {
  6. class MainWindow;
  7. }
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MainWindow(QWidget *parent = nullptr);
  13. ~MainWindow();
  14. //用来等待服务器响应
  15. void waitAndReadData();
  16. //功能函数
  17. void sendemil_fun();
  18. //登陆smtp服务器时所需的id和password,需要是base64编码格式
  19. QByteArray name;
  20. QByteArray passwd;
  21. //发送的标题和内容
  22. QByteArray s_Title;
  23. QByteArray s_Content;
  24. //发送邮件的邮件地址和接收地址,发送地址就是登陆的
  25. QByteArray sendemail;
  26. QByteArray rcvemail;
  27. private:
  28. Ui::MainWindow *ui;
  29. QTcpSocket *m_pSocket;
  30. QString m_receiverData;
  31. };
  32. #endif // MAINWINDOW_H
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include<QString>
  4. #include <QtNetwork>
  5. #include<QtDebug>
  6. MainWindow::MainWindow(QWidget *parent) :
  7. QMainWindow(parent),
  8. ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. m_pSocket=new QTcpSocket();
  12. //此程序发送邮件作为主线程,会堵塞窗口,可以根据情况来使用子线程来运行,demo先使用主线程来完成邮件发送
  13. //点击发送,获取所需参数,进行发送
  14. connect(ui->pushButton,&QPushButton::clicked,this,[=]{
  15. this->m_pSocket = new QTcpSocket;
  16. this->name = ui->logid_lineEdit->text().toUtf8().toBase64();
  17. this->passwd = ui->logpasswd_lineEidt->text().toUtf8().toBase64();
  18. this->sendemail = ui->SendEmail_lineEdit->text().toUtf8();
  19. this->rcvemail=ui->Rcvemail_lineEdit->text().toUtf8();
  20. m_receiverData = ui->subject_lineEdit->text().toUtf8();
  21. s_Content = ui->textEdit_Text->toPlainText().toUtf8();
  22. sendemil_fun();
  23. });
  24. }
  25. MainWindow::~MainWindow()
  26. {
  27. delete ui;
  28. delete m_pSocket;
  29. }
  30. void MainWindow::waitAndReadData()
  31. {
  32. m_pSocket->waitForReadyRead(1000);
  33. m_receiverData = m_pSocket->readAll();
  34. //将其显示在服务器反馈TextEdit中
  35. qDebug()<<m_receiverData<<endl;
  36. ui->server_textEdit->append(m_receiverData);
  37. }
  38. void MainWindow::sendemil_fun()
  39. {
  40. m_pSocket->connectToHost("smtp.qq.com",25,QTcpSocket::ReadWrite); //连接qq邮箱
  41. m_pSocket->waitForConnected(1000);
  42. waitAndReadData();
  43. m_pSocket->write("helo yuanzhaoyi\r\n");
  44. waitAndReadData();
  45. m_pSocket->write("auth login\r\n");
  46. waitAndReadData();
  47. m_pSocket->write(name+"\r\n"); //写入用户名
  48. waitAndReadData();
  49. m_pSocket->write(passwd+"\r\n"); //写入密码
  50. waitAndReadData();
  51. m_pSocket->write("mail from: <"+sendemail+">\r\n"); //发送的邮箱
  52. waitAndReadData();
  53. m_pSocket->write("rcpt to: <"+rcvemail+">\r\n"); //接收的邮箱
  54. waitAndReadData();
  55. m_pSocket->write("data\r\n"); //开始写入
  56. waitAndReadData();
  57. m_pSocket->write("from:<"+sendemail+">\r\n"); //发送名称
  58. waitAndReadData();
  59. m_pSocket->write("to:<"+rcvemail+">"); //接收名称
  60. waitAndReadData();
  61. m_pSocket->write("data\r\n");
  62. waitAndReadData();
  63. m_pSocket->write("Subject:"+s_Title+"\r\n"); //标题
  64. m_pSocket->write("\r\n");
  65. m_pSocket->write(s_Content.append("\r\n")); //内容
  66. m_pSocket->write(".\r\n");
  67. waitAndReadData();
  68. m_pSocket->write("quit\r\n");
  69. m_pSocket->disconnect();
  70. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/282900
推荐阅读
相关标签
  

闽ICP备14008679号