Android Tutorial Using POP3 to communicate with Android devices

Many developers face the challenge of sending data to remote devices. These devices can be at times offline, sleeping or without proper network coverage.
There are several possible solutions. The device can contact a web server which will return the required data.
Another solution is to use Google push notification framework.
Here I want to present a third solution which doesn't require any custom server and is pretty simple.
Using the Net library a device can connect to a mail server and download the mail messages.
This solution can fit very well in many cases and is very simple to manage. Mail servers are very common and are easy to work with.
All you need is an email service that supports POP3. Gmail is one such service.

The POP3 object from the Net library returns the raw messages as strings.
Additional work is required to get the message fields and especially to extract attachments from the messages.
MailParser code module is included in the attached project.
MailParser parses the messages, saves the attachments and returns Messages objects which hold the various fields.

MailParser cannot be used as a real mail client. There are many possible formats and encodings. MailParser can handle simple formats with zero or more attachments.

Parsing a message is done by calling:
B4X:
      Dim m As Message
      m = MailParser.ParseMail(MessageText, File.DirRootExternal)
The first parameter is the raw message and the second is the folder that the attachments will be saved to.

MailParser requires StringUtils library for the base64 decoding.
 

Attachments

  • MailParser.zip
    6.5 KB · Views: 2,659

Stulish

Active Member
Licensed User
Longtime User
Mail woes

I have been playing with the net library and the mail parser, i have created a listview to try and display the headers of the emails. This works (in a fashion), if i send an email then it will display in the list box, but the next time i run the program there are no emails in the list box. I have sent multiple emails and then run the B4A app and it shows each subject in the listview(each new email on a new line), but the next time i run the program no subjects show. i thought each time i run the program it would connect to gmail and download all emails (i have not set it to delete messages, and they are all still showing on the gmail webpage)
The updated downloadCompleted subroutine is below:

Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
If Success = False Then
Log("Exception:" & LastException.Message)
Else
Log(MessageId)
'Parse the mail
Dim m As Message
m = MailParser.ParseMail(MessageText, File.DirRootExternal)
Log(m)
ListView1.AddSingleLine ("From:" & m.FromField & " - " & m.Subject )
End If
End Sub

any ideas ???:sign0085: am i missing something :sign0104:
 

vukanilod

Member
Licensed User
Longtime User
Here si the code:

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
pop.useSSL = True
pop.Initialize("pop.gmail.com",995,"[email protected]","mypassword",pop)
End If

pop.ListMessages
The rest of code is same as in app you posted, but it is not working.

Tnx!
 

vukanilod

Member
Licensed User
Longtime User
Do you mean:
Dim pop As POP3?

I did this, if you thought of this. If there's something else, please let me know.
Tnx
 

vukanilod

Member
Licensed User
Longtime User
I also tried to run in debug mode and I can see that app is not entering
Sub POP_ListCompleted neither
Sub POP_DownloadCompleted.
Right after Sub Activity_Create it goes to
Sub Activity_Resume...
 

vukanilod

Member
Licensed User
Longtime User
Thank you, Erel! But I already changed the order of commands and nothing changed.

How do you mean that these events will happen when result is ready? Does this mean that I have to wait for some time to retrieve the list of messages? Tnx
 

vukanilod

Member
Licensed User
Longtime User
I've tried it again, left application waiting for half an hour and also I sent a couple of mails to this address, but nothing happened.... Obviously I'm missing some part or something...

here is the code I'm using:
Sub Process_Globals
Dim pop As POP3
End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
pop.Initialize("pop.gmail.com",995,"[email protected]","mymailpassword",pop)
pop.useSSL = True
pop.ListMessages

End If

pop.useSSL = True
pop.ListMessages




End Sub

Sub POP_ListCompleted (Success As Boolean, Messages As Map)
If Success = False Then
Log(LastException.Message)
Else
'download all messages
'change last parameter to True if you want to delete the messages from the server
For i = 0 To Messages.Size - 1
pop.DownloadMessage(Messages.GetKeyAt(i), False)
Next
End If
pop.Close
End Sub
Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
If Success = False Then
Log(LastException.Message)
Else
Log(MessageId)
'Parse the mail
Dim m As Message
m = MailParser.ParseMail(MessageText, File.DirRootExternal)
Log(m)
End If
End Sub

Sub Activity_Pause(UserClosed As Boolean)

End Sub
Sub Activity_Resume


End Sub
 

vukanilod

Member
Licensed User
Longtime User
Erel,

Everything seems to be working now, except that attachments with .csv extension are not received and downloaded to device. I'm trying to figure out why's this happening?
If you have any idea, please let me know.


Thanks a lot!
 

juancarloscm

Member
Licensed User
Longtime User
hello
a question that when I go to connect I get the following message and all parameters are well
which can be

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
pop.Initialize("pop.gmail.com", 110, "[email protected]", "xxxxxxx", "pop")
End If
pop.ListMessages
End Sub


thanks


java.net.SocketTimeoutException: Connection timed out
 
Top