5.40LTS Cannot send mail with TLS/Asynchronous

Just starting out? Need help? Post your questions and find answers here.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by RichAlgeni »

If anyone needs another example, just replace the data in variables userName, mailFrom and password. It works beautifully:

Code: Select all

; process to test new email capabilities in PB 5.40

EnableExplicit

; open the console window, initialize the network, send the message, then quit

InitNetwork()

OpenConsole()

Define result.i
Define mailNumber.i
Define mailHost.s     = "smtp.gmail.com"
Define userName.s     = "your_email_address@gmail.com"
Define mailFrom.s     = "your_email_address@gmail.com"
Define password.s     = "your_password_here"
Define mailPort.i     = 587
Define mailTo.s       = "your_destination@destination.com"
Define subject.s      = "64 bit email Test using TLS Encryption"
Define emailBody.s    = "Dear Sir/Madam, this email is brought to you courtesy of 64 bit PureBasic Version 5.40 TLS Encryption."

PrintN("SendEmail() > About to create mail!")

mailNumber = CreateMail(#PB_Any, userName, subject)
If mailNumber > 0
    AddMailRecipient(mailNumber, mailTo, #PB_Mail_To)
    SetMailBody(mailNumber, emailBody)
    result = SendMail(mailNumber, mailHost, mailPort, #PB_Mail_UseSSL, userName, password)
    FreeMail(mailNumber)

    PrintN("SendEmail() > completed, return value: " + Str(result))
Else
    PrintN("SendEmail() > error creating mail process")
EndIf

Input()

CloseConsole()
Port 587 must tell PureBasic to use TLS Encryption. For less secure SSL, use port 465.

See for more information: http://stackoverflow.com/questions/1579 ... 65-and-587
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by netmaestro »

Google prevents my sign-in attempt every time. I have to find the setting in my profile to loosen that up.
BERESHEIT
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by skywalk »

Same here. No go with either port. :?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by netmaestro »

I finally got irritated enough to go find the setting in gmail. It is under Settings->Accounts and Import->Other Google account settings->Connected apps and sites->Allow less secure apps:ON

Once I found the setting and turned it on, the RichAlgeni code works without problem. But I'm going to guess that because this mail is machine-produced it would go to people's spam folder first unless the recipient had the sender whitelisted, correct? Anyone know for sure?
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by Fangbeast »

I never knew that setting existed so never touched it. So why does my email from PB work when others have failed??

Why is my setup okay when others just fail? Should I do a screen dump of my gmail settings so people can compare in case they are accessing a different setup somehow?

After Fred did that SSL fix, it has just worked ever since.

(Scratching head)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by IdeasVacuum »

Should I do a screen dump of my gmail settings
I think that would be a great help 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by skywalk »

Thanks netmaestro :)
Above code works with Port 587(465 fails) if I change the gmail setting:
; https://myaccount.google.com/security?hl=en#activity
; Access for less secure apps has been turned = ON.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by RichAlgeni »

Fangbeast wrote:I never knew that setting existed so never touched it. So why does my email from PB work when others have failed??

Why is my setup okay when others just fail? Should I do a screen dump of my gmail settings so people can compare in case they are accessing a different setup somehow?

After Fred did that SSL fix, it has just worked ever since.

(Scratching head)
I've had my Google email account for years, the SSL and TLS code has worked fine for me since 2013.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [Done] 5.40LTS Cannot send mail with TLS/Asynchronous

Post by Fangbeast »

RichAlgeni wrote:
Fangbeast wrote:I never knew that setting existed so never touched it. So why does my email from PB work when others have failed??

Why is my setup okay when others just fail? Should I do a screen dump of my gmail settings so people can compare in case they are accessing a different setup somehow?

After Fred did that SSL fix, it has just worked ever since.

(Scratching head)
I've had my Google email account for years, the SSL and TLS code has worked fine for me since 2013.
Mine didn't work with the initial PB 5.40 release, I reported it, Fred fixed something and then it did. I've had a Google email account since they released it and never changed any settings either.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: 5.40LTS Cannot send mail with TLS/Asynchronous

Post by RichardL »

Hi,
I used the code by RichAlgeni (with my name and account details etc) and it worked first go. Good!
But... when I added a 'cc' and a 'jpg' or 'PDF' attachment the email body text disappears, just the attachment shows in ThunderBird.
Any idea what I'm likely to be doing wrong?
I'm using PB5.41LTS

RichardL

Code: Select all

EnableExplicit

InitNetwork()
OpenConsole()

Define CRLF.s = Chr(13)+Chr(10)
Define result.i
Define mailNumber.i
Define mailHost.s     = "smtp.gmail.com"

Define userName.s    = "xxxxxxxxxxxx"
Define mailFrom.s     = "xxxxxxxxxxxx"
Define Password.s     = "xxxxxxxxxxxx"
Define mailTo.s         = "xxxxxxxxxxxx"
Define ccmailTo.s      = "xxxxxxxxxxxx"
Define subject.s      = "Ermintrude will be late."
Define emailBody.s    
Define mailPort.i     = 587
emailBody = "This email was sent automatically by a prototype email sender. 01"+CRLF
emailBody + "This email was sent automatically by a prototype email sender. 02"+CRLF
emailBody + "This email was sent automatically by a prototype email sender. 03"+CRLF
emailBody + "This email was sent automatically by a prototype email sender. 04"+CRLF
emailBody + "This email was sent automatically by a prototype email sender. 05"+CRLF
emailBody + "This email was sent automatically..... and made longer..."

PrintN("SendEmail() > About to create mail!")

mailNumber = CreateMail(#PB_Any, userName, subject)
If mailNumber > 0
  AddMailAttachment(mailNumber, "Knockers.pdf","C:\temp\Intact_020415163929547.pdf")
  SetMailBody(mailNumber, emailBody)
  AddMailRecipient(mailNumber, mailTo , #PB_Mail_To)
  AddMailRecipient(mailNumber,ccmailTo, #PB_Mail_Cc)
  result = SendMail(mailNumber, mailHost, mailPort, #PB_Mail_UseSSL, userName, Password)
  FreeMail(mailNumber)
   PrintN("SendEmail() > completed, return value: " + Str(result))
Else
  PrintN("SendEmail() > error creating mail process")
EndIf

Input()

CloseConsole()
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: 5.40LTS Cannot send mail with TLS/Asynchronous

Post by bbanelli »

@RichardL

Confirmed in 5.42 x86 (Windows 7). No body is shown if attachment is enabled. Perhaps you should move this to bugs section?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: 5.40LTS Cannot send mail with TLS/Asynchronous

Post by RichAlgeni »

Confirmed with 5.42 x64.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: 5.40LTS Cannot send mail with TLS/Asynchronous

Post by RichAlgeni »

Post Reply