Page 1 of 1

How To: Send mail through Gmail's SMTP

Posted: Sat Mar 23, 2019 2:22 pm
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

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

Posted: Sun Mar 24, 2019 9:59 am
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

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

Posted: Sun Mar 24, 2019 10:26 am
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

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

Posted: Sun Mar 24, 2019 10:55 am
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.

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

Posted: Mon Mar 25, 2019 8:19 am
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

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

Posted: Mon Mar 25, 2019 9:19 am
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.

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

Posted: Mon Mar 25, 2019 5:43 pm
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

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

Posted: Mon Mar 25, 2019 10:22 pm
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

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

Posted: Tue Mar 26, 2019 8:14 am
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!

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

Posted: Tue Mar 26, 2019 11:48 am
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.

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

Posted: Wed Mar 04, 2020 9:17 pm
by firace
Thanks for this example! Also had to use port 587 by the way, was failing with 465.