-
Notifications
You must be signed in to change notification settings - Fork 21
/
Unit2.pas
207 lines (188 loc) · 6.67 KB
/
Unit2.pas
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
unit Unit2;
interface
uses
Winapi.Windows
, Winapi.Messages
, System.SysUtils
, System.Variants
, System.Classes
, Vcl.Graphics
, Vcl.Controls
, Vcl.StdCtrls
, Vcl.Forms
, Vcl.Dialogs
, Vcl.ExtCtrls
, IdSASL
, IdSASLCollection
, IdExplicitTLSClientServerBase
, EmailOAuthDm
, IdSASL.Oauth.OAuth2Bearer
, IdSASL.Oauth.XOAUTH2
, Email.Demo.Types
, Globals // rename from globals.sample.pas and update contents if missing
;
type
TForm2 = class(TForm)
btnAuthenticate: TButton;
btnSendMsg: TButton;
rgEmailProviders: TRadioGroup;
btnCheckMsg: TButton;
btnClearAuthToken: TButton;
btnCheckIMAP: TButton;
Memo1: TMemo;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnCheckMsgClick(Sender: TObject);
procedure btnClearAuthTokenClick(Sender: TObject);
procedure rgEmailProvidersClick(Sender: TObject);
procedure btnCheckIMAPClick(Sender: TObject);
procedure btnAuthenticateClick(Sender: TObject);
procedure btnSendMsgClick(Sender: TObject);
private
{ Private declarations }
EmailOAuthDataModule : TEmailOAuthDataModule;
procedure LogMsg(const msg: string);
public
{ Public declarations }
procedure UpdateButtonsEnabled;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
const
Providers : array[0..3] of TMailProviderInfo =
(
( AuthenticationType : TIdSASLXOAuth;
AuthorizationEndpoint : 'https://accounts.google.com/o/oauth2/auth?access_type=offline';
AccessTokenEndpoint : 'https://accounts.google.com/o/oauth2/token';
LogoutEndpoint : 'https://www.google.com/accounts/Logout';
ClientID : google_clientid;
ClientSecret : google_clientsecret;
ClientAccount : google_clientAccount; // your @gmail.com email address
ClientName : clientname;
Scopes : 'https://mail.google.com/ openid';
SmtpHost : 'smtp.gmail.com';
SmtpPort : 465;
PopHost : 'pop.gmail.com';
PopPort : 995;
ImapHost : 'imap.gmail.com';
ImapPort : 143;
AuthName : 'Google';
TLS : utUseImplicitTLS;
TwoLinePOPFormat: False
),
( AuthenticationType : TIdSASLXOAuth;
AuthorizationEndpoint : 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';//'https://login.live.com/oauth20_authorize.srf';
AccessTokenEndpoint : 'https://login.microsoftonline.com/common/oauth2/v2.0/token';//'https://login.live.com/oauth20_token.srf';
LogoutEndpoint : 'https://login.microsoftonline.com/common/oauth2/v2.0/logout';
ClientID : microsoft_clientid;
ClientSecret : '';
ClientAccount : microsoftoffice_clientaccount; // your @live.com or @hotmail.com email address
ClientName : clientname;
Scopes : 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access';
//'wl.imap offline_access';
SmtpHost : 'smtp-mail.outlook.com';
SmtpPort : 587;
PopHost : 'smtp-mail.outlook.com';
PopPort : 995;
ImapHost : 'outlook.office365.com';
ImapPort : 993;
AuthName : 'Microsoft';
TLS : utUseExplicitTLS;
TwoLinePOPFormat: True
),
( AuthenticationType : TIdSASLXOAuth;
AuthorizationEndpoint : 'https://login.live.com/oauth20_authorize.srf';
AccessTokenEndpoint : 'https://login.live.com/oauth20_token.srf';
LogoutEndpoint : 'https://login.live.com/logout.srf';
ClientID : microsoft_clientid;
ClientSecret : '';
ClientAccount : microsoft_clientAccount; // your @live.com or @hotmail.com email address
// Scopes : 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access';
ClientName : clientname;
Scopes : 'wl.imap wl.emails wl.offline_access';
SmtpHost : 'smtp.office365.com';
SmtpPort : 587;
PopHost : 'outlook.office365.com';
PopPort : 995;
ImapHost : 'outlook.office365.com';
ImapPort : 993;
AuthName : 'Hotmail';
TLS : utUseExplicitTLS;
TwoLinePOPFormat: false
),
( AuthenticationType : TIdOAuth2Bearer;
AuthorizationEndpoint : 'https://api.login.yahoo.com/oauth2/request_auth';
AccessTokenEndpoint : 'https://api.login.yahoo.com/oauth2/get_token';
LogoutEndpoint : '';
ClientID : yahoo_clientid;
ClientSecret : yahoo_clintsecret;
ClientAccount : '[email protected]'; // your @live.com or @hotmail.com email address
// Scopes : 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access';
ClientName : clientname;
Scopes : 'email mail-r mail-w';
SmtpHost : 'smtp.mail.yahoo.com';
SmtpPort : 465;
PopHost : '';
PopPort : 995;
ImapHost : 'imap.mail.yahoo.com';
ImapPort : 993;
AuthName : 'Yahoo';
TLS : utUseExplicitTLS;
TwoLinePOPFormat: false
)
);
procedure TForm2.FormDestroy(Sender: TObject);
begin
FreeAndNil(EmailOAuthDataModule);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
EmailOAuthDataModule := TEmailOAuthDataModule.Create(nil);
EmailOAuthDataModule.OnLog := LogMsg;
EmailOAuthDataModule.HWNDHandle := Self.Handle;
EmailOAuthDataModule.AppHandle := Application.Handle;
EmailOAuthDataModule.Provider := Providers[rgEmailProviders.ItemIndex];
EmailOAuthDataModule.SetupAuthenticator;
UpdateButtonsEnabled;
end;
procedure TForm2.UpdateButtonsEnabled;
begin
btnAuthenticate.Enabled := not EmailOAuthDataModule.HasRefreshToken;
btnClearAuthToken.Enabled := EmailOAuthDataModule.HasRefreshToken;
end;
procedure TForm2.btnAuthenticateClick(Sender: TObject);
begin
EmailOAuthDataModule.Authenticate;
UpdateButtonsEnabled;
end;
procedure TForm2.btnCheckIMAPClick(Sender: TObject);
begin
EmailOAuthDataModule.CheckIMAP;
end;
procedure TForm2.btnCheckMsgClick(Sender: TObject);
begin
EmailOAuthDataModule.CheckPOP;
end;
procedure TForm2.btnClearAuthTokenClick(Sender: TObject);
begin
EmailOAuthDataModule.ClearAuthentication;
UpdateButtonsEnabled;
end;
procedure TForm2.btnSendMsgClick(Sender: TObject);
begin
EmailOAuthDataModule.SendMessage(clientsendtoaddress, '');
end;
procedure TForm2.LogMsg(const msg: string);
begin
Memo1.Lines.Add(msg);
end;
procedure TForm2.rgEmailProvidersClick(Sender: TObject);
begin
EmailOAuthDataModule.SelectedProvider := rgEmailProviders.ItemIndex;
EmailOAuthDataModule.Provider := Providers[rgEmailProviders.ItemIndex];
EmailOAuthDataModule.SetupAuthenticator;
UpdateButtonsEnabled;
end;
end.