Results 1 to 20 of 20

Thread: Sending mail - problem with authentication

  1. #1
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sending mail - problem with authentication

    Hello. I wanted to add an option to my application, which lets user to send e-mail to my mailbox (my mail adress is set in the source code). I found the source code for this feature on the net and everything is quite good until... the SMTP server is sending me responses like: "Server XYZ requires Authentication .... blah blah". As SMTP server I'm using popular mail services' servers and they obviously requires Authentication. What does it mean? How to do the Authentication? I tried to look for servers withoud this need but the results were terrible - I haven't found anything. I can't use localhost, because it won't work for other users (who don't have servers installed).

    Help me, please . Thanks in advance.
    Last edited by kremuwa; 9th March 2010 at 11:42.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mail - problem with authentication

    It looks like that you have to provide a username and a password before being able to use the SMTP protocol.

  3. #3
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    Ok, so here is the code (it comes from this forum).

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

    The way I'm using it:
    Qt Code:
    1. Smtp *newMail = new Smtp("anonymous@gall.com","recipient@mail.com","Such a title","Such mail content");
    To copy to clipboard, switch view to plain text mode 

    What should I change to make it work?

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mail - problem with authentication

    You still have to provide authentification if the SMTP requires one.

  5. #5
    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: Sending mail - problem with authentication

    Quote Originally Posted by kremuwa View Post
    What should I change to make it work?
    Do you expect us to do your work for you?

    And to formally answer your question - "change your approach from expecting others to do your work for you and copying their code to doing something on your own". You can start with understanding how the SMTP protocol works. If you still want to copy code, find a more complete solution to copy from - I know there are a few of those as I have written (and published) one myself.
    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.


  6. #6
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    Ok, I managed to do it by myself. Sorry for my ignorance

  7. #7
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mail - problem with authentication

    It has got nothing to do with 'ignorance' (we went all trought it, and knowledge is never achieved anyway) but rather that people are willing to help you but not code for you .

  8. #8
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    I see... So... will you help me with setting a right text codec for the mail? I'm using polish letters - ś, ć, ź, ą, ń, ó, ę. They are not correctly shown in the message (I can see something like ¹æêñóœŸ). I don't know how to deal with it.

  9. #9
    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 mail - problem with authentication

    I think your going about it completely the wrong way. If there's a mail server around that will accept mail without authentication, spammers will quickly find it and use it to send millions of emails per day through. So you need a username and password, and hardcoding ones that work into your application would be silly as those details would be quickly abused by others.

    So you have two options: You can setup a server somewhere (non-SMTP or only accepts email to one particular address and on a non-standard port) and have your program connect to that, or you can use the users own mail client. Most individuals use the later. Organisations (such as Mozilla) typically use the former.

    If you wish to use non-ascii characters in an email (such as polish letters), then you need to learn about MIME content transfer encoding. I remember it being discussed in RFC 2045.

  10. #10
    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: Sending mail - problem with authentication

    Also let's not forget that the mail server is not interpreting the contents of the message in any way (everything between "data" and "." is just passed through), it's just the client applications that do. Bottom line is you just need to format your content properly.
    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.


  11. #11
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    Thanks for both of you.

    fatjuicymole - currently I'm connecting to smtp server (which belongs to poczta.onet.pl - popular mail service in poland) using my login and password which are specified in the code. Are you saying that "bad people" will fetch this data? What can they do then?

    If I create my own SMTP server, which would accept mails from only one mail adress on non-standard port, as you said - amm... will it cost much money?

  12. #12
    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 mail - problem with authentication

    "bad people" can use your login and password to send thousands of emails to people. This can happen until your account is banned.

    If you create your own SMTP server, it will be free of charge if you host it yourself on your own connection, else you need to shop around. It can be expensive.

  13. #13
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    If I host my own SMTP server, can't they use it knowing my login data (fetched from my program)? And how can I accept mails only from one specified e-mail adress? Is this a kind of property of an SMTP server?

  14. #14
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mail - problem with authentication

    currently I'm connecting to smtp server (which belongs to poczta.onet.pl - popular mail service in poland) using my login and password which are specified in the code. Are you saying that "bad people" will fetch this data? What can they do then?
    Harcoding your login and password make yourself recognize by the server (so validate the connection). And then, people using your app will be seen by the server as if you were the one sending a mail.

    Instead your app should asked for a valid login and password on the relevant SMTP server. So, never harcode one in your app.

  15. #15
    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 mail - problem with authentication

    If you hosted your own SMTP server, it would be impractical for other people to use it, as it would only ever accept mail to local addresses to itself (where there might only be one), so other people just wouldn't bother.

    Spammers are only interested in "open" mail servers - those which will accept mail destined for anywhere. As soon as your mail server rejects all non-local mail, people lose interest very quickly and go find some other mail server. This is the typical mail server configuration.

  16. #16
    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: Sending mail - problem with authentication

    I think bringing up your own mail server is an overkill here. A solution with the end-user providing authentication data to the application is much better. A not so bad alternative is to embed some of the capabilities of an smtp server directly into your application. Actually what is required besides what the client side of smtp offers is an ability to query dns for an MX record for a particular destination address. When you connect to the appropriate server, it won't require you to authenticate yourself (i.e. you don't need to authorize to poczta.onet.pl if you are sending mails destined for poczta.onet.pl domain). Unfortunately QHostInfo doesn't provide means for querying for MX records so this needs to be done manually.
    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.


  17. #17
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sending mail - problem with authentication

    toutarrive, wysota - I'm not sure if I got you well but let me explain what do I need it for. I've got pretty small application, which I want to have one additional feature - it should send mails to my mail account, containing information about bugs and opinions from users. They click "send mail" in menu, they write a message content, they specify their e-mail (so I can answer them) and they click "Send". I only want this mail to be delivered to my e-mail account - nothing else. So asking user for login and password is not a good solution here. Now, with such a knowledge, could you say what would be the best thing to do here?

  18. #18
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mail - problem with authentication

    See answers #9 and #16.

  19. #19
    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 mail - problem with authentication

    Quote Originally Posted by kremuwa View Post
    toutarrive, wysota - I'm not sure if I got you well but let me explain what do I need it for. I've got pretty small application, which I want to have one additional feature - it should send mails to my mail account, containing information about bugs and opinions from users. They click "send mail" in menu, they write a message content, they specify their e-mail (so I can answer them) and they click "Send". I only want this mail to be delivered to my e-mail account - nothing else. So asking user for login and password is not a good solution here. Now, with such a knowledge, could you say what would be the best thing to do here?
    The best thing to do would be to fire up the users email client, let them write the message content there, and then click send. No need to request there email address, as the email client will have that already. It'll also go through the appropriate mail server so you don't need to worry about that.

    Why make it more complicated than it needs to be?

  20. #20
    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: Sending mail - problem with authentication

    Quote Originally Posted by kremuwa View Post
    toutarrive, wysota - I'm not sure if I got you well but let me explain what do I need it for. I've got pretty small application, which I want to have one additional feature - it should send mails to my mail account, containing information about bugs and opinions from users. They click "send mail" in menu, they write a message content, they specify their e-mail (so I can answer them) and they click "Send". I only want this mail to be delivered to my e-mail account - nothing else. So asking user for login and password is not a good solution here. Now, with such a knowledge, could you say what would be the best thing to do here?
    If you are going to send mail to only one specific address (yours) then you can hardcode your application to connect directly to the server handling mail for your account and transmit the message directly to it withouth authenticating. Just be aware that should the mx record for the domain be changed, your application will not be able to transmit emails anymore.

    A much cleaner approach would be to have a php script somewhere in the web that would receive feedback from your application instead of using smtp protocol and emails for this. Getting free hosting with php capabilities shouldn't pose any problem.
    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.


Similar Threads

  1. Sending Mail through QProcess
    By KaptainKarl in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2008, 21:51
  2. Sending Mail
    By systemz89 in forum Qt Programming
    Replies: 11
    Last Post: 15th June 2008, 16:16
  3. Sending Mail with attachment files
    By joy in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2008, 08:51
  4. help with e-mail sending code
    By locus in forum Qt Programming
    Replies: 4
    Last Post: 4th July 2007, 18:07
  5. Sending mail using Qt
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2006, 11:17

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.