Open default email client

JonPM

Well-Known Member
Licensed User
Longtime User
Been searching for a way to open the devices default email app to send an email. Will also need the To, Subject, and message to populate the opened email. Can't figure out how to so this. Any advice?

Sent from my DROIDX
 

pluton

Active Member
Licensed User
Longtime User
Try with this:

This will add menu button - Mail Me

B4X:
Sub Activity_Create(FirstTime As Boolean)
activity.AddMenuItem2("Mail Author","mailme",LoadBitmap(File.DirAssets,"mail.png"))
[COLOR="Green"]' here goes more code so i just deleted[/COLOR]
End Sub
[COLOR="Green"]' And then just put this:[/COLOR]
Sub mailme_click
   Dim mailapp As Email
   mailapp.To.Add("[email protected]")
   mailapp.Subject = "From my app"
   StartActivity(mailapp.GetIntent)

End Sub

Use Phone library
Just type mailapp and you will see a list to add:
image.png
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Try with this:

This will add menu button - Mail Me

B4X:
Sub Activity_Create(FirstTime As Boolean)
activity.AddMenuItem2("Mail Author","mailme",LoadBitmap(File.DirAssets,"mail.png"))
[COLOR="Green"]' here goes more code so i just deleted[/COLOR]
End Sub
[COLOR="Green"]' And then just put this:[/COLOR]
Sub mailme_click
   Dim mailapp As Email
   mailapp.To.Add("[email protected]")
   mailapp.Subject = "From my app"
   StartActivity(mailapp.GetIntent)

End Sub

Use Phone library
Just type mailapp and you will see a list to add:
image.png

Tried this on emulator and didn't work. It opened an email but didn't auto fill the to, subject, and body. Is this methods inconsistent across devices?

Sent from my DROIDX
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
How to Call Stock Email and Pass Info

Been searching for a way to open the devices default email app to send an email. Will also need the To, Subject, and message to populate the opened email. Can't figure out how to so this. Any advice?

Sent from my DROIDX

I use this code in several APPs I have written and it works well. Place this code in your app and call it with your text or variables. Example:

SendMail("[email protected]", "This is my email message here", "My Email Subject", "listoffriends.txt")

B4X:
Sub SendMail(SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
   Dim ei As Email
   ei.To.Add(Trim(SendTO))
   ei.Body = SendBody
   ei.Subject = Trim(SendSub)
   If SendAtt <> "" Then
      ei.Attachments.Add(SendAtt)
   End If   
   StartActivity(ei.GetIntent)
End Sub

Hope this helps,

Margret
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Thanks Margret, that SendMail is very helpful to me!

Do you perhaps have the equivalent for SendSMS?

Edit:
After posting the above I realised the Phonesms.Send already does that.
 
Last edited:
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
I use this code in several APPs I have written and it works well. Place this code in your app and call it with your text or variables. Example:

SendMail("[email protected]", "This is my email message here", "My Email Subject", "listoffriends.txt")

B4X:
Sub SendMail(SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
   Dim ei As Email
   ei.To.Add(Trim(SendTO))
   ei.Body = SendBody
   ei.Subject = Trim(SendSub)
   If SendAtt <> "" Then
      ei.Attachments.Add(SendAtt)
   End If   
   StartActivity(ei.GetIntent)
End Sub

Hope this helps,

Margret

Is there a library I need to include? It's not recognizing Trim when I compile
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Upvote 0

GPJMA

Member
Licensed User
Longtime User
'trim error'

Hi Margret I also had this problem and followed your great advice but I still get the problem with 'trim' not being declared.
I loaded your 'S' module but no difference.
I did load up your string utilities demo and that works fine.
What am I missing, apart from grey cells?
I am desperately trying to send a simple email from my app but just cannot get it right.
Thanks:sign0161:

Here is the error message when compiling
Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'trim' is used before it was assigned any value.
Occurred on line: 283
ei.To.Add(Trim(SendTO))
and here is your sub code
Sub SendMail(SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
Dim ei As Email
ei.To.Add(Trim(SendTO))
ei.Body = SendBody
ei.Subject = Trim(SendSub)
If SendAtt <> "" Then
ei.Attachments.Add(SendAtt)
End If

StartActivity(ei.GetIntent)
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
String Functions & Email Issues

OK, let me see if I can explain this correctly. The string functions should be added to your project as a code module. Open my string functions project and select to view the module named s. Press Ctrl + A to select all. Now press Ctrl + C to copy it to memory. Now open your project and then in the IDE menu select:

Project
Add New Module
Code Module

When ask for the name enter s, then press enter. Now select this new code module window and delete everything in the new s code module. Now press Ctrl + V to view and paste the clipboard. You should now see the string functions on screen. Now save your project. Now copy the code below and paste it at the end of the s code module and save your project again:

B4X:
Sub SendMail(SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
   Dim ei As Email
   ei.To.Add(Trim(SendTO))
   ei.Body = SendBody
   ei.Subject = Trim(SendSub)
   If SendAtt <> "" Then
      ei.Attachments.Add(SendAtt)
   End If   
   StartActivity(ei.GetIntent)
End Sub

You should now be set to use this any time by calling:

B4X:
s.SendMail("[email protected]", "This is my email message here", "My Email Subject", "listoffriends.txt")

NOTICE: Make sure to put the s. before the SendMail function as it is now part of the s code module. Let me know if this helps or if you have other problems.

Thanks,

Margret
 
Last edited:
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
B4X:
Sub SendMail(SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
   Dim ei As Email
   ei.To.Add(Trim(SendTO))
   ei.Body = SendBody
   ei.Subject = Trim(SendSub)
   If SendAtt <> "" Then
      ei.Attachments.Add(SendAtt)
   End If  
   StartActivity(ei.GetIntent)
End Sub

I've been using this code for years but I need to change out-going emails from HTML to plain text.

Does anyone know how to use IsBodyHTML with this code or some other workaround?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@NJDude I know that as I personally use the Net library to send Invoices to customers. It was just that @nfordbscndrd specifically stated "IsBodyHTML" and not "HtmlBody", so I was just wondering is he was using a library that I was not aware off as I've not come across "IsBodyHTML" before.

Cheers :)
 
Upvote 0
Top