Small library for reading mail messages via POP3. Currently doesn\'t return all header fields, and does only plain text authorization.
Diff: pop3.h
- Revision:
- 0:ec8a3cef200d
- Child:
- 1:f2b05d1c91be
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pop3.h Mon Jan 10 21:11:01 2011 +0000 @@ -0,0 +1,121 @@ +#ifndef __POP3_H__ +#define __POP3_H__ + +#include <string> +#include <list> + +#include "mbed.h" +#include "EthernetNetIf.h" +#include "TCPSocket.h" + +#define BUFSIZE 256 + +using namespace std; + +/** + The class representing a received mail message. All fields are stored in un-altered form, so character set conversion might need to be performed. +*/ +class Pop3Message +{ + public: + /** + the 'From:' header + */ + string from; + /** + the 'Subject:' header + */ + string subject; + /** + the actual mail content, line by line + */ + list<string> content; +}; + +class Pop3CommandResponse; + +/** + Handling all POP3 related aspects. All operations are synchronous. This class expects a set-up network connection. + + Needed libraries: + * DNSResolver + * NetServices (for TCP socket), or NetServicesMin +*/ +class Pop3 +{ + public: + /** + Creates the POP3 handler + + @param servername the DNS name of the server + @param username the username + @param password the users password + */ + Pop3(const char* servername, const char* username, const char* password); + + /** + closes the connection, and cleans up resources + */ + ~Pop3(); + + /** + Retrieve a list of all message IDs. This method doesn't care whether the messages have been read already or not. + The IDs are not sorted, but returned in the order the server delivers them. + + @return the list of all message IDs stored at the server. + */ + list<string> *getMessages(); + + /** + retrieves as single message + + @param id the message ID to retrieve + @param getSig when false, the signature will be stripped (separated by the line '-- ') + @param deleteOnReceive when true the message will be deleted after a sucessful retrieval + @return the message container + */ + Pop3Message* getMessage(string id, bool getSig=false, bool deleteOnReceive=false); + + /** + @param id the ID of the message to be deleted + @return true if the deletion was sucessful + */ + bool deleteMessage(string id); + + /** + Connects to the server. Needs the ethernet connection already set up. + + @return true if the connection has been made (and the user logged in) + */ + bool init(); + + /** + closes the connection, and cleans up resources + */ + void close(); + + private: + Pop3CommandResponse* sendCommand(string cmd); + Pop3CommandResponse* sendCommandMultiResponse(string cmd); + + void onTCPSocketEvent(TCPSocketEvent e); + + string readResponseLine(); + + bool _connecting; + bool _connected; + bool _closed; + TCPSocket _socket; + + const char* _username; + const char* _password; + const char* _servername; + + char _readbuf[BUFSIZE]; + int _readpos; + int _readlen; + +}; + + +#endif \ No newline at end of file