Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems sending email
Qt 6.11 is out! See what's new in the release blog

Problems sending email

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GGILOYAN
    wrote on last edited by
    #1

    Hi everyone, please help

    smtp.h
    @#ifndef SMTP_H
    #define SMTP_H
    #include <QtNetwork/QAbstractSocket>
    #include <QtNetwork/QSslSocket>
    #include <QString>
    #include <QTextStream>
    #include <QDebug>
    #include <QtWidgets/QMessageBox>
    #include <QByteArray>
    #include <QFile>
    #include <QFileInfo>
    class Smtp : public QObject
    {
    Q_OBJECT
    public:
    Smtp( const QString &user, const QString &pass,
    const QString &host, int port = 465, int timeout = 30000 );
    ~Smtp();
    void sendMail( const QString &from, const QString &to,
    const QString &subject, const QString &body,
    QStringList files = QStringList());
    signals:
    void status( const QString &);
    private slots:
    void stateChanged(QAbstractSocket::SocketState socketState);
    void errorReceived(QAbstractSocket::SocketError socketError);
    void disconnected();
    void connected();
    void readyRead();
    private:
    int timeout;
    QString message;
    QTextStream *t;
    QSslSocket *socket;
    QString from;
    QString rcpt;
    QString response;
    QString user;
    QString pass;
    QString host;
    int port;
    enum states{Tls, HandShake ,Auth,User,Pass,Rcpt,Mail,Data,Init,Body,Quit,Close};
    int state;
    };
    #endif
    @
    smtp.cpp
    @#include "smtp.h"
    Smtp::Smtp( const QString &user, const QString &pass, const QString &host, int port, int timeout )
    {
    socket = new QSslSocket(this);
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(socket, SIGNAL(connected()), this, SLOT(connected() ) );
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(errorReceived(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
    connect(socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
    this->user = user;
    this->pass = pass;
    this->host = host;
    this->port = port;
    this->timeout = timeout;
    }
    void Smtp::sendMail(const QString &from, const QString &to, const QString &subject, const QString &body, QStringList files)
    {
    message = "To: " + to + "\n";
    message.append("From: " + from + "\n");
    message.append("Subject: " + subject + "\n");
    message.append("MIME-Version: 1.0\n");
    message.append("Content-Type: multipart/mixed; boundary=frontier\n\n");
    message.append( "--frontier\n" );
    message.append( "Content-Type: text/plain\n\n" );
    message.append(body);
    message.append("\n\n");
    if(!files.isEmpty())
    {
    qDebug() << "Files to be sent: " << files.size();
    foreach(QString filePath, files)
    {
    QFile file(filePath);
    if(file.exists())
    {
    if (!file.open(QIODevice::ReadOnly))
    {
    qDebug("Couldn't open the file");
    QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Couldn't open the file\n\n" ) );
    return ;
    }
    QByteArray bytes = file.readAll();
    message.append( "--frontier\n" );
    message.append( "Content-Type: application/octet-stream\nContent-Disposition: attachment; filename="+ QFileInfo(file.fileName()).fileName() +";\nContent-Transfer-Encoding: base64\n\n" );
    message.append(bytes.toBase64());
    message.append("\n");
    }
    }
    }
    else
    qDebug() << "No attachments found";
    message.append( "--frontier--\n" );
    message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    message.replace( QString::fromLatin1( "\r\n.\r\n" ),QString::fromLatin1( "\r\n..\r\n" ) );
    this->from = from;
    rcpt = to;
    state = Init;
    socket->connectToHostEncrypted(host, port); //"smtp.gmail.com" and 465 for gmail TLS
    if (!socket->waitForConnected(timeout)) {
    qDebug() << socket->errorString();
    }
    t = new QTextStream( socket );
    }
    Smtp::~Smtp()
    {
    delete t;
    delete socket;
    }
    void Smtp::stateChanged(QAbstractSocket::SocketState socketState)
    {
    qDebug() <<"stateChanged " << socketState;
    }
    void Smtp::errorReceived(QAbstractSocket::SocketError socketError)
    {
    qDebug() << "error " <<socketError;
    }
    void Smtp::disconnected()
    {
    qDebug() <<"disconneted";
    qDebug() << "error " << socket->errorString();
    }
    void Smtp::connected()
    {
    qDebug() << "Connected ";
    }
    void Smtp::readyRead()
    {
    qDebug() <<"readyRead";
    // SMTP is line-oriented

    QString responseLine;
    do
    {
        responseLine = socket->readLine();
        response += responseLine;
    }
    while ( socket->canReadLine() && responseLine[3] != ' ' );
    
    responseLine.truncate( 3 );
    
    qDebug() << "Server response code:" <<  responseLine;
    qDebug() << "Server response: " << response;
    
    if ( state == Init && responseLine == "220" )
    {
        // banner was okay, let's go on
        *t << "EHLO localhost" <<"\r\n";
        t->flush();
    
        state = HandShake;
    }
    else if ( state == Quit && responseLine == "250" )
    {
    
        *t << "QUIT\r\n";
        t->flush();
        // here, we just close.
        state = Close;
        emit status( tr( "Message sent" ) );
    }
    else if ( state == Close )
    {
        deleteLater();
        return;
    }
    else
    {
        // something broke.
        QMessageBox::warning( 0, tr( "Qt Simple SMTP client" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
        state = Close;
        emit status( tr( "Failed to send message" ) );
    }
    response = "";
    

    }
    @

    When I call Smtp::sendMail function it's works without any error
    socket->waitForConnected(timeout) returns true, but email doesn't sent.
    Smtp settings I wrote rigth.

    Why doesn't send email and when should be burn readyRead function??

    PLease help.

    Thanks!

    1 Reply Last reply
    0
    • mrdebugM Offline
      mrdebugM Offline
      mrdebug
      wrote on last edited by
      #2

      I have write a smtp component without ssl (up to now) that works very well. I can send mails in html format with attached images (as body or as attach).
      If you have no time to spend on it I can help you.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GGILOYAN
        wrote on last edited by
        #3

        Thank you.
        this is my email address:ggiloyan@gmail.com
        PLease send me the example, i added your skype contact!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved