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:
      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.


    6.5 KB · Views: 2,404


Well-Known Member
Licensed User
Longtime User
I checked :

The mail contains an attachement and the "Headers" and "Msg" have the following content :

<!>anywheresoftware.b4a.keywords.Common 151<!> Headers = Content-Type: text/plain; charset="utf-8"
<!>anywheresoftware.b4a.keywords.Common 151<!> Msg = [Attachments=(ArrayList) [], BCCField=, Body=
, CCField= , ContentType= multipart/mixed; boundary="asdasdwdwqd__HV_qwdqwdddwq", FromField= Risto Lite <>
, ToField=, Subject= Risto_03/16/2013, IsInitialized=true

You can see that the Content-Type in the Headers is different from the Content-Type in the Msg.
The HandlePart sub check for the Content-Type in the "Headers" and fail to download the attachment.

I don't known why...


Licensed User
Longtime User

I'm having problems runing this pop example. it seemed to be working one year ago, and now there is only blank screen... Sorry for reopening the subject, but it is quite strange...


Licensed User
Longtime User
how can i solve thiss
when i writing edittext.text=m.FromField
it came like this =?iso-8859-1?q?emailadress?=<>.
i want to only mail adress. helpp me


B4X founder
Staff member
Licensed User
Longtime User
You will need to manually parse this string:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As String = "=?iso-8859-1?q?emailadress?=<>"
   Log (FindEmail(s))
End Sub
Sub FindEmail(s As String) As String
   Dim m As Matcher = Regex.Matcher("<([^>]*)>", s)
   If m.Find Then
      Return m.Group(1)
      Return ""
   End If
End Sub


Licensed User
Longtime User
Sorry erel. How i can check new email arrive on pop in background with a notification?


Licensed User
Longtime User
Hi Erel can u add function to get date time the server mail received the email (not based on phone time)


Licensed User
Longtime User
Have it on Header


Received: from ( [192.168.01])
by (Internet Inbound) with ESMTP id 44524380000AA
for <>; Sat, 24 Nov 2035 11:45:15 −0500 (EST)

on the last line on received header

can u add to your mailparser function



Licensed User
Longtime User
Hello to all, i search send directly a email (without user intervention), i try:

SMTP.Initialize("", 465, "","clave123","SMTP")
SMTP.Subject = "hola"
SMTP.Body = "Hola"
MessageSent event not run

the question, ¿because not send the email? o_O
Last edited:


Licensed User
Longtime User
Hi Erel,

Today I run the pop3 example.. I can only see a blank screen with the title: POP3 Example.
Any help appreciated.

thanks in advance.