Results 1 to 12 of 12

Thread: Sending Mail

  1. #1
    Join Date
    Dec 2007
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Sending Mail

    Hello...
    How can I send an e-mail from a QT4 application? Don't write the code, please explain it me how... I stopped where I can't find any QT4 Class which helps me
    Thanks, systemz89

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Sending Mail

    There is no class for smtp. I remember writing a simple similar class for pop3, so that's not a big deal to do it for smtp as well. And there are non-Qt classes for sending mails through smtp - you might use them.

  3. #3
    Join Date
    Dec 2007
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Re: Sending Mail

    Quote Originally Posted by wysota View Post
    There is no class for smtp. I remember writing a simple similar class for pop3, so that's not a big deal to do it for smtp as well. And there are non-Qt classes for sending mails through smtp - you might use them.
    And where can I find some non-Qt classes?
    And what about QTcpSocket (QNetwork module) class?
    Last edited by systemz89; 9th December 2007 at 11:30. Reason: I started to read about networking in Qt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Sending Mail

    You can use QTcpSocket, but it's just a socket - you need to implement the whole smtp protocol yourself. As for the classes - ask google

  5. #5
    Join Date
    Dec 2007
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Re: Sending Mail

    I find on the internet a source code:

    Qt Code:
    1. #ifndef SMTP_H
    2. #define SMTP_H
    3.  
    4. #include <QApplication>
    5. #include <QMessageBox>
    6. #include <QtDebug>
    7. #include <QString>
    8. #include <QObject>
    9. #include <QTcpSocket>
    10. #include <QTextStream>
    11.  
    12. class Smtp : public QObject
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Smtp( const QString &from, const QString &to, const QString
    18. &subject, const QString &body );
    19. ~Smtp();
    20.  
    21. signals:
    22. void status( const QString & );
    23.  
    24. private slots:
    25. void readyRead();
    26. void connected();
    27.  
    28. void stateChanged( QAbstractSocket::SocketState socketState );
    29. void errorReceived( QAbstractSocket::SocketError socketError );
    30. void disconnected();
    31.  
    32. private:
    33. enum State {
    34. Init,
    35. Mail,
    36. Rcpt,
    37. Data,
    38. Body,
    39. Quit,
    40. Close
    41. };
    42.  
    43. QString message;
    44. QString from;
    45. QString rcpt;
    46. QTcpSocket * socket;
    47. int state;
    48. QString response;
    49. };
    50.  
    51.  
    52. Smtp::Smtp( const QString &from, const QString &to, const QString
    53. &subject, const QString &body )
    54. {
    55. socket = new QTcpSocket( this );
    56. connect( socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
    57. connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
    58. connect( socket, SIGNAL( error( QAbstractSocket::SocketError) ),
    59. this, SLOT( errorReceived( QAbstractSocket::SocketError ) ) );
    60. connect( socket, SIGNAL( stateChanged( QAbstractSocket::SocketState)
    61. ), this, SLOT( stateChanged( QAbstractSocket::SocketState ) ) );
    62. connect( socket, SIGNAL( disconnectedFromHost()), this,
    63. SLOT(disconnected() ) );
    64. message = QString::fromLatin1( "From: " ) +from
    65. +QString::fromLatin1( "\nTo: " ) +to +QString::fromLatin1( "\nSubject: "
    66. ) +subject +QString::fromLatin1( "\n\n" ) +body +"\n";
    67. message.replace( QString::fromLatin1( "\n" ), QString::fromLatin1(
    68. "\r\n" ) );
    69. message.replace( QString::fromLatin1( "\r\n.\r\n" ),
    70. QString::fromLatin1( "\r\n..\r\n" ) );
    71. this->from = from;
    72. rcpt = to;
    73. state = Init;
    74.  
    75. // change this to correct email server address
    76. socket->connectToHost( "xx.xx.xx.xxx", 25 );
    77.  
    78. if( socket->waitForConnected( 30000 ) )
    79. qDebug()("connected");
    80.  
    81. t = new QTextStream( socket );
    82. }
    83.  
    84. Smtp::~Smtp()
    85. {
    86. delete t;
    87. delete socket;
    88. }
    89. void Smtp::stateChanged( QAbstractSocket::SocketState socketState)
    90. {
    91. qDebug() <<"stateChanged " << socketState;
    92. }
    93.  
    94. void Smtp::errorReceived( QAbstractSocket::SocketError socketError)
    95. {
    96. qDebug() << "error " <<socketError;
    97. }
    98.  
    99. void Smtp::disconnected()
    100. {
    101. qDebug() <<"disconneted";
    102. qDebug() << "error " << socket->errorString();
    103. }
    104.  
    105. void Smtp::connected()
    106. {
    107. qDebug() << "Connected ";
    108. }
    109.  
    110. void Smtp::readyRead()
    111. {
    112. if( !socket->canReadLine() )
    113. return;
    114.  
    115. QString responseLine;
    116.  
    117. do {
    118. responseLine = socket->readLine();
    119. response += responseLine;
    120. } while( socket->canReadLine() && responseLine[3] != ' ' );
    121.  
    122. responseLine.truncate( 3 );
    123.  
    124. if ( state == Init && responseLine[0] == '2' )
    125. {
    126. // banner was okay, let's go on
    127. *t << "HELO there\r\n";
    128. t->flush();
    129. state = Mail;
    130. }
    131. else if ( state == Mail && responseLine[0] == '2' )
    132. {
    133. // HELLO response was okay (well, it has to be)
    134. *t << "MAIL FROM: <" << from << ">\r\n";
    135. t->flush();
    136. state = Rcpt;
    137. }
    138. else if ( state == Rcpt && responseLine[0] == '2' )
    139. {
    140.  
    141. *t << "RCPT TO: <" << rcpt << ">\r\n";
    142. t->flush();
    143. state = Data;
    144. }
    145. else if ( state == Data && responseLine[0] == '2' )
    146. {
    147. *t << "DATA\r\n";
    148. t->flush();
    149. state = Body;
    150. }
    151. else if ( state == Body && responseLine[0] == '3' )
    152. {
    153. *t << message << ".\r\n";
    154. t->flush();
    155. state = Quit;
    156. }
    157. else if ( state == Quit && responseLine[0] == '2' )
    158. {
    159. *t << "QUIT\r\n";
    160. // here, we just close.
    161. state = Close;
    162. //t->flush();
    163. emit status( tr( "Message sent" ) );
    164. }
    165. else if ( state == Close )
    166. {
    167. deleteLater();
    168. return;
    169. }
    170. else
    171. {
    172. // something broke.
    173. QMessageBox::warning( qApp->activeWindow(), tr( "Qt Mail Example" ), tr( "Unexpected reply from SMTP server:\n\n" ) + response );
    174. state = Close;
    175. }
    176. response = "";
    177. }
    178.  
    179. #endif
    To copy to clipboard, switch view to plain text mode 

    This code is in Qt3 and I can't correct it to Qt4... How can I resolve the convertion from Qt3 to Qt4?
    Last edited by systemz89; 9th December 2007 at 12:10. Reason: missing [code] tags

  6. The following user says thank you to systemz89 for this useful post:

    noktus (8th August 2008)

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Sending Mail

    This code is Qt4 code.

  8. #7
    Join Date
    Dec 2007
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Re: Sending Mail

    Quote Originally Posted by wysota View Post
    This code is Qt4 code.
    Ok.
    I get the following error on compile:
    Qt Code:
    1. smtp.h: In constructor &#8216;Smtp::Smtp(const QString&, const QString&, const QString&, const QString&)’:
    2. smtp.h:79: error: no match for call to &#8216;(QDebug) (const char [10])’
    3. make: *** [main.o] Error 1
    To copy to clipboard, switch view to plain text mode 

    Here is the problem:
    Qt Code:
    1. if( socket->waitForConnected( 30000 ) )
    2. qDebug()("connected");
    To copy to clipboard, switch view to plain text mode 

    And won't work with
    Qt Code:
    1. if( socket->waitForConnected( 30000 ) )
    2. qDebug() << "connected";
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by systemz89; 9th December 2007 at 12:27. Reason: updated contents

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Sending Mail

    include <QtDebug>

  10. #9
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending Mail

    Hi,
    Please write details of your Qt version and OS. If it is Linix, Then you configure a mail server in your system and write the details to a file, What you need to send as a mail and use this command for send it as a mail
    system("cat YourFileName.txt | mail -s \" Your Subject \" to_email_id@xxx.com");

    After this you remove your test file.

  11. #10
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending Mail

    The Smtp class which systemz89 provided is working. Thanx for saving me time.
    Only changed to qDebug() << "connected"; made the class separated to .h and .cpp, and added main.cpp for execution. Then i tested it with local smtp server and it worked.
    Now i should think how to make TLS AUTH to some smtp server too.
    Last edited by sadjoker; 15th June 2008 at 02:25.

  12. #11
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sending Mail

    Quote Originally Posted by sadjoker View Post
    The Smtp class which systemz89 provided is working. Thanx for saving me time.
    Only changed to qDebug() << "connected"; made the class separated to .h and .cpp, and added main.cpp for execution. Then i tested it with local smtp server and it worked.
    Now i should think how to make TLS AUTH to some smtp server too.

    Have a look on:
    http://qt-webdav.svn.sourceforge.net..._auth_console/
    i write this chunk and work on qmail server auth smtp + formated html text mail

  13. #12
    Join Date
    Feb 2008
    Posts
    50
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending Mail

    thanx, i`ll have a look.
    I decided to try the next thing:
    i got the "Free SMTP Server" app... i`ll try to call it with QProcess from my app, then send the mail trough it and close the process. no need to use anyone else smtp servers.

Similar Threads

  1. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 19:47
  2. how to read Email from INBOX
    By amit_pansuria in forum Qt Programming
    Replies: 22
    Last Post: 7th June 2007, 06:40
  3. Sending mail using Qt
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2006, 11:17
  4. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23
  5. Did you people get the unsolicited mail? - "Release of Makedoc"
    By jamadagni in forum General Discussion
    Replies: 5
    Last Post: 14th February 2006, 12:46

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.