Results 1 to 20 of 36

Thread: Sending email using Qt.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Mar 2006
    Posts
    2
    Thanks
    2
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending email using Qt.

    Quote Originally Posted by johnny_sparx
    Is there a simple way to send email using Qt?
    I see that it can be done by using ActiveQt to control Outlook. I am not interested in doing this. Is there another way?
    Depending on the version of QT, the 3.x series had a smtp mail example in the "networking" examples.

    The modified structure for QT 4.x is below:

    smtp.h

    Qt Code:
    1. /****************************************************************************
    2. ** $Id: qt/smtp.h 3.3.6 edited Aug 31 2005 $
    3. **
    4. ** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
    5. **
    6. ** This file is part of an example program for Qt. This example
    7. ** program may be used, distributed and modified without limitation.
    8. **
    9. *****************************************************************************/
    10.  
    11. #ifndef SMTP_H
    12. #define SMTP_H
    13.  
    14. #include <QTcpSocket>
    15. #include <QString>
    16. #include <QTextStream>
    17. #include <QDebug>
    18. #include <QMessageBox>
    19.  
    20. class Smtp : public QObject
    21. {
    22. Q_OBJECT
    23.  
    24.  
    25. public:
    26. Smtp( const QString &from, const QString &to,
    27. const QString &subject, const QString &body );
    28. ~Smtp();
    29.  
    30. signals:
    31. void status( const QString &);
    32.  
    33. private slots:
    34. void stateChanged(QTcpSocket::SocketState socketState);
    35. void errorReceived(QTcpSocket::SocketError socketError);
    36. void disconnected();
    37. void connected();
    38. void readyRead();
    39.  
    40. private:
    41. QString message;
    42. QTcpSocket *socket;
    43. QString from;
    44. QString rcpt;
    45. QString response;
    46. enum states{Rcpt,Mail,Data,Init,Body,Quit,Close};
    47. int state;
    48.  
    49. };
    50. #endif
    To copy to clipboard, switch view to plain text mode 

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

    To send a mail just use:
    Qt Code:
    1. Smtp *newMail = new Smtp("from@address.com","to@address.com"," Your Subject","My body text");
    2. delete newMail;
    To copy to clipboard, switch view to plain text mode 

    Please note: change the server address in "smtp.cpp" to your local server, most of the time localhost will work.. remote hosts should also work though.

    As well, this may not work right off the bat, i speciallized it to work for my program.. i just hacked it back together for an example. The code is essentially correct, i just usually include the "To: BLAH\n", "From: BLAH\n" and "Subject: BLAH\n" within my message body.. so if there is something wrong it should be in that area.

    Good Luck!

    J

  2. The following 5 users say thank you to nielsenj for this useful post:

    lllturtle (16th August 2011), Persoontje (23rd December 2007), scary hallo (9th January 2014), sunil.thaha (18th December 2006), tablebubble (13th September 2011)

Similar Threads

  1. Replies: 9
    Last Post: 23rd August 2012, 00:01
  2. Replies: 7
    Last Post: 5th January 2009, 08:27
  3. Sending Mail through QProcess
    By KaptainKarl in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2008, 21:51
  4. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 19:47
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

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
  •  
Qt is a trademark of The Qt Company.