Results 1 to 13 of 13

Thread: send mail from qt app problem

  1. #1
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default send mail from qt app problem

    hi all...

    i'm currently developing an application that will send an email. ive'd search the forum for the similar topics, and found out some.

    i used the smtp code:

    smtp.cpp
    #include "smtp.h"
    #include <QAbstractSocket>

    Smtp::Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to, const QString &subject, const QString &body )
    {

    socket = new QTcpSocket( 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()));;

    message = "To: " + to + "\n";
    message.append("From: " + from + "\n");
    message.append("Subject: " + subject + "\n");
    message.append(body);
    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->connectToHost( strmailserver , port);
    if(socket->waitForConnected ( 10000 )){
    qDebug("connected");
    }

    t = new QTextStream( 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()
    {
    //output->append("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 );


    if ( state == Init && responseLine[0] == '2' )
    {
    // banner was okay, let's go on

    *t << "HELO there\r\n";
    t->flush();

    state = Mail;
    }
    else if ( state == Mail && responseLine[0] == '2' )
    {
    // HELO response was okay (well, it has to be)

    *t << "MAIL FROM: " << from << "\r\n";
    t->flush();
    state = Rcpt;
    }
    else if ( state == Rcpt && responseLine[0] == '2' )
    {

    *t << "RCPT TO: " << rcpt << "\r\n"; //r
    t->flush();
    state = Data;
    }
    else if ( state == Data && responseLine[0] == '2' )
    {

    *t << "DATA\r\n";
    t->flush();
    state = Body;
    }
    else if ( state == Body && responseLine[0] == '3' )
    {

    *t << message << "\r\n.\r\n";
    t->flush();
    state = Quit;
    }
    else if ( state == Quit && responseLine[0] == '2' )
    {

    *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 Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
    state = Close;
    }
    response = "";
    }
    Smtp::~Smtp()
    {
    delete t;
    delete socket;
    }
    smtp.h
    #ifndef SMTP_H
    #define SMTP_H

    #include <QTcpSocket>
    #include <QString>
    #include <QTextStream>
    #include <QDebug>
    #include <QMessageBox>

    class Smtp : public QObject
    {
    Q_OBJECT


    public:
    Smtp( const QString &strmailserver, const int &port, const QString &from, const QString &to,
    const QString &subject, const QString &body );
    ~Smtp();

    signals:
    void status( const QString &);

    private slots:
    void stateChanged(QAbstractSocket::SocketState socketState);
    void errorReceived(QAbstractSocket::SocketError socketError);
    void disconnected();
    void connected();
    void readyRead();

    private:
    QString message;
    QTextStream *t;
    QTcpSocket *socket;
    QString from;
    QString rcpt;
    QString response;
    enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
    int state;

    };
    #endif
    i call the class with this parameters:

    Smtp *sendMail = new Smtp( "my-mail-server", 25, "myemail@yahoo.com","receiveremail@yahoo.com", "Subject", "This is the message.");
    delete sendMail;
    during runtime the debug output is:

    stateChanged QAbstractSocket::HostLookupState
    stateChanged QAbstractSocket::ConnectingState
    stateChanged QAbstractSocket::ConnectedState
    Connected
    connected
    stateChanged QAbstractSocket::ClosingState
    stateChanged QAbstractSocket::UnconnectedState
    disconneted
    now, i check my email and i havent receive any mail. is there anyone here who knows how to fix this? thnks a lot..

  2. The following user says thank you to cutie.monkey for this useful post:

    brushycreek (30th September 2010)

  3. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: send mail from qt app problem

    Well, Smtp is working with signals/slots and is asynchronous. (It needs an QEventLoop running.)

    You delete the object right after creating it. You connect in the constructor (wait for some reasing synchronously for the connection to be established). Then you exit the constructor and the caller immediately deletes to smtp object. This a) causes a disconnect (good thing) and b) obviously prevents any data from being read in your read-Slot etc.

    Just do not delete the object (at least not until you're done).

  4. The following 2 users say thank you to caduel for this useful post:

    brushycreek (30th September 2010), cutie.monkey (27th January 2009)

  5. #3
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: send mail from qt app problem

    thnks.. its finally working.. i checked my code and found out that when i call the smtp class, i used delete sendMail which is wrong..

  6. #4
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: send mail from qt app problem

    hi! all, another question..

    using the smtp class, im trying to send mail to multiple recipients, that is, each email add is separated by a semi colon ';', but during runtime an error occured,

    Unexpected reply from SMTP server: 500 Syntax error, command unrecognized
    is there anyone here knows how to solve this error or knows another way to send email to multiple recipients, also with cc(carbon copy)?

    thank you very much!

  7. #5
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: send mail from qt app problem

    It's possible to attach file?

  8. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: send mail from qt app problem

    its clearly a command error..which means the data u r sending is wrong or formatted wrongly..hv a look at example of expanding a mailing list in:

    http://www.ietf.org/rfc/rfc0821.txt

    this will give a much better insight into everything u are doing and u'll be able to debug well.

  9. The following user says thank you to talk2amulya for this useful post:

    brushycreek (30th September 2010)

  10. #7
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send mail from qt app problem

    Hi,

    I too am using the same code for mailing. But I am still not able to send any mail. When I run the app, it is stayed on Connected state not going any further. I would be grateful to any help.

    Qt Code:
    1. #include "smtp.h"
    2.  
    3. Smtp::Smtp( const QString &from, const QString &to, const QString &subject, const QString &body )
    4. {
    5. socket = new QTcpSocket( this );
    6.  
    7. connect( socket, SIGNAL(readyRead()), this, SLOT( readyRead() ) );
    8. connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
    9. connect( socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
    10. SLOT(errorReceived(QAbstractSocket::SocketError)));
    11. connect( socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
    12. SLOT(stateChanged(QAbstractSocket::SocketState)));
    13. connect(socket, SIGNAL(disconnected()), this,
    14. SLOT(disconnected()));;
    15.  
    16. message = "To: " + to + "\n";
    17. message.append("From: " + from + "\n");
    18. message.append("Subject: " + subject + "\n");
    19. message.append(body);
    20. message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    21. message.replace( QString::fromLatin1( "\r\n.\r\n" ),
    22. QString::fromLatin1( "\r\n..\r\n" ) );
    23. this->from = from;
    24. rcpt = to;
    25. state = Init;
    26. this->Maddress = to.mid( to.indexOf('@',1)+1);
    27. QString s= "smtp.";
    28. s.append(this->Maddress);
    29. socket->connectToHost( s , 465);
    30.  
    31. if(socket->waitForConnected ( 40000 )) {qDebug("connected"); }
    32.  
    33. t = new QTextStream( socket );
    34. }
    35. Smtp::~Smtp()
    36. {
    37. delete t;
    38. delete socket;
    39. }
    40. void Smtp::stateChanged(QAbstractSocket::SocketState socketState)
    41. {
    42.  
    43. qDebug() <<"stateChanged " << socketState;
    44. }
    45.  
    46. void Smtp::errorReceived(QAbstractSocket::SocketError socketError)
    47. {
    48. qDebug() << "error " <<socketError;
    49. }
    50.  
    51. void Smtp::disconnected()
    52. {
    53.  
    54. qDebug() <<"disconneted";
    55. qDebug() << "error " << socket->errorString();
    56. }
    57.  
    58. void Smtp::connected()
    59. {
    60. //output->append("connected");
    61. qDebug() << "Connected ";
    62. }
    63.  
    64. void Smtp::readyRead()
    65. {
    66.  
    67. qDebug() <<"readyRead";
    68. // SMTP is line-oriented
    69.  
    70. QString responseLine;
    71. do
    72. {
    73. responseLine = socket->readLine();
    74. response += responseLine;
    75. }
    76. while ( socket->canReadLine() && responseLine[3] != ' ' );
    77.  
    78. responseLine.truncate( 3 );
    79.  
    80.  
    81. if ( state == Init && responseLine[0] == '2' )
    82. {
    83. // banner was okay, let's go on
    84.  
    85. *t << "HELO there\r\n";
    86. t->flush();
    87.  
    88. state = Mail;
    89. }
    90. else if ( state == Mail && responseLine[0] == '2' )
    91. {
    92. // HELO response was okay (well, it has to be)
    93.  
    94. *t << "MAIL FROM: " << from << "\r\n";
    95. t->flush();
    96. state = Rcpt;
    97. }
    98. else if ( state == Rcpt && responseLine[0] == '2' )
    99. {
    100.  
    101. *t << "RCPT TO: " << rcpt << "\r\n"; //r
    102. t->flush();
    103. state = Data;
    104. }
    105. else if ( state == Data && responseLine[0] == '2' )
    106. {
    107.  
    108. *t << "DATA\r\n";
    109. t->flush();
    110. state = Body;
    111. }
    112. else if ( state == Body && responseLine[0] == '3' )
    113. {
    114.  
    115. *t << message << "\r\n.\r\n";
    116. t->flush();
    117. state = Quit;
    118. }
    119. else if ( state == Quit && responseLine[0] == '2' )
    120. {
    121.  
    122. *t << "QUIT\r\n";
    123. t->flush();
    124. // here, we just close.
    125. state = Close;
    126. emit status( tr( "Message sent" ) );
    127. }
    128. else if ( state == Close )
    129. {
    130. deleteLater();
    131. return;
    132. }
    133. else
    134. {
    135. // something broke.
    136. QMessageBox::warning( 0, tr( "Qt Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
    137. state = Close;
    138. }
    139. response = "";
    140. }
    To copy to clipboard, switch view to plain text mode 
    #ifndef SMTP_H
    #define SMTP_H

    #include <QTcpSocket>
    #include <QString>
    #include <QTextStream>
    #include <QDebug>
    #include <QMessageBox>

    class Smtp : public QObject
    {
    Q_OBJECT


    public:
    Smtp( const QString &from, const QString &to,
    const QString &subject, const QString &body );
    ~Smtp();

    signals:
    void status( const QString &);

    private slots:
    void stateChanged(QAbstractSocket::SocketState socketState);
    void errorReceived(QAbstractSocket::SocketError socketError);
    void disconnected();
    void connected();
    void readyRead();

    private:
    QString message;
    QTextStream *t;
    QTcpSocket *socket;
    QString from;
    QString rcpt;
    QString Maddress;
    QString response;
    enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
    int state;

    };
    #endif
    stateChanged QAbstractSocket::HostLookupState
    stateChanged QAbstractSocket::ConnectingState
    stateChanged QAbstractSocket::ConnectedState
    Connected
    connected
    I am trying to send mail to xxx@gmail.com and from xx@hotmail.com

    Thank you,
    Baluk

  11. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send mail from qt app problem

    Your code is flawed. You can not assume that every mail server starts with 'smtp' and ends with a part of the users mail address.

    You will have to do a proper mail exchanger lookup and connect to that host instead. This doesn't cure all your problems however, as lots of servers block connections from ADSL/Cable IP addresses. To only way around this is to run your own server and connect to that one.

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: send mail from qt app problem

    Furthermore many servers reject email the first time it is sent and you have to resend it after some grace time. This is called graylisting. Some servers will just reject your mail because it is being sent from a wrong ip, etc.

    If you want to send mails directly from your application, you should be connecting to a server you know will accept your mail and then let it handle the mail routing. Then you don't have to do any server lookups and such.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #10
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: send mail from qt app problem

    Hello all! First I want to thank all of you. This information has been very helpful. With some minor modifications, I am now able to send an email via SMTP. The code that I am using was pasted from this forum and tweaked to get it to work with Qt4.6.2.

    I am having one issue though - my body text never gets transferred! What is happening? I get a subject line, but no body text. My ultimate goal is to attach a file, but I am a bit worried about accomplishing that if I cannot even get my body text to show up!

    It is this part of the code that I really do not understand. 1) Does it have to be done in this manner? 2) What is the purpose of the replaces? 3) How is SMTP getting my Subject Text, but not my Body Text? Any help is appreciated. (P.S. I am going to post this on another thread in order to make sure it gets seen).

    Qt Code:
    1. Smtp::Smtp( const QString &from, const QString &to, const QString &subject, const QString &body ) {
    2. ...
    3. sMessage = "To: " + to + "\n";
    4. sMessage.append("From: " + from + "\n");
    5. sMessage.append("Subject: " + subject + "\n");
    6. sMessage.append(body);
    7. sMessage.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    8. sMessage.replace( QString::fromLatin1( "\r\n.\r\n" ), QString::fromLatin1( "\r\n..\r\n" ) );
    9. sFrom = from;
    10. sTo = to;
    11. ...}
    To copy to clipboard, switch view to plain text mode 

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: send mail from qt app problem

    There needs to be an empty line between the last header ("Subject" in your case) and first line of the body of the message.

    (P.S. I am going to post this on another thread in order to make sure it gets seen).
    Please don't do that. We can see your message very well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    brushycreek (30th September 2010)

  16. #12
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Thumbs up Re: send mail from qt app problem

    Quote Originally Posted by wysota View Post
    There needs to be an empty line between the last header ("Subject" in your case) and first line of the body of the message.
    Excellent! Here are the results. I tried:
    Qt Code:
    1. sMessage.append("Subject: " + subject + "\n");
    2. sMessage.append("\r\n" + body);
    To copy to clipboard, switch view to plain text mode 

    which yielded body text and I tried:

    Qt Code:
    1. sMessage.append("Subject: " + subject + "\r\n");
    2. sMessage.append(body);
    To copy to clipboard, switch view to plain text mode 

    which also yielded body text. It seems that the "\r" is missing from all of the code snippets in the thread. Thank you! Btw, I am also getting:

    X-UIDL: 576795945
    X-IMail-ThreadID: b5a1038d0000ddec

    appended to the end of my body text. What is that?

    Please don't do that. We can see your message very well.
    Yes sir.

  17. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: send mail from qt app problem

    The \r are not missing at all, they are implemented near the end:
    Qt Code:
    1. sMessage.replace( QString::fromLatin1( "\n" ), QString::fromLatin1( "\r\n" ) );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 19:47
  4. how to read Email from INBOX
    By amit_pansuria in forum Qt Programming
    Replies: 22
    Last Post: 7th June 2007, 06:40
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.