PDA

View Full Version : connect to smtp gmail server



IwantToLearen
13th February 2013, 15:48
Hi,
I am trying to send an email using gmail or yahoo ..etc.
I was looking into this (URL="http://www.qtcentre.org/threads/2221-Sending-email-using-Qt) thread
The issue i am facing is, when i try to connect to smtp.gmail.com, the connection fails. and the HostNotFoundError is thrown.

The smtp server name for gmail i got it from this (http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm) website, here it says that

server address :smtp.gmail.com
port : 465
TLS/SSL required: yes, which i did not understand, what exactly i have to do for this case.

The code i am using is the code written by patrik08 (http://www.qtcentre.org/members/1605-patrik08) in this thread (http://www.qtcentre.org/threads/2221-Sending-email-using-Qt) post #9, I also made the edit that was mentioned in post #11.

so right now,I'm not able to get the connection to be successful.


connect(smtpSocket,SIGNAL(connected()),this,SLOT(s mtpConnected()))
connect(smtpSocket,SIGNAL(error(QAbstractSocket::S ocketError)),this,SLOT(smtpError(QAbstractSocket:: SocketError)))
....
....
smtpSocket->connectToHost("smtp.gmail.com",465) // I also tried port 25 as in the original code but it failes to.

Please if any one can help me with this I would really appreciate it.

BTW: the reason for making new thread, because i asked there and no one reply, as the thread really old and has too many replies so it seems no one is interested to reply , as well it maybe i am asking a noob questions, so I though maybe someone here will be welling to help me.


I made a compiled version with GUI interface to use the SMTP class{8712}, so if anyone would like to test and try it.

Thanks

ChrisW67
13th February 2013, 21:53
The error message is descriptive of the problem: your machine cannot resolve the DNS name "smtp.gmail.com" into an IP address. This may be a transient error in your DNS service or it may be because you are behind a proxy. Either way, Qt is simply reporting what the operating system is telling it.

IwantToLearen
14th February 2013, 14:39
Thanks for explaining to me the reason, Yeah i do I am connected using a proxy, so how can I achieve this ? which is sending an email while i am using proxy ? is it possible ?


Thanks

alrawab
14th February 2013, 17:12
use QNetworkProxy
QNetworkProxy
then smtpsocket->setProxy(your_ QNetworkProxy)

IwantToLearen
14th February 2013, 19:42
Thanks,
I used the QNetworkProxy, and now the error message changed to "SocketTimeoutError".

alrawab
14th February 2013, 22:27
Thanks,
I used the QNetworkProxy, and now the error message changed to "SocketTimeoutError".
https://github.com/bluetiger9/SmtpClient-for-Qt

IwantToLearen
14th February 2013, 22:45
Thank you so much,
Yeah i found this code and I was using it as well, i added a proxy to it.
I still get time out error.
I print out the status and the error, and i get this:

statechanged :: QAbstractSocket::HostLookupState
statechanged :: QAbstractSocket::ConnectingState
statechanged :: QAbstractSocket::UnconnectedState
socketError :: QAbstractSocket::SocketTimeoutError
statechanged :: QAbstractSocket::UnconnectedState

I will try this with other internet connection, where i do not need to use proxy and I see what will i get.

alrawab
15th February 2013, 07:33
http://support.google.com/mail/bin/answer.py?hl=en&answer=78775
465 ==> 587

IwantToLearen
15th February 2013, 15:52
Looking into the link you gave me, its says gmail uses 465 for SSL and 587 for TLS, and the code uses QSslSocket. they also say try using 25 for SSL as well.

I tried all the ports possible, still get the timeout error.

As reading on other post, (http://www.qtcentre.org/threads/39228-Problem-using-gmail-SMTP-to-send-E-mail-from-application) Squidge on reply #5 was saying


'Read Timeout' sounds to me like a successfull connect, but a failed negotiation (ie, waiting for something that didn't appear in a suitable time frame)

After Tottish showed a wireshark results for the time out erro, and Wysota on reply #7, as the time out happen because of waiting for SSL handshake and it fails.

but I though the QSslSocket is for a SSL connection and will do the SSL handshake.

ChrisW67
15th February 2013, 23:17
On Windows you need the OpenSSL DLL available to the program. If that is your platform absence of the runtime library may be your problem.

alrawab
16th February 2013, 05:56
dude don't try to reinvent the wheel as I mentioned before this class is perfect and deal with different types of sockets
https://github.com/bluetiger9/SmtpClient-for-Qt
move src Dir to your project and right click ==>add Existing Files (choose src :))
now if you use windows you have to get openSSL runtime (@ChrisW67 #10)
http://slproweb.com/download/Win32OpenSSL_Light-1_0_0k.exe
put libeay32.dll,libssl32.dll and ssleay32.dll whrer QtNetwork4.dll (or add them to your enviroment path)
for proxy :

QNetworkProxy proxy(QNetworkProxy::HttpProxy,_change_it_your_pro xy,_change_it_proxy_port,_change_it_user,_change_i t_password);
socket->setProxy(proxy);
Good Luck
8725

IwantToLearen
18th February 2013, 15:11
Thanks for your replying and help.

I am really not trying to reinvent the wheel. I am using the SmtpClient-for-Qt and i added the set proxy method as well. I am not working on windows I am working on Linux/ redhat.
as I see in the screen shot you attached, that you sent an email using it successfully, did you used proxy or not ??

Thanks.

paulbiom
21st February 2013, 16:55
I spent ages trying to communicate with GMails SMTP server over a non proxied connection. It was only when I tried SSL over port 25 that I had any joy, I couldn't get anything back using TSL at all.. If it's an option try port 25 SSL without the proxy and then when that's working try adding the proxy code.