Android Question Net Library - Using SMTP to send an email message.

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm experimenting with the Net library in an attempt to send an email message using SMTP with gmail as the email server. The message is not being sent. My logs show a "False" coming from the "Success" event. No errors are reported from the Try / Catch statement in the coding.

B4X:
email.Initialize("smtp.gmail.com", 587, "MyEmail@gmail.com", "MyPassword", "EmailClient")
   
Dim Body As String
Dim Subj As String
Dim EmailTo As String
       
Subj="Test Email."
Body= "Testing 123."
EmailTo="SomeEmailAddress@gmail.com"

Try
    email.StartTLSMode = True
    email.UseSSL = True
    email.To.Add(EmailTo)
    email.HtmlBody=True
    email.Subject = Subj
    email.Body = Body
    email.Sender = "Emad"
    email.Send
   
    Catch
        Log("SendEmail " & LastException)
    End Try

Sub EmailClient_MessageSent(Success As Boolean)
    Log(Success)
End Sub

I went to the gmail help page and made sure my gmail account was set up correctly which it was. I checked the settings they suggested and I think I have the coding match most of them. Here are the settings that they said to use.

Outgoing Mail (SMTP) Serversmtp.gmail.com
Requires SSL: Yes
Requires TLS: Yes (if available)
Requires Authentication: Yes
Port for SSL: 465
Port for TLS/STARTTLS: 58

It seems I'm missing the authentication settings in my code so I attempted to set the authentication method but I didn't see any sample code showing what the syntax should be when trying to set that setting.
Screenshot 2020-10-28 at 11.55.00.png
I also don't know if gmail requires AUTH_CRAM_MD5, AUTH_LOGIN or AUTH_PLAIN.

All help will be appreciated since I don't know what's missing.

Thanks.
 

drgottjr

Expert
Licensed User
Longtime User
i have 2 comments:
1) when you say your gmail account is set up correctly, do you mean that you have "less secure app access" turned on? without it, google won't act as your smtp sender. by the way, i have used the service, but google turns it off if you don't use it regularly. such was the case when i went to my account to see about answering your question.
2) i forget why, but when i wrote my email app a number of years ago, in addition to the net library, i also see that i used the smtpextras library. you may want to check that out as well. perhaps it simply offered some feature i needed at the time.

by the way, i hope it's not your plan to distribute an app that uses your gmail credentials.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
i have 2 comments:
1) when you say your gmail account is set up correctly, do you mean that you have "less secure app access" turned on? without it, google won't act as your smtp sender. by the way, i have used the service, but google turns it off if you don't use it regularly. such was the case when i went to my account to see about answering your question.
2) i forget why, but when i wrote my email app a number of years ago, in addition to the net library, i also see that i used the smtpextras library. you may want to check that out as well. perhaps it simply offered some feature i needed at the time.

by the way, i hope it's not your plan to distribute an app that uses your gmail credentials.
Thanks for the name of that library. I will try it. Where can I find the "less secure app access" setting? I want to make make sure it's turned on.

I won't be using my email credentials but the user will need to do so. I'm also thinking it may not be a good idea to ask the user to do all of that stuff anyway. Right now the app just redirects them to their chosen email client. All they have to do is tap Send from their email client. Originally I was just looking for a way for the user to not even need to go to their email client.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
i have 2 comments:
1) when you say your gmail account is set up correctly, do you mean that you have "less secure app access" turned on? without it, google won't act as your smtp sender. by the way, i have used the service, but google turns it off if you don't use it regularly. such was the case when i went to my account to see about answering your question.
2) i forget why, but when i wrote my email app a number of years ago, in addition to the net library, i also see that i used the smtpextras library. you may want to check that out as well. perhaps it simply offered some feature i needed at the time.

by the way, i hope it's not your plan to distribute an app that uses your gmail credentials.
I just tried smtpextras which also did not work so we are going for the more secure way of calling the email client using this coding.

B4X:
    Dim msg As Email
 
    msg.Subject = "Song Requests"
    msg.Body = EditTextRequestedSongs.Text
    msg.To.Add("TheRadioStation@gmail.com")
 
    StartActivity(msg.GetIntent)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Thanks for the name of that library. I will try it. Where can I find the "less secure app access" setting? I want to make make sure it's turned on.
just go to google and search for "gmail less secure app access". you may have to follow a couple links. they buried it somewhere.
 
Upvote 0
Top