Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include "smtp.h"
  2. #include <QtGui>
  3. #include <QtNetwork/QSslSocket>
  4.  
  5. smtp::smtp(QObject *parent) :
  6.     QObject(parent)
  7. {
  8.     conn = 0;
  9.     m_user = "";
  10.     m_password = "";
  11.     server = "";
  12.     port = 25;
  13.     SSL = false;
  14.     sending = false;
  15. }
  16. void smtp::setUser(QString user, QString password){
  17.     m_user = user;
  18.     m_password = password;
  19. }
  20.  
  21. void smtp::configureServer(QString server, int port=25, bool useSSL = false){
  22.     this->server = server;
  23.     this->port = port;
  24.     this->SSL = useSSL;
  25. }
  26.  
  27. void smtp::setMessage(QString subject, QString text){
  28.     subject = subject.replace("\r\n","\n");
  29.     body = text.replace("\r\n","\n");
  30. }
  31.  
  32. smtp_error smtp::send(){
  33.     if (server.isEmpty()){
  34.         emit sent(INVALID_SERVER);
  35.         return INVALID_SERVER;
  36.     }
  37.     if (!sending){
  38.         sending = true;
  39.         if (conn) delete conn;
  40.         conn = new QSslSocket(this);
  41.         if (SSL){
  42.             conn->connectToHostEncrypted(server,port);
  43.         }
  44.         else conn->connectToHost(server,port);
  45.     }
  46.     connect(conn,SIGNAL(readyRead()),this,SLOT(onRead()));
  47.     connect(conn,SIGNAL(connected()),this,SLOT(onConnect()));
  48.     connect(conn,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onConnectError()));
  49.    
  50.  
  51. }
  52. void smtp::onRead(){
  53.    
  54.     while(conn->isReadable()){
  55.         QByteArray buf;
  56.         buf = conn->readLine();
  57.         if (buf.contains("AUTH")){
  58.            QByteArray a;
  59.            a.append(m_user);
  60.            a.append('\0');
  61.            a.append(m_user);
  62.            a.append('\0');
  63.            a.append(m_password);
  64.    
  65.            QString coded = a.toBase64();
  66.            conn->write(QString("AUTH PLAIN %1\n").arg(coded).toAscii());
  67.            emit sendState(1);
  68.        }
  69.        else if (buf.startsWith("235")){ //auth accepted
  70.            conn->write(QString("MAIL FROM: <%1>\n").arg(m_user).toAscii());      
  71.            foreach(coded,recipients){
  72.                conn->write(QString("RCPT TO: <%1>\n").arg(coded).toAscii());
  73.            }
  74.            emit sendState(2);
  75.            conn->write("DATA\n");
  76.        }
  77.        else if(buf.startsWith("535")){ //wrong password
  78.            emit sent(INVALID_USER);  
  79.            conn->close();
  80.            sending = false;
  81.        }
  82.        else if (buf.contains("354")) { //mail body
  83.            /*from: <Nome_do_Remetente>
  84.            cc: <destinatario_x1>
  85.            bcc: <destinatario_x2>
  86.            Date: dd mmm aaaa hh:mm:ss +0000
  87.            subject: <Assunto>*/
  88.            conn->write(QString(
  89.                         "from: <%1>\n"
  90.                         "subject: <%1>\n\n"
  91.                        ).arg(m_user).arg(subject).toAscii());
  92.            conn->write(body);
  93.            conn->write("\r\n.\r\n");
  94.            
  95.                
  96.        }
  97.        else if(buf.startsWith("250 2.0.0")){ // sent
  98.            emit sent(SUCESS);
  99.            conn->close();
  100.            sending = false;
  101.        }
  102.        
  103.     }
  104.    
  105. }
  106. void smtp::onConnect()
  107. {
  108.     conn->write("EHLO\n");
  109.     emit sendState(0);
  110.  
  111. }
  112. void smtp::onConnectError(){
  113.     sending = false;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement