-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSmtp.h
296 lines (263 loc) · 7.13 KB
/
CSmtp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// CSmtp.h: interface for the Smtp class.
//
//////////////////////////////////////////////////////////////////////
#pragma once
#ifndef __CSMTP_H__
#define __CSMTP_H__
#include <vector>
#include <string.h>
#include <assert.h>
#ifdef __linux__
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <openssl/ssl.h>
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#ifndef HAVE__STRNICMP
#define HAVE__STRNICMP
#define _strnicmp strncasecmp
#endif
#define OutputDebugStringA(buf)
typedef unsigned short WORD;
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct hostent* LPHOSTENT;
typedef struct servent* LPSERVENT;
typedef struct in_addr* LPIN_ADDR;
typedef struct sockaddr* LPSOCKADDR;
#define LINUX
#else
#include <winsock2.h>
#include <time.h>
#pragma comment(lib, "ws2_32.lib")
//Add "openssl-0.9.8l\inc32" to Additional Include Directories
#include "openssl\ssl.h"
#if _MSC_VER < 1400
#define snprintf _snprintf
#else
#define snprintf sprintf_s
#endif
#endif
#include "md5.h"
#define TIME_IN_SEC 3*60 // how long client will wait for server response in non-blocking mode
#define BUFFER_SIZE 10240 // SendData and RecvData buffers sizes
#define MSG_SIZE_IN_MB 25 // the maximum size of the message with all attachments
#define COUNTER_VALUE 100 // how many times program will try to receive data
const char BOUNDARY_TEXT[] = "__MESSAGE__ID__54yg6f6h6y456345";
enum CSmptXPriority
{
XPRIORITY_HIGH = 2,
XPRIORITY_NORMAL = 3,
XPRIORITY_LOW = 4
};
class ECSmtp
{
public:
enum CSmtpError
{
CSMTP_NO_ERROR = 0,
WSA_STARTUP = 100, // WSAGetLastError()
WSA_VER,
WSA_SEND,
WSA_RECV,
WSA_CONNECT,
WSA_GETHOSTBY_NAME_ADDR,
WSA_INVALID_SOCKET,
WSA_HOSTNAME,
WSA_IOCTLSOCKET,
WSA_SELECT,
BAD_IPV4_ADDR,
UNDEF_MSG_HEADER = 200,
UNDEF_MAIL_FROM,
UNDEF_SUBJECT,
UNDEF_RECIPIENTS,
UNDEF_LOGIN,
UNDEF_PASSWORD,
BAD_LOGIN_PASSWORD,
BAD_DIGEST_RESPONSE,
BAD_SERVER_NAME,
UNDEF_RECIPIENT_MAIL,
COMMAND_MAIL_FROM = 300,
COMMAND_EHLO,
COMMAND_AUTH_PLAIN,
COMMAND_AUTH_LOGIN,
COMMAND_AUTH_CRAMMD5,
COMMAND_AUTH_DIGESTMD5,
COMMAND_DIGESTMD5,
COMMAND_DATA,
COMMAND_QUIT,
COMMAND_RCPT_TO,
MSG_BODY_ERROR,
CONNECTION_CLOSED = 400, // by server
SERVER_NOT_READY, // remote server
SERVER_NOT_RESPONDING,
SELECT_TIMEOUT,
FILE_NOT_EXIST,
MSG_TOO_BIG,
BAD_LOGIN_PASS,
UNDEF_XYZ_RESPONSE,
LACK_OF_MEMORY,
TIME_ERROR,
RECVBUF_IS_EMPTY,
SENDBUF_IS_EMPTY,
OUT_OF_MSG_RANGE,
COMMAND_EHLO_STARTTLS,
SSL_PROBLEM,
COMMAND_DATABLOCK,
STARTTLS_NOT_SUPPORTED,
LOGIN_NOT_SUPPORTED
};
ECSmtp(CSmtpError err_) : ErrorCode(err_) {}
CSmtpError GetErrorNum(void) const {return ErrorCode;}
std::string GetErrorText(void) const;
private:
CSmtpError ErrorCode;
};
enum SMTP_COMMAND
{
command_INIT,
command_EHLO,
command_AUTHPLAIN,
command_AUTHLOGIN,
command_AUTHCRAMMD5,
command_AUTHDIGESTMD5,
command_DIGESTMD5,
command_USER,
command_PASSWORD,
command_MAILFROM,
command_RCPTTO,
command_DATA,
command_DATABLOCK,
command_DATAEND,
command_QUIT,
command_STARTTLS
};
// TLS/SSL extension
enum SMTP_SECURITY_TYPE
{
NO_SECURITY,
USE_TLS,
USE_SSL,
DO_NOT_SET
};
typedef struct tagCommand_Entry
{
SMTP_COMMAND command;
int send_timeout; // 0 means no send is required
int recv_timeout; // 0 means no recv is required
int valid_reply_code; // 0 means no recv is required, so no reply code
ECSmtp::CSmtpError error;
}Command_Entry;
class CSmtp
{
public:
CSmtp();
virtual ~CSmtp();
void AddRecipient(const char *email, const char *name=NULL);
void AddBCCRecipient(const char *email, const char *name=NULL);
void AddCCRecipient(const char *email, const char *name=NULL);
void AddAttachment(const char *path);
void AddMsgLine(const char* text);
void ClearMessage();
bool ConnectRemoteServer(const char* szServer, const unsigned short nPort_=0,
SMTP_SECURITY_TYPE securityType=DO_NOT_SET,
bool authenticate=true, const char* login=NULL,
const char* password=NULL);
void DisconnectRemoteServer();
void DelRecipients(void);
void DelBCCRecipients(void);
void DelCCRecipients(void);
void DelAttachments(void);
void DelMsgLines(void);
void DelMsgLine(unsigned int line);
void ModMsgLine(unsigned int line,const char* text);
unsigned int GetBCCRecipientCount() const;
unsigned int GetCCRecipientCount() const;
unsigned int GetRecipientCount() const;
const char* GetLocalHostIP() const;
const char* GetLocalHostName();
const char* GetMsgLineText(unsigned int line) const;
unsigned int GetMsgLines(void) const;
const char* GetReplyTo() const;
const char* GetMailFrom() const;
const char* GetSenderName() const;
const char* GetSubject() const;
const char* GetXMailer() const;
CSmptXPriority GetXPriority() const;
void Send();
void SetCharSet(const char *sCharSet);
void SetLocalHostName(const char *sLocalHostName);
void SetSubject(const char*);
void SetSenderName(const char*);
void SetSenderMail(const char*);
void SetReplyTo(const char*);
void SetReadReceipt(bool requestReceipt=true);
void SetXMailer(const char*);
void SetLogin(const char*);
void SetPassword(const char*);
void SetXPriority(CSmptXPriority);
void SetSMTPServer(const char* server, const unsigned short port=0, bool authenticate=true);
private:
std::string m_sLocalHostName;
std::string m_sMailFrom;
std::string m_sNameFrom;
std::string m_sSubject;
std::string m_sCharSet;
std::string m_sXMailer;
std::string m_sReplyTo;
bool m_bReadReceipt;
std::string m_sIPAddr;
std::string m_sLogin;
std::string m_sPassword;
std::string m_sSMTPSrvName;
unsigned short m_iSMTPSrvPort;
bool m_bAuthenticate;
CSmptXPriority m_iXPriority;
char *SendBuf;
char *RecvBuf;
SOCKET hSocket;
bool m_bConnected;
struct Recipient
{
std::string Name;
std::string Mail;
};
std::vector<Recipient> Recipients;
std::vector<Recipient> CCRecipients;
std::vector<Recipient> BCCRecipients;
std::vector<std::string> Attachments;
std::vector<std::string> MsgBody;
void ReceiveData(Command_Entry* pEntry);
void SendData(Command_Entry* pEntry);
void FormatHeader(char*);
int SmtpXYZdigits();
void SayHello();
void SayQuit();
// TLS/SSL extension
public:
SMTP_SECURITY_TYPE GetSecurityType() const
{ return m_type; }
void SetSecurityType(SMTP_SECURITY_TYPE type)
{ m_type = type; }
bool m_bHTML;
private:
SMTP_SECURITY_TYPE m_type;
SSL_CTX* m_ctx;
SSL* m_ssl;
void ReceiveResponse(Command_Entry* pEntry);
void InitOpenSSL();
void OpenSSLConnect();
void CleanupOpenSSL();
void ReceiveData_SSL(SSL* ssl, Command_Entry* pEntry);
void SendData_SSL(SSL* ssl, Command_Entry* pEntry);
void StartTls();
};
#endif // __CSMTP_H__