5

I am trying to develop a small application for sending and receiving emails on the Android plataform. Currently i have been using the Javamail api trying to send an email. However i thought that if i implement my app using javamail how am i going to receive an email and get a notification from my app that i have recieved it? Is this having to do with Service and Provder classes found on Android? I am a complete beginner on android.

Also i tried this piece of code found here :

Android Programming - Send mail

with no luck since application does not work.

Community
  • 1
  • 1
skay-
  • 1,546
  • 3
  • 16
  • 26
  • Sending and receiving mails are unfortunately two very different things... One thing is a server, and the other a client. This is like writing a web browser or a HTTP server. It's not the same thing. – m0skit0 Nov 04 '11 at 12:47
  • Yes i know, but how am i going to recieve an email on android? Is there an email class? Because i couldnt find a way. I want to create very simple application like default email one. – skay- Nov 04 '11 at 12:48
  • 1
    You cannot receive an email since you're not an email server and you don't have a domain, not to talk about DNS server. You maybe mean fetch an email from an email server. For that you need to use POP3/IMAP protocols. Check for a library for that. – m0skit0 Nov 04 '11 at 12:50
  • Hmm, yes basicaly my problem is how to fetch the data. I know that k9 open source app has that functionality and they provide the ode but since there are over 150 classes i cannot find exaclty where they are fetching the mails. – skay- Nov 04 '11 at 12:54
  • As I said, search for POP3/IMAP. – m0skit0 Nov 04 '11 at 12:57

1 Answers1

5

JavaMail can also be used to fetch mails from a POP3 server. As an example look at this class: PopMailImporter.java, especially the importMails and importMail function.

Hontvári Levente
  • 1,242
  • 10
  • 16
  • Soo i can built the email app just by using the Javamail right? – skay- Nov 04 '11 at 13:09
  • @Stelios An email app can be anything, but at least I can say that JavaMail make it easy to send mails to an SMTP (more specifically submission) server and receive mails from a POP3 server. – Hontvári Levente Nov 04 '11 at 13:51
  • Thank you very much, i am currently looking at the class, but can you help me a bit more with its functionality. I am new to this kind of stuff and i am stucked=/. Thanks in advance. – skay- Nov 05 '11 at 12:44
  • @Stelios You are welcome. If you have questions about the example class above then I can help, but I do not know anything about Android. If you have other specific questions I would recommend you to post it in a new question. – Hontvári Levente Nov 05 '11 at 20:56
  • Thanks again. Basically i cant really understand importMails(..) method. First it creates the properties object, then the Session; remotehost should be: smtp.gmail.com if i am not mistaken. Then i connect to the server and it then it calls the other method, and so on. This is a bit complicated example and it seems that i miss stuff. If i am able to do it in java then it should not have problem and android aswell. Do you have another more simple example? Thanks again. – skay- Nov 05 '11 at 22:28
  • @Stelios The Properties object stores configuration properties, but it is empty in the example. The function creates a POP3 session, connects to a store (here that means a POP3 account on a server), opens a folder (there is only one folder in POP3 anyway), then retrieves all messages. It iterates over the list of messages and reads their content by opening them as a stream. – Hontvári Levente Nov 06 '11 at 01:17
  • @Stelios No, smtp.gmail.com is for sending. For retrieving mail through POP3 you have to use pop.gmail.com. Unfortunately the POP server maintained by google does encryption in a non-standard way. See another example, http://www.javaer.org/j2ee/3-javamail/8-javamail-connecting-gmail-using-pop3-with-ssl specifically for google. – Hontvári Levente Nov 06 '11 at 01:25
  • Ok thank you very much Hontvari. I have used the code example and i was able to fetch the data from my email account. One thing though that i dont really understand is that gmail.openFolder("INBOX"); suppose to open the inbox folder only? Because i currently have only INBOX selected but emails from trash folder are also displayed. Also now that i have run the application once, when i re-run it it says that there are no messages even though the last time i ve run it there was. – skay- Nov 06 '11 at 03:53
  • And last when the body is printed on the screen for some reason i get the objects body reference and a binary output like this: javax.mail.util.SharedByteArrayInputStream@3abc87 CONTENT-TYPE: multipart/alternative; boundary=0015174c3d6ad784b804b108d2d5. Also do you know how do i distinguish when an email is not read? I know i have asked many questions but thanks in advance for everything and i really apriecate any help. – skay- Nov 06 '11 at 03:55
  • @Stelios INBOX is a dummy parameter, POP3 does not have a concept of folders. JavaMail uses the same API for IMAP, another mail retrieval protocol which does know about folders. In a POP3 mailbox you get all incoming mails independently from their final placement in gmail. I don't see your code, so I do not know why are messages deleted, but if you copied the first example, then comment out the "message.setFlag(Flags.Flag.DELETED, true); line. – Hontvári Levente Nov 06 '11 at 19:00
  • "Also do you know how do i distinguish when an email is not read?" This is not possible in POP3. I can imagine that it is possible in IMAP. AFAIK gmail also maintains an IMAP service. I do not understand your question about the output, especially because I do not see the code, maybe post the code as a new question. – Hontvári Levente Nov 06 '11 at 19:15
  • Thanks for your valuable information. I am using the code that you provided here on discussion not on the answer.Thanks again and i will post my problem, with the code in another new Question. – skay- Nov 07 '11 at 00:07