Page 1 of 2
5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Tue Sep 01, 2015 2:55 am
by Fangbeast
Using the code below straight from the PB 5.4 manual with my server, username and password, I get "Error", "Can't sent the mail !"
Not my code for once:):) Anyone have an idea if the manual is wrong? Or is using the new flags "#PB_Mail_Asynchronous | #PB_Mail_UseSSL" clashing somehow or is it just a bug?
Code: Select all
InitNetwork()
If CreateMail(0, "bangfeast@gmail.com", "Hello !")
SetMailBody(0, "Hello !" + #CRLF$ +
"This is a multi-" + #CRLF$ +
"line mail !")
AddMailAttachment(0, "Geebee !" , "C:\iViewRipper_2015_08_30_23-16-51.dat")
; Change the recipients to real one
AddMailRecipient(0, "mookiefangy@gmail.com", #PB_Mail_To)
; Set the SMTP server to use
Result = SendMail(0, "smtp.gmail.com", 465, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, "bangfeast", "throbbinunderpants")
Repeat
Progress = MailProgress(0)
Delay(300)
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: {5.40LTS] Cannot send mail with TLS/Asynchronous
Posted: Tue Sep 01, 2015 8:06 am
by Fred
Fixed for beta 2. For gmail you will need to use the port 587 (TLS).
Re: {5.40LTS] Cannot send mail with TLS/Asynchronous
Posted: Tue Sep 01, 2015 9:00 am
by Fangbeast
Fred wrote:Fixed for beta 2. For gmail you will need to use the port 587 (TLS).
That's interesting, didn't know that. Been using 465 with gmail for years because that's what their setup pages told me (when I first setup my accounts) and never changed it since.
I found this below but am none the wiser. Seems like 465 is automatic and 587 is manually started. Oh well, both work well with any client I have tried but will set PB to use 587:):)
Port 465 is for smtps - SSL encryption is started automatically before any SMTP level communication.
Port 587 is for msa - it is almost like standard SMTP port. SSL encryption may be started by STARTTLS command at SMTP level if server supports it and your ISP does not filter server's EHLO reply (reported 2014 Nov).
MSA should accept email after authentication (e.g. after SMTP AUTH). It helps to stop outgoing spam when netmasters of DUL ranges can block outgoing connections to SMTP port (port 25)
Ahh, found a better explanation:):) (My brain is limited these days)
These port assignments are specified by the Internet Assigned Numbers Authority (IANA):
Port 587: [SMTP] Message submission (SMTP-MSA), a service that accepts submission of email from email clients (MUAs). Described in RFC 6409.
Port 465: URL Rendesvous Directory for SSM [sic] (entirely unrelated to email)
Historically, port 465 was initially planned for the SMTPS encryption and authentication “wrapper” over SMTP, but it was quickly deprecated (within months, and over 15 years ago) in favor of STARTTLS over SMTP (RFC 3207). Despite that fact, there are probably many servers that support the deprecated protocol wrapper, primarily to support older clients that implemented SMTPS. Unless you need to support such older clients, SMTPS and its use on port 465 should remain nothing more than an historical footnote.
The hopelessly confusing and imprecise term, SSL, has often been used to indicate the SMTPS wrapper and TLS to indicate the STARTTLS protocol extension.
For completeness:
Port 25: Simple Mail Transfer (SMTP-MTA), a service that accepts submission of email from other servers (MTAs or MSAs). Described in RFC 5321.
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Tue Sep 01, 2015 12:52 pm
by Fangbeast
Fred, when I use port 587 with Thunderbird, my email times out on me constantly. When I use 465, it works immediately.
Did you have any timing problems in PB mail with port 587 and SSL?
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Thu Sep 03, 2015 11:02 pm
by Fangbeast
YAY! Thanks Fred. The example below from the manual (cleaned up a little) now works. I can now revisit some old projects that were interesting to me.
One question..Is there any way to get the error message returned by this flag? If #PB_Mail_Error occurred, would be nice to be able to read the error to see if there is anything the coder missed perhaps and be able to fix it.
Code: Select all
If InitNetwork() <> 0
If CreateMail(0, "flappyhead@gmail.com", "Hello smegbrain!")
SetMailBody(0, "Hello !" + #CRLF$ + "This is a multi-" + #CRLF$ + "line email !")
AddMailAttachment(0, "Knickers !" , "C:\Users\bangf\Downloads\Downloaded stuff\LGS_8.72.98_x64_Logitech.exe.log")
; Change the recipients to a real one
AddMailRecipient(0, "rastus@gmail.com", #PB_Mail_To)
; Set the SMTP server to use
Result = SendMail(0, "smtp.gmail.com", 587, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, "smegface", "stoopidpassword")
Repeat
Progress.i = MailProgress(0)
; I added this section to see if anything was happening
If ProgressId.i = #PB_Mail_Connected
Debug "Connected to the email server."
ElseIf ProgressId.i = #PB_Mail_Error
ErrorCounter.i + 1
Debug "Error connecting and sending message."
Else
Debug "Sent " + Str(ProgressId.i) + " bytes of this email."
EndIf
; End of my added section
Delay(300)
Until Progress.i = #PB_Mail_Finished Or Progress.i = #PB_Mail_Error Or ErrorCounter.i = 40
If Progress = #PB_Mail_Finished
Debug "--------------------------------------------------------------------------------"
Debug "Information: - Mail correctly sent !"
Else
Debug "--------------------------------------------------------------------------------"
Debug "Error: - Can't send the email !"
EndIf
EndIf
Else
Debug "--------------------------------------------------------------------------------"
Debug "Error: - Cannot initialise the network environment !"
EndIf
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Tue Sep 15, 2015 11:17 pm
by aaaaaaaargh
Hi,
has anybody tried this with 5.40LTS Beta 3? I can't get it to work, it worked with Beta2
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Wed Sep 16, 2015 9:03 am
by Fangbeast
aaaaaaaargh wrote:Hi,
has anybody tried this with 5.40LTS Beta 3? I can't get it to work, it worked with Beta2
Just tested it here, works fine. Checked ThunderBird and it had received the two test messages that I had sent.
PB 5.40 LTS Beta 3(x86), windows 10 X64
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Thu Sep 17, 2015 11:59 pm
by netmaestro
When I try with gmail it doesn't work, just prints "Can't send the mail!" and a few seconds after trying I get this email from google:

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 1:12 am
by Fangbeast
Just tested this one again here, still works fine.
Code: Select all
Procedure EmailMessage()
;
If InitNetwork() <> 0
;
MailToUser.s = "eatworms@gmail.com"
MailToSubject.s = "PureBasic 5.40, Beta 3, TLS Authentication test"
MailToBody.s = "Yo, Bozo, wot yu sending utter crap to yourself for ya twit???"
;
HumanName.s = "Underpants"
ServerName.s = "smtp.gmail.com"
PortNumber.i = 587 ; Old port was 465
UserName.s = "Underpants"
Password.s = "wotsmepassword"
EmailAddress.s = "Underpants@gmail.com"
;
If HumanName.s And ServerName.s And PortNumber.i And UserName.s And Password.s And EmailAddress.s
;
If MailToUser.s And MailToSubject.s And MailToBody.s
;
OutgoingMailId.i = CreateMail(#PB_Any, EmailAddress.s, MailToSubject.s)
;
If OutgoingMailId.i
;
SetMailBody(OutgoingMailId.i, MailToBody.s)
;
AddMailRecipient(OutgoingMailId.i, MailToUser.s, #PB_Mail_To)
;
If SendMail(OutgoingMailId.i, ServerName.s, PortNumber.i, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, UserName.s, Password.s)
;
Repeat
ProgressId.i = MailProgress(OutgoingMailId.i)
If ProgressId.i = #PB_Mail_Connected
Debug "Connected to the email server."
ElseIf ProgressId.i = #PB_Mail_Error
ErrorCounter.i + 1
Debug "Error connecting and sending message."
Else
Debug "Sent " + Str(ProgressId.i) + " bytes of this email."
EndIf
Delay(300)
Until ProgressId.i = #PB_Mail_Finished Or Progress.i = #PB_Mail_Error Or ErrorCounter.i = 20
;
If ProgressId.i = #PB_Mail_Finished
Debug "Email message packet correctly sent."
ElseIf ProgressId.i = #PB_Mail_Error
Debug "Email message packet could not be sent."
EndIf
;
Else
Debug "The mail packet could not be sent."
EndIf
;
Else
Debug "Could not create the outgoing email packet."
EndIf
;
Else
Debug "One or more of the recipient's details are empty."
EndIf
;
Else
Debug "One or more of the email server details are empty."
EndIf
;
Else
Debug "Could not initialise the network environment to send mail"
EndIf
;
EndProcedure
EmailMessage()
And lifting this one straight out of the manual for attachments and forgot that google blocks any mention of 7'zip, zip, log. bat' etc so renamed attachment to .moo and it worked as well.
Code: Select all
If InitNetwork() <> 0
If CreateMail(0, "underpants@gmail.com", "Hello !")
SetMailBody(0, "Hello !" + #CRLF$ + "This is a multi-" + #CRLF$ + "line mail !")
AddMailAttachment(0, "Underpants !" , "C:\Users\bangf\Downloads\Downloaded stuff\diffuse.moo")
; Change the recipients to real one
AddMailRecipient(0, "eatworms@gmail.com", #PB_Mail_To)
; Set the SMTP server to use
; Result.i = SendMail(0, "smtp.gmail.com", 587, #PB_Mail_Asynchronous, "underpants", "mepassword")
Result = SendMail(0, "smtp.gmail.com", 587, #PB_Mail_Asynchronous | #PB_Mail_UseSSL, "underpants", "mepassword")
Repeat
Progress.i = MailProgress(0)
; I added this section to see if anything was happening
If ProgressId.i = #PB_Mail_Connected
Debug "Connected to the email server."
ElseIf ProgressId.i = #PB_Mail_Error
ErrorCounter.i + 1
Debug "Error connecting and sending message."
Else
Debug "Sent " + Str(ProgressId.i) + " bytes of this email."
EndIf
; End of my added section
Delay(300)
Until Progress.i = #PB_Mail_Finished Or Progress.i = #PB_Mail_Error Or ErrorCounter.i = 40
If Progress = #PB_Mail_Finished
Debug "--------------------------------------------------------------------------------"
Debug "Information: - Mail correctly sent !"
Else
Debug "--------------------------------------------------------------------------------"
Debug "Error: - Can't send the email !"
EndIf
EndIf
Else
Debug "--------------------------------------------------------------------------------"
Debug "Error: - Cannot initialise the network environment !"
EndIf
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 12:26 pm
by Fred
netmaestro wrote:When I try with gmail it doesn't work, just prints "Can't send the mail!" and a few seconds after trying I get this email from google:

There is a parameter in your google account to allow less securised apps to connect, and it's enabled for me so it's probably why it works for me as well. I don't know the process to use to get secure connect with gmail.
https://support.google.com/accounts/ans ... 0255?hl=en
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 12:41 pm
by IdeasVacuum
The minimum requirement for Google security is to post them a severed limb........
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 1:00 pm
by Fred
After some investigation, to have it working without tuning the account security down, we need to handle the OAuth 2.0 protocol, which request access to the user and then get a token. It's not really doable from a PB app (may be if you use WebGadget() ?) so I don't think we will support it.
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 5:08 pm
by GPI
Fred wrote:After some investigation, to have it working without tuning the account security down, we need to handle the OAuth 2.0 protocol, which request access to the user and then get a token. It's not really doable from a PB app (may be if you use WebGadget() ?) so I don't think we will support it.
Maybe it is a good idea to create a general OAuth-Library, because many websites use this protocol.
Maybe something like "token.s=OAuthRequester(website.s)"
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Fri Sep 18, 2015 5:25 pm
by Fred
How would this work ? With the built-in WebBrowser ?
Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous
Posted: Sat Oct 24, 2015 1:10 pm
by Dude
Just a heads-up: I was trying to send a secure mail with port 587 and the #PB_Mail_UseSSL flag, but it kept failing with an error. Turns out I just had to remove the #PB_Mail_UseSSL flag and it worked with port 587 without it. Hope this helps someone!
