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

luke2012

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 <luca.colombo75@gmail.com>
, ToField= risto.test2013@gmail.com, 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...
 

vukanilod

Member
Licensed User
Longtime User
Hi,

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

zekigultekin

Member
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?=<emailadress@test.com>.
i want to only mail adress. helpp me
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to manually parse this string:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As String = "=?iso-8859-1?q?emailadress?=<emailadress@test.com>"
   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)
   Else
      Return ""
   End If
End Sub
 

caspali

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

BudiWang

Member
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)
 

BudiWang

Member
Licensed User
Longtime User
Have it on Header

example

Received: from mailer01.example.net (mailer01.example.net [192.168.01])
by ixde-df8.example.com (Internet Inbound) with ESMTP id 44524380000AA
for <recipient@example.com>; Sat, 24 Nov 2035 11:45:15 −0500 (EST)

on the last line on received header

can u add to your mailparser function

thanks
 

ComposerB4A

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

SMTP.Initialize("smtp.mail.yahoo.com", 465, "email123@yahoo.es","clave123","SMTP")
SMTP.To.Add("email123@yahoo.es")
SMTP.Subject = "hola"
SMTP.Body = "Hola"
SMTP.Send
...
MessageSent event not run

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

Beja

Expert
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.
 
Top