00001
00002
00003
00004
00005
00006
00007 #ifndef _SMTPCONNECTION_H
00008 #define _SMTPCONNECTION_H
00009
00010 #include <string>
00011 #include <errno.h>
00012 #include <sys/socket.h>
00013 #include <sys/types.h>
00014 #include <netinet/in.h>
00015 #include <arpa/inet.h>
00016 #include <pthread.h>
00017 #include "Thread.h"
00018
00019 using namespace std;
00020
00021 namespace SmtpThread
00022 {
00023
00024 #define STATE_NOT_CONNECTED 0
00025 #define STATE_CONNECTED 1
00026 #define STATE_HELO 2
00027 #define STATE_MAILFROM 3
00028 #define STATE_RCPTTO 4
00029 #define STATE_DATA 5
00030 #define STATE_DATA_SENDING 6
00031
00032
00033 #define SA struct sockaddr
00034 #define SAI struct sockaddr_in
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 class SMTPConnection
00052 {
00053
00054
00055 public:
00056
00057
00058 SMTPConnection();
00059
00060
00061 ~SMTPConnection();
00062
00063
00064
00065
00066 enum SMTPState
00067 {
00068 NOT_CONNECTED,
00069 CONNECTED ,
00070 HELO ,
00071 MAILFROM ,
00072 RCPTTO ,
00073 DATA ,
00074 DATA_SENDING
00075 };
00076
00077
00078
00079
00080
00081
00082
00083
00084 SMTPState getSMTPState();
00085
00086
00087
00088
00089
00090 void setSMTPHost( string host );
00091
00092
00093
00094
00095
00096 void setSMTPPort( int port );
00097
00098
00099
00100
00101
00102 void setMyHostname( string myname );
00103
00104
00105
00106
00107
00108
00109
00110
00111 int sendMail( string& from, string& to, string& msg );
00112
00113
00114
00115
00116 void disconnect();
00117
00118
00119 private:
00120
00121
00122 SMTPState _smtp;
00123
00124
00125 string _host;
00126
00127
00128 string _myname;
00129
00130
00131 int _port;
00132
00133
00134 string _from;
00135
00136
00137 string _to;
00138
00139
00140 string _body;
00141
00142
00143 int _tcpSocket;
00144
00145
00146 SAI * _remoteEnd;
00147
00148
00149 void initSocket();
00150
00151
00152 void closeSocket();
00153
00154
00155
00156
00157
00158
00159 int readSMTPState();
00160
00161
00162
00163
00164
00165 int sendSMTP( const string msg );
00166
00167
00168
00169
00170
00171 int sendSMTP_CRLF( const string msg );
00172
00173
00174
00175
00176
00177 int readSMTPLine();
00178
00179
00180
00181
00182
00183
00184 int sendSMTPMessage();
00185
00186
00187
00188
00189 void initConnection();
00190
00191
00192
00193
00194
00195 char _state[4];
00196
00197
00198
00199
00200 char _buf1[ 1000 ];
00201
00202
00203
00204
00205
00206 char _buf2;
00207
00208 };
00209
00210 }
00211 #endif
00212