It is currently Sat Dec 14, 2019 10:57 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: How To: Send mail through Gmail's SMTP
PostPosted: Sat Mar 23, 2019 2:22 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
Hi all! Gmail's security is a bit tricky to set up, and they keep changing their procedure, so here's what works for me as of March 2019. I'll update this from time to time if/when needed. :)

First, you must understand that you can't use your normal Gmail password with SMTP. You have to create an app-specific password instead, through your Gmail Account settings, like so:

Image

You'll then get a custom password for use with your device (or in our case, our PureBasic app):

Image

If you need a more detailed tutorial on creating app passwords, then click here:
https://www.lifewire.com/get-a-password-to-access-gmail-by-pop-imap-2-1171882

Then, all you need to do is configure PureBasic's mail settings as below to send mail through Gmail's SMTP, without needing to change or lower Gmail's default security settings, etc.

Regarding the port number: only 465 works for me, but only 587 works for others; so try both if one fails.

Hope this tip helps someone. 8)

Code:
recipient$="recipient@somewhere.com"

sender_login$="sender@gmail.com"
sender_password$="gmail-app-password"
sender_port=465 ; Try 587 if 465 fails.

InitNetwork()

If CreateMail(0, sender_login$, "Hello !")
 
  SetMailBody(0, "Hello !" + #CRLF$ +
                 "This is a multi-" + #CRLF$ +
                 "line mail !")
 
  AddMailRecipient(0, recipient$, #PB_Mail_To)
 
  Result = SendMail(0, "smtp.gmail.com", sender_port, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, sender_login$, sender_password$)
 
  Repeat
    Progress = MailProgress(0)
    Delay(1000)
  Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
 
  If Progress = #PB_Mail_Finished
    MessageRequester("Information", "Mail correctly sent !")
  Else
    MessageRequester("Error", "Can't sent the mail !")
  EndIf
 
EndIf


Last edited by Dude on Tue Mar 26, 2019 11:47 am, edited 3 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Sun Mar 24, 2019 9:59 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jan 12, 2008 3:25 pm
Posts: 344
Location: Greece
Hello.
I did set up an app password inside Gmail and executed the code but it is not working (#PB_Mail_Error).
Is there another necessary step to set before code's execution?
Regards

_________________
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Sun Mar 24, 2019 10:26 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2214
Location: Singapore
thanos wrote:
I did set up an app password inside Gmail and executed the code but it is not working...

The App Password sign-in option is to circumvent two-factor authorisation, if enabled. Not sure if that effectively lowers the security protocols required by Gmail.

> Google Support: Sign in using App Passwords

_________________
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Sun Mar 24, 2019 10:55 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
TI-994A wrote:
The App Password sign-in option is to circumvent two-factor authorisation, if enabled.

Yes, because our PureBasic apps can't be sent a two-factor authorisation code to verify with. It's a Google-supplied workaround, rather than circumvention. ;)

thanos wrote:
I did set up an app password inside Gmail and executed the code but it is not working (#PB_Mail_Error)

Is everything literally set up like in my code? The port number must be 465 and not 587, etc. And you're sending from an actual Gmail account and not trying to use Gmail's SMTP to send mail from a non-Gmail account? Also, make sure your PC isn't running a firewall that might stop it.

thanos wrote:
Is there another necessary step to set before code's execution?

Nope, the code in my example is a standalone snippet that works for me. For example, I can use PureBasic to send mail through my Gmail to a Hotmail account with no problems at all. The sent mail even shows up in Gmail's "Sent" folder.

I edited my first post with a link to more detailed instructions on creating an app password.


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Mon Mar 25, 2019 8:19 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jan 12, 2008 3:25 pm
Posts: 344
Location: Greece
@dude
Thank you for your response.

Dude wrote:
Is everything literally set up like in my code? The port number must be 465 and not 587, etc. And you're sending from an actual Gmail account and not trying to use Gmail's SMTP to send mail from a non-Gmail account? Also, make sure your PC isn't running a firewall that might stop it.

Code:
EnableExplicit
InitNetwork()
Define From$   = "solvXXXXXX@gmail.com"
Define ToMail$ = "consXXXXXX@gmail.com"
Define AppPwd$ = "uhkdsstblcddjtsx"
Define Result, Progress
If CreateMail(0, From$, "Hello !")
 
  SetMailBody(0, "Hello !" + #CRLF$ +
                 "This is a multi-" + #CRLF$ +
                 "line mail!")
 
  AddMailRecipient(0, ToMail$, #PB_Mail_To)
 
  Result = SendMail(0, "smtp.gmail.com", 465, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, From$, AppPwd$)
  Repeat
    Progress = MailProgress(0)
    Delay(1000)
  Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
   
  Debug Progress
  If Progress = #PB_Mail_Finished
    MessageRequester("Information", "Mail correctly sent !")
  Else
    MessageRequester("Error", "Can't sent the mail !")
  EndIf
 
EndIf


dude wrote:
Nope, the code in my example is a standalone snippet that works for me. For example, I can use PureBasic to send mail through my Gmail to a Hotmail account with no problems at all. The sent mail even shows up in Gmail's "Sent" folder.

I edited my first post with a link to more detailed instructions on creating an app password.

The firewall is inactive and follow exactly the instructions of your post and of the guide.
Same error.
Regards

_________________
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Mon Mar 25, 2019 9:19 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
Hmm, that's weird. Maybe there's a bug with MailProgress()? Try changing this line:

Code:
Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error

to this:

Code:
Until Progress = #PB_Mail_Finished

Just to see if PureBasic is getting an error when it shouldn't be.


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Mon Mar 25, 2019 5:43 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jan 12, 2008 3:25 pm
Posts: 344
Location: Greece
Dude wrote:
Hmm, that's weird. Maybe there's a bug with MailProgress()? Try changing this line:

Code:
Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error

to this:

Code:
Until Progress = #PB_Mail_Finished

Just to see if PureBasic is getting an error when it shouldn't be.

Same results.
The program falls into an endless loop.
If you check the value of Progress
Code:
Debug Progress
it returns 0 and after about 10 iterations, returns -2
Code:
...
  Repeat
    Progress = MailProgress(0)
    Delay(1000)
    Debug Progress
  Until Progress = #PB_Mail_Finished ;Or Progress = #PB_Mail_Erro

Regards

_________________
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Mon Mar 25, 2019 10:22 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
Try port 587 then. Although, that number doesn't work for me, and it's not what Google says to use. Other than that, I don't know why it's not working for you but does for me. Sorry.

Also maybe try this (but I didn't have to do it):
https://support.google.com/accounts/answer/6010255


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Tue Mar 26, 2019 8:14 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Jan 12, 2008 3:25 pm
Posts: 344
Location: Greece
Dude wrote:
Try port 587 then. Although, that number doesn't work for me, and it's not what Google says to use. Other than that, I don't know why it's not working for you but does for me. Sorry.

Also maybe try this (but I didn't have to do it):
https://support.google.com/accounts/answer/6010255

I tried 587 and worked correctly.
I saw that Google does not suggest this port, but it is working.
Thanks a lot!

_________________
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!


Top
 Profile  
Reply with quote  
 Post subject: Re: How To: Send mail through Gmail's SMTP
PostPosted: Tue Mar 26, 2019 11:48 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1904
thanos wrote:
I tried 587 and worked correctly.
I saw that Google does not suggest this port, but it is working.
Thanks a lot!

Glad you got it working! :D Don't know why we both need different port numbers, though. First post has been edited with this information.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye