Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: Sending email using Qt.

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

    Default Re: Sending email using Qt.

    Is the server working correctly? You can tell easily by using another email client and trying to send some mail.

  2. #22
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending email using Qt.

    Hi,

    I am trying to send mail to gmail server. I don't need my own server.

    Thank You,
    Baluk

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

    Default Re: Sending email using Qt.

    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 

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

    Default Re: Sending email using Qt.

    EDIT: Please don't post in multiple topics. It's considered rude. I posted a reply here only to notice that Wysota posted a reply in your other thread.

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

    Default Re: Sending email using Qt.

    Quote Originally Posted by squidge View Post
    EDIT: Please don't post in multiple topics. It's considered rude. I posted a reply here only to notice that Wysota posted a reply in your other thread.
    Yes, yes, yes. I have already had my hand slapped. Won't happen again. Thank you for responding.

  6. #26
    Join Date
    Sep 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Sending email using Qt.

    Hi am new in qt..My problem is ..My application does connect to server but its not realy sending the emails that i want it to sent.Any help me

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

    Default Re: Sending email using Qt.

    This thread has not had anyone post to it since September 2011. I am curious how many of the above posters were able to get their e-mail functionality working? After a push in the right direction from you guys, plus reading a book on SMTP and some additional web help on Enhanced SMTP, I was able to create a very robust and flexible e-mail client within my application. I have the option to require user authentication (Ehelo) and I can send attachments, which was the real reason that I even pursued having e-mail capabilities in the first place. The bottom line is that not only it is possible to send e-mails using SMTP/Enhanced SMTP, but it is possible to send attachments as well. The number of attachments and the file type does not matter. I hope you were all successful in your pursuit. Thanks for your input.

  8. #28
    Join Date
    Feb 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending email using Qt.

    thank you very much

  9. #29
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Smile Re: Sending email using Qt.

    Can someone post some working code please? It'd be much appreciated and save the rest of us a lot of time and effort.
    Sure, we could dig at this for a few days and get it to eventually work -- but why climb a mountain when someone's already built an escalator?

    Cheers in advance

  10. #30
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Sending email using Qt.

    Also you can send e-mail by means of Qxt library http://libqxt.bitbucket.org. Namely, one can use
    int send ( const QxtMailMessage & message )
    of QxtSmtp class.

  11. #31
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending email using Qt.

    Quote Originally Posted by nielsenj View Post
    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
    Hi, how I can add attachment to this code ?
    Regards

  12. #32
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending email using Qt.

    Hello guys,
    I am trying to send an email using the code in post#9, i did make the modifiction that were mentioned in post#11. I was trying to send a msg using gmail.
    so what i did was :
    Smtp = *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailPass ward");
    newMail->send("mygmail@gamil.com","mygmail@gamil.com","sub ject","body txt");

    but i the connectoin does not want to establish
    last msg i got is :

    ###Config server smtp connect to .............. "smtp.gmail.com"

    can you please tell me what is wrong am i doing ??

    thanks

  13. #33
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sending email using Qt.

    so what i did was :

    Smtp = *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailP ass ward");
    Really? How did you get your compiler to accept that?

  14. #34
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending email using Qt.

    Really? How did you get your compiler to accept that?
    Man, take it easy it is just a typing mistake.

    Smtp *newMail = new ("smtp.gmail.com","mygmail@gamil.com","mygmailPass ward");
    Last edited by IwantToLearen; 12th February 2013 at 21:20.

  15. #35
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    231
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Sending email using Qt.

    Hi,

    I have questions about post #3 of this theme.

    1. What is the server this:

    Qt Code:
    1. socket->connectToHost( "smtp.yourserver.com", 25);
    To copy to clipboard, switch view to plain text mode 

    What have I write instead "smtp.yourserver.com"

    2. What is the "output" variable? Is it for example?

    Thank you!

Similar Threads

  1. Replies: 9
    Last Post: 23rd August 2012, 01:01
  2. Replies: 7
    Last Post: 5th January 2009, 09:27
  3. Sending Mail through QProcess
    By KaptainKarl in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2008, 22:51
  4. sending encrypted data via mail using smtp
    By vermarajeev in forum General Programming
    Replies: 20
    Last Post: 14th August 2007, 20:47
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.