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,791

Beja

Expert
Licensed User
Longtime User
also tried pop.gmail.com and got this:
java.net.UnknownHostException: pop.gmail.com
 

Beja

Expert
Licensed User
Longtime User
Thanks Erel,
In fact I use real device except in rare cases when I use the emulator..
Any other suggestion appreciated.
 

Beja

Expert
Licensed User
Longtime User
Hi Erel,
This is the only line I modified:
pop.Initialize("pop.google.com", 995, "user@example.com", "password", "pop")

I could use the Internet for all other purposes.
 

IslamQabel

Active Member
Licensed User
Longtime User
Hi Erel:
Unfortunately, i receive the following error message
*** Remote compilation mode ***
Parsing code. 0.02
Compiling code. 0.02
Compiling layouts code. 0.00
Sending data to remote compiler. Error
step: Compiling generated Java code.
javac 1.7.0_09
src\anywheresoftware\b4a\samples\pop3\main.java:267: error: package anywheresoftware.b4a.net does not exist
public static anywheresoftware.b4a.net.POPWrapper _pop = null;
^
1 error
 

IslamQabel

Active Member
Licensed User
Longtime User
Dear All......I am using a program that you can send email using SMTP ......The user have to enter the password in text box, so i want to display the entered password as **************** to hide what written how to do it???

Thanks
 

tactic1960

Member
Licensed User
Longtime User
Hi Erel, i used MailParser for save an attachment by i have then same problem of Luke2012. I send the mail with SMTP perfectly, receive the same mail with POP3 but MailParser don't save the file.
I have debugged MailParser and i have see the problem,the function ParseMultipartBody fails.
I attach a file with the mail, if you debug can see the problem

Thanks
 

Attachments

  • mail.txt
    9.7 KB · Views: 445

Erel

B4X founder
Staff member
Licensed User
Longtime User
It misses the closing boundary.
Change ParseMultiparyBody to:
B4X:
Sub ParseMultipartBody (Mail As String, Msg As Message)
   'find first boundary
   index = Mail.IndexOf2("--" & boundary, index)
   ReadNextLine(Mail)
   Dim headers As StringBuilder
   headers.Initialize
   Do While index < Mail.Length
     Dim line As String
     line = ReadNextLine(Mail)
     If line.Length > 0 Then
       headers.Append(line).Append(" ")
     Else If index < Mail.Length Then
       Dim nextPart As Int
       nextPart = Mail.IndexOf2("--" & boundary, index)
       If nextPart = -1 Then nextPart = Mail.Length + 4 '<-- new line
       If nextPart-4 > index Then
         HandlePart(headers.ToString, Mail.SubString2(index, nextPart-4), Msg)
       End If
       index = nextPart
       ReadNextLine(Mail)
       headers.Initialize
     End If
   Loop
End Sub
 

le_toubib

Active Member
Licensed User
Longtime User
hi all
i m trying this method to communicate with other devices, using gmail, but the error log says i need to web login first ,
indeed i dont to give the username n password to my users to login (that wouldnt even work new gmail security restrictions)
my questions : what are the other free email servers that can work seemlessly with the good old username and password method) using net library without user interference
 

le_toubib

Active Member
Licensed User
Longtime User
Yes, and enabled pop3 from gmail settings ... Smtp works fine (with security alerts ask the time) but Pop3 requires web login first then it works
 

le_toubib

Active Member
Licensed User
Longtime User
I tried yandex for that purpose , it worked after enabling pop3 without requiring web login
 
Top