Android Question sending emails silently/automatically?

Dave O

Well-Known Member
Licensed User
Longtime User
Hi all,

I'm designing a "note to self" app that lets you record a voice note and email it as a voice attachment, preferably without invoking the UI for the email app.

To send using the user's GMail, there's the GMail REST API:
https://www.b4x.com/android/forum/threads/b4x-sending-emails-with-gmail-rest-api.81736/

However, I'd also like to give the user some alternatives, in case they don't use GMail, or don't use it exclusively (e.g. I use Outlook for work, GMail for personal).

It looks like Microsoft offers a Graph API for sending Outlook messages (which apparently deprecates their Outlook/Android SDK). I assume I'd need a wrapper library to use this in B4A? (I searched the forums but didn't find anything obvious.)

Any other email APIs/libraries that might be useful for this?
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
Hi Dave,

I would try this (SMTP): https://www.b4x.com/android/forum/threads/new-net-library-android-ftp-smtp-and-pop3.10892/#content

and here is some code to assist:
(You need this lib https://www.b4x.com/android/forum/threads/lib-netextras.26902/#post-188495 - Thanks @warwound )

This code is not intended as a final solution, but rather as a guideline. (I send Prescriptions to Pharmacies with it)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim SMTPExtras1 As SMTPExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        SMTPExtras1.Initialize("smtp.somedomain.com", Port, "Username", "Password", "EventName") '<-- Event Name in this case - "SMTPExtras1"
        SMTPExtras1.UseSSL = False   '   Gmail requires SSL.
    End If
    SendNewScript
End Sub

Sub SendNewScript
    ProgressDialogShow("Preparing your request")
    File.Copy(Path1, GlobFileName, Starter.shared, GlobFileName) 'Put the file in a shared space ready for attaching
    Dim SendTo As String = Starter.kvs.Get("FaveMail")
    Dim SendBCC As String = Starter.kvs.Get("eMail")
    Dim SendBody As String = "Please prepare this script and contact me once it is complete and ready for delivery or collection."&CRLF&CRLF&"I do understand that I can only receive my medication on presentation of the original script"&CRLF&CRLF&CRLF&CRLF&Starter.kvs.get("UserName")
    Dim SendSub As String = "New Script for: "&Starter.kvs.get("UserName")&" - "&Starter.kvs.get("IdNumber")
''''    SendEmail (SendTo, SendBody, SendSub, File.Combine(Starter.shared, GlobFileName ))

        SMTPExtras1.AuthMethod=SMTPExtras1.AUTH_LOGIN
        SMTPExtras1.To.Add(SendTo)
'        SMTPExtras1.BCC.Add(SendBCC)
        SMTPExtras1.Subject = SendSub
        SMTPExtras1.Body = SendBody
        SMTPExtras1.AddAttachment(Starter.shared, GlobFileName)
        SMTPExtras1.AddHeader("Who","PAS APP at "&DateTime.Now)
         SMTPExtras1.AddHeader("Sender Id:", "Value: "&Starter.kvs.get("IdNumber"))
        '   if you uncomment the next line then you'll see that the MessageSent(Success As Boolean) event is raised instead of the MessageSent2(Success As Boolean, Tag As Object) event
        SMTPExtras1.Tag=Starter.kvs.Get("FaveMail")
         SMTPExtras1.Send

    LogColor("Fav eMail: "&Starter.kvs.Get("FaveMail"),Colors.Magenta)
    LogColor("CC eMail: "&Starter.kvs.Get("eMail"),Colors.Magenta)
    
        SMTPExtras1.To.Add(SendBCC)
        SMTPExtras1.Subject = SendSub
        SMTPExtras1.Body = SendBody
        SMTPExtras1.AddAttachment(Starter.shared, GlobFileName)
        SMTPExtras1.AddHeader("Who","PAS APP at "&DateTime.Now)
        SMTPExtras1.AddHeader("Sender Id:", "Value: "&Starter.kvs.get("IdNumber"))
        '   if you uncomment the next line then you'll see that the MessageSent(Success As Boolean) event is raised instead of the MessageSent2(Success As Boolean, Tag As Object) event
        SMTPExtras1.Tag=Starter.kvs.Get("eMail")
        SMTPExtras1.Send
    
    scriptsend = False

End Sub

Sub SMTPExtras1_MessageSent(Success As Boolean)
    '   the event MessageSent(Success As Boolean) is identical to the same event in the default SMTP object
    Log("SMTPExtras1_MessageSent "&Success)
    If Success Then
        ProgressDialogHide
        ToastMessageShow("Your Prescription was Sent Successfully", True)
    Else
        ProgressDialogHide
        ToastMessageShow("Error Sending Prescription", True)
        Log(LastException.Message)
    End If
'    Button1.Enabled=True
End Sub

Sub SMTPExtras1_MessageSent2(Success As Boolean, Tag As Object)
    '   MessageSent2(Success As Boolean, Tag As Object) is a new event introduced in SMTPExtras
 
    '   if you set the SMTPExtras Tag property to a value then SMTPEXtras will raise the event MessageSent2(Success As Boolean, Tag As Object)
    '   if you do not set the SMTPExtras Tag property, or you set it to Null, then SMTPExtras will instead raise the event MessageSent(Success As Boolean)
 
    Log("SMTPExtras1_MessageSent2 "&Success&", "&Tag)
    If Success Then
        ProgressDialogHide
        ToastMessageShow("Message sent successfully to "&Tag, True)
    Else
        ProgressDialogHide
        ToastMessageShow("Error sending message to "&Tag, True)
        Log(LastException.Message)
    End If
End Sub
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Hi Bill,

Thanks for posting this. I assume that this would mean the app would be sending emails using an email account I controlled (e.g. supplying the username and password). (I think it would be bad form to ask the user to supply their login details.)

I'd prefer them to use their own email account from a privacy standpoint (not having their content go through an email account they don't control), and not having to maintain that account myself. But a good option to keep in reserve. :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
some alternatives
i would use also the "share to" functionality at android.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Yes, I've used the "share to" method (using intents), but as far as I know, that still involves showing the email UI and the user still having to click Send.

In my app, the idea is to be able to launch the app, start talking, then press Send (or just turn off the phone) and it sends without any further user action. That's the dream, anyway. :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
yo but i meant also share to google drive or one drive or something else as file.
why do you prefer as mail? u used it as cloud storage because the mails are synchronized to other devices?
everybody non developer i know private did not use the a mail system or at least very rare.
i would not input my mail username and password in any app and without u can not send mails from this account. (except intent based)
i think a own web api/service would be better so send this data to a mail address (or list) by "your" mail account.
or direct from users phone with "your" mail service account credentials. (i read you not want it)
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
The first version of the app will send the voice recording to the user's email inbox, so they can act on it later when they have time.

Eventually the app may offer the ability to convert their speech to text and turn that into a task in Google Tasks or whatever.

I used an app like this for years, but it recently stopped working (no longer on the Play Store), so I'm keen to reproduce it, but make it even faster to use - launch, talk, send (ideally just the 2 taps).
 
Upvote 0
Top