Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "smtp.h"
- #include <QtGui>
- #include <QtNetwork/QSslSocket>
- smtp::smtp(QObject *parent) :
- QObject(parent)
- {
- conn = 0;
- m_user = "";
- m_password = "";
- server = "";
- port = 25;
- SSL = false;
- sending = false;
- }
- void smtp::setUser(QString user, QString password){
- m_user = user;
- m_password = password;
- }
- void smtp::configureServer(QString server, int port=25, bool useSSL = false){
- this->server = server;
- this->port = port;
- this->SSL = useSSL;
- }
- void smtp::setMessage(QString subject, QString text){
- subject = subject.replace("\r\n","\n");
- body = text.replace("\r\n","\n");
- }
- smtp_error smtp::send(){
- if (server.isEmpty()){
- emit sent(INVALID_SERVER);
- return INVALID_SERVER;
- }
- if (!sending){
- sending = true;
- if (conn) delete conn;
- conn = new QSslSocket(this);
- if (SSL){
- conn->connectToHostEncrypted(server,port);
- }
- else conn->connectToHost(server,port);
- }
- connect(conn,SIGNAL(readyRead()),this,SLOT(onRead()));
- connect(conn,SIGNAL(connected()),this,SLOT(onConnect()));
- connect(conn,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onConnectError()));
- }
- void smtp::onRead(){
- while(conn->isReadable()){
- QByteArray buf;
- buf = conn->readLine();
- if (buf.contains("AUTH")){
- QByteArray a;
- a.append(m_user);
- a.append('\0');
- a.append(m_user);
- a.append('\0');
- a.append(m_password);
- QString coded = a.toBase64();
- conn->write(QString("AUTH PLAIN %1\n").arg(coded).toAscii());
- emit sendState(1);
- }
- else if (buf.startsWith("235")){ //auth accepted
- conn->write(QString("MAIL FROM: <%1>\n").arg(m_user).toAscii());
- foreach(coded,recipients){
- conn->write(QString("RCPT TO: <%1>\n").arg(coded).toAscii());
- }
- emit sendState(2);
- conn->write("DATA\n");
- }
- else if(buf.startsWith("535")){ //wrong password
- emit sent(INVALID_USER);
- conn->close();
- sending = false;
- }
- else if (buf.contains("354")) { //mail body
- /*from: <Nome_do_Remetente>
- cc: <destinatario_x1>
- bcc: <destinatario_x2>
- Date: dd mmm aaaa hh:mm:ss +0000
- subject: <Assunto>*/
- conn->write(QString(
- "from: <%1>\n"
- "subject: <%1>\n\n"
- ).arg(m_user).arg(subject).toAscii());
- conn->write(body);
- conn->write("\r\n.\r\n");
- }
- else if(buf.startsWith("250 2.0.0")){ // sent
- emit sent(SUCESS);
- conn->close();
- sending = false;
- }
- }
- }
- void smtp::onConnect()
- {
- conn->write("EHLO\n");
- emit sendState(0);
- }
- void smtp::onConnectError(){
- sending = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement