Fixed issue #1737: Make a button to rename remote
[TortoiseGit.git] / src / Utils / CSmtp.h
blob9ae95d46db38948fc7645e5624470f25064f790e
1 // CSmtp.h: interface for the Smtp class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #if !defined __CSMTP_H__
6 #define __CSMTP_H__
8 #if _MSC_VER > 1000
9 #pragma once
10 #endif // _MSC_VER > 1000
12 #include <winsock2.h>
14 #include <assert.h>
15 #include "base64.h"
17 #pragma comment(lib, "ws2_32.lib")
19 #pragma warning(push)
20 #pragma warning(disable:4786)
22 #include <vector>
23 #include <string>
25 #define BUFFER_SIZE 10240 // SendData and RecvData buffers sizes
26 #define DELAY_IN_MS 10 // delay between send and recv functions
27 #define MSG_SIZE_IN_MB 5 // the maximum size of the message with all attachments
29 const char BOUNDARY_TEXT[] = "__MESSAGE__ID__54yg6f6h6y456345";
31 enum CSmtpError
33 CSMTP_NO_ERROR = 0,
34 CSMTP_WSA_STARTUP = 100, // WSAGetLastError()
35 CSMTP_WSA_VER,
36 CSMTP_WSA_SEND,
37 CSMTP_WSA_RECV,
38 CSMTP_WSA_CONNECT,
39 CSMTP_WSA_GETHOSTBY_NAME_ADDR,
40 CSMTP_WSA_INVALID_SOCKET,
41 CSMTP_WSA_HOSTNAME,
42 CSMTP_BAD_IPV4_ADDR,
43 CSMTP_UNDEF_MSG_HEADER = 200,
44 CSMTP_UNDEF_MAILFROM,
45 CSMTP_UNDEF_SUBJECT,
46 CSMTP_UNDEF_RECIPENTS,
47 CSMTP_UNDEF_LOGIN,
48 CSMTP_UNDEF_PASSWORD,
49 CSMTP_UNDEF_RECIPENT_MAIL,
50 CSMTP_COMMAND_MAIL_FROM = 300,
51 CSMTP_COMMAND_EHLO,
52 CSMTP_COMMAND_AUTH_LOGIN,
53 CSMTP_COMMAND_DATA,
54 CSMTP_COMMAND_QUIT,
55 CSMTP_COMMAND_RCPT_TO,
56 CSMTP_MSG_BODY_ERROR,
57 CSMTP_CONNECTION_CLOSED = 400, // by server
58 CSMTP_SERVER_NOT_READY, // remote server
59 CSMTP_FILE_NOT_EXIST,
60 CSMTP_MSG_TOO_BIG,
61 CSMTP_BAD_LOGIN_PASS,
62 CSMTP_UNDEF_XYZ_RESPOMSE,
63 CSMTP_LACK_OF_MEMORY
66 enum CSmptXPriority
68 XPRIORITY_HIGH = 2,
69 XPRIORITY_NORMAL = 3,
70 XPRIORITY_LOW = 4
73 class CSmtp
75 public:
76 CSmtp();
77 virtual ~CSmtp();
78 bool AddRecipient(const char *email, const char *name=NULL);
79 bool AddBCCRecipient(const char *email, const char *name=NULL);
80 bool AddCCRecipient(const char *email, const char *name=NULL);
81 bool AddAttachment(const char *path);
82 const unsigned int GetBCCRecipientCount();
83 const unsigned int GetCCRecipientCount();
84 const unsigned int GetRecipientCount();
85 const char* const GetLocalHostIP();
86 const char* const GetLocalHostName();
87 const char* const GetMessageBody();
88 const char* const GetReplyTo();
89 const char* const GetMailFrom();
90 const char* const GetSenderName();
91 const char* const GetSubject();
92 const char* const GetXMailer();
93 CSmptXPriority GetXPriority();
94 CSmtpError GetLastError();
95 bool Send();
96 void SetMessageBody(const char*);
97 void SetSubject(const char*);
98 void SetSenderName(const char*);
99 void SetSenderMail(const char*);
100 void SetReplyTo(const char*);
101 void SetXMailer(const char*);
102 void SetLogin(const char*);
103 void SetPassword(const char*);
104 void SetXPriority(CSmptXPriority);
105 void SetSMTPServer(const char* server,const unsigned short port=0);
107 private:
108 CSmtpError m_oError;
109 char* m_pcLocalHostName;
110 char* m_pcMailFrom;
111 char* m_pcNameFrom;
112 char* m_pcSubject;
113 char* m_pcMsgBody;
114 char* m_pcXMailer;
115 char* m_pcReplyTo;
116 char* m_pcIPAddr;
117 char* m_pcLogin;
118 char* m_pcPassword;
119 char* m_pcSMTPSrvName;
120 unsigned short m_iSMTPSrvPort;
121 CSmptXPriority m_iXPriority;
122 char *SendBuf;
123 char *RecvBuf;
125 WSADATA wsaData;
126 SOCKET hSocket;
128 struct Recipent
130 std::string Name;
131 std::string Mail;
134 std::vector<Recipent> Recipients;
135 std::vector<Recipent> CCRecipients;
136 std::vector<Recipent> BCCRecipients;
137 std::vector<std::string> Attachments;
139 bool ReceiveData();
140 bool SendData();
141 bool FormatHeader(char*);
142 int SmtpXYZdigits();
143 SOCKET ConnectRemoteServer(const char* server, const unsigned short port=NULL);
145 friend char* GetErrorText(CSmtpError);
148 #pragma warning(pop)
150 #endif // __CSMTP_H__