How To: Send mail through Gmail's SMTP

Share your advanced PureBasic knowledge/code with the community.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

How To: Send mail through Gmail's SMTP

Post by Dude »

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 ... -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: Select all

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.
thanos
Enthusiast
Enthusiast
Posts: 422
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: How To: Send mail through Gmail's SMTP

Post by thanos »

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!
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: How To: Send mail through Gmail's SMTP

Post by TI-994A »

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! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How To: Send mail through Gmail's SMTP

Post by Dude »

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.
thanos
Enthusiast
Enthusiast
Posts: 422
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: How To: Send mail through Gmail's SMTP

Post by thanos »

@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: Select all

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!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How To: Send mail through Gmail's SMTP

Post by Dude »

Hmm, that's weird. Maybe there's a bug with MailProgress()? Try changing this line:

Code: Select all

Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
to this:

Code: Select all

Until Progress = #PB_Mail_Finished
Just to see if PureBasic is getting an error when it shouldn't be.
thanos
Enthusiast
Enthusiast
Posts: 422
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: How To: Send mail through Gmail's SMTP

Post by thanos »

Dude wrote:Hmm, that's weird. Maybe there's a bug with MailProgress()? Try changing this line:

Code: Select all

Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
to this:

Code: Select all

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: Select all

Debug Progress
it returns 0 and after about 10 iterations, returns -2

Code: Select all

...
  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!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How To: Send mail through Gmail's SMTP

Post by Dude »

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
thanos
Enthusiast
Enthusiast
Posts: 422
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: How To: Send mail through Gmail's SMTP

Post by thanos »

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!
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How To: Send mail through Gmail's SMTP

Post by Dude »

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.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: How To: Send mail through Gmail's SMTP

Post by firace »

Thanks for this example! Also had to use port 587 by the way, was failing with 465.
Post Reply