Dependents: TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more
SMTPClient.h
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /** \file 00025 SMTP Client header file 00026 */ 00027 00028 #ifndef SMTP_CLIENT_H 00029 #define SMTP_CLIENT_H 00030 00031 class EmailMessage; 00032 00033 #include "core/net.h" 00034 #include "api/TCPSocket.h" 00035 #include "api/DNSRequest.h" 00036 #include "EmailMessage.h" 00037 #include "mbed.h" 00038 00039 ///SMTP client results 00040 enum SMTPResult { 00041 SMTP_OK, ///<Success 00042 SMTP_PROCESSING, ///<Processing 00043 SMTP_DNS, ///<Could not resolve name 00044 SMTP_PRTCL, ///<Protocol error 00045 SMTP_TIMEOUT, ///<Connection timeout 00046 SMTP_DISC ///<Disconnected 00047 }; 00048 00049 ///SMTP authentication 00050 enum SMTPAuth { 00051 SMTP_AUTH_NONE, ///<No authentication 00052 SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication 00053 }; 00054 #include "core/netservice.h" 00055 00056 ///A simple SMTP Client 00057 /** 00058 The SMTPClient is composed of: 00059 - The actual client (SMTPClient) 00060 - A class (EmailMessage) to hold the message addresses and content for sending 00061 */ 00062 class SMTPClient : protected NetService { 00063 public: 00064 ///Instantiates the SMTP client 00065 SMTPClient(); 00066 00067 ///Destructor for the SMTP client 00068 virtual ~SMTPClient(); 00069 00070 ///Full constructor for the SMTP client 00071 /** 00072 @param host : SMTP server host 00073 @param heloDomain : domain name of client 00074 @param user : username 00075 @param password : password 00076 @param auth : authentication type 00077 */ 00078 SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth); 00079 00080 ///Set server host 00081 /** 00082 @param host : SMTP server host 00083 */ 00084 void setServer(const Host& host); 00085 00086 ///Provides a plain authentication feature (Base64 encoded username and password) 00087 /** 00088 @param user : username 00089 @param password : password 00090 */ 00091 void setAuth(const char* user, const char* password); // Plain authentication 00092 00093 ///Turns off authentication 00094 void clearAuth(); // Clear authentication 00095 00096 ///Set HELO domain (defaults to localhost if not set) 00097 /** 00098 @param heloDomain : domain name of client (strictly should be fully qualified domain name) 00099 */ 00100 void setHeloDomain(const char* heloDomain); 00101 00102 //High Level setup functions 00103 ///Sends the message (blocking) 00104 /** 00105 @param pMessage : pointer to a message 00106 00107 Blocks until completion 00108 */ 00109 SMTPResult send(EmailMessage* pMessage); //Blocking 00110 00111 ///Sends the message (non blocking) 00112 /** 00113 @param pMessage : pointer to a message 00114 @param pMethod : callback function 00115 00116 The function returns immediately and calls the callback on completion or error 00117 */ 00118 SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking 00119 00120 ///Sends the message (non blocking) 00121 /** 00122 @param pMessage : pointer to a message 00123 @param pItem : instance of class on which to execute the callback method 00124 @param pMethod : callback method 00125 00126 The function returns immediately and calls the callback on completion or error 00127 */ 00128 template<class T> 00129 SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking 00130 setOnResult(pItem, pMethod); 00131 doSend(pMessage); 00132 return SMTP_PROCESSING; 00133 } 00134 00135 ///Sends the message (non blocking) 00136 /** 00137 @param pMessage : pointer to a message 00138 00139 The function returns immediately and calls the previously set callback on completion or error 00140 */ 00141 void doSend(EmailMessage* pMessage); 00142 00143 ///Setup the result callback 00144 /** 00145 @param pMethod : callback function 00146 */ 00147 void setOnResult( void (*pMethod)(SMTPResult) ); 00148 00149 ///Setup the result callback 00150 /** 00151 @param pItem : instance of class on which to execute the callback method 00152 @param pMethod : callback method 00153 */ 00154 class CDummy; 00155 template<class T> 00156 void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) { 00157 m_pCb = NULL; 00158 m_pCbItem = (CDummy*) pItem; 00159 m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod; 00160 } 00161 00162 ///Setup timeout 00163 /** 00164 @param ms : time of connection inactivity in ms after which the request should timeout 00165 */ 00166 void setTimeout(int ms); 00167 00168 ///Gets the last response from the server 00169 string& getLastResponse(); 00170 00171 virtual void poll(); //Called by NetServices 00172 00173 protected: 00174 int rc(char* buf); //Return code 00175 void process(bool writeable); //Main state-machine 00176 00177 void resetTimeout(); 00178 00179 void init(); 00180 void close(); 00181 00182 void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary 00183 void connect(); //Start Connection 00184 00185 int tryRead(); //Read data and try to feed output 00186 void readData(); //Data has been read 00187 void writeData(); //Data has been written & buf is free 00188 00189 void onTCPSocketEvent(TCPSocketEvent e); 00190 void onDNSReply(DNSReply r); 00191 void onResult(SMTPResult r); //Called when exchange completed or on failure 00192 void onTimeout(); //Connection has timed out 00193 00194 string encodePlainAuth(); // Encode plain authentication (username and password in based 64) 00195 bool okPostAuthentication(int code); // True if server response is ok following authentication 00196 00197 private: 00198 SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available 00199 00200 CDummy* m_pCbItem; 00201 void (CDummy::*m_pCbMeth)(SMTPResult); 00202 void (*m_pCb)(SMTPResult); 00203 00204 TCPSocket* m_pTCPSocket; 00205 00206 Timer m_watchdog; 00207 int m_timeout; 00208 00209 DNSRequest* m_pDnsReq; 00210 Host m_server; 00211 00212 bool m_closed; 00213 00214 enum SMTPStep { 00215 SMTP_HELLO, 00216 SMTP_AUTH, 00217 SMTP_FROM, 00218 SMTP_TO, 00219 SMTP_DATA, 00220 SMTP_BODY, 00221 SMTP_BODYMORE, 00222 SMTP_EOF, 00223 SMTP_BYE, 00224 SMTP_CLOSED 00225 }; 00226 00227 SMTPStep m_state; 00228 00229 EmailMessage* m_pMessage; 00230 00231 int m_posInMsg; 00232 int m_posInCRLF; 00233 00234 SMTPResult m_blockingResult; //Result if blocking mode 00235 string m_response; 00236 int m_responseCode; 00237 string m_username; 00238 string m_password; 00239 SMTPAuth m_auth; 00240 string m_heloDomain; 00241 00242 vector<string>::iterator m_To; 00243 }; 00244 00245 #endif // SMTP_CLIENT_H
Generated on Tue Jul 12 2022 16:54:38 by 1.7.2