-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUnit2.pas
More file actions
262 lines (237 loc) · 8.72 KB
/
Unit2.pas
File metadata and controls
262 lines (237 loc) · 8.72 KB
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
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
, Vcl.ComCtrls
, 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;
mmoLogging: TMemo;
btnSendViaREST: TButton;
PageControl1: TPageControl;
tsEmail: TTabSheet;
tsLogging: TTabSheet;
lblFrom: TLabel;
lblRecipientAddress: TLabel;
edtFromAddress: TEdit;
lblFromName: TLabel;
edtFromName: TEdit;
lblRecipientName: TLabel;
edtRecipientAddress: TEdit;
edtRecipientName: TEdit;
mmoBody: TMemo;
lblSubject: TLabel;
edtSubject: TEdit;
btnSendHTMLMsg: TButton;
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);
procedure btnSendViaRESTClick(Sender: TObject);
procedure btnSendHTMLMsgClick(Sender: TObject);
private
{ Private declarations }
EmailOAuthDataModule : TEmailOAuthDataModule;
procedure LogMsg(const msg: string);
public
{ Public declarations }
procedure UpdateButtonsEnabled;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
uses
TaurusTLS;
const
Providers : array[0..2] 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;
Scopes : 'https://mail.google.com/ openid email';
SmtpHost : 'smtp.gmail.com';
SmtpPort : 465;
PopHost : 'pop.gmail.com';
PopPort : 995;
ImapHost : 'imap.gmail.com';
ImapPort : 143;
AuthName : 'Google';
TLS : utUseImplicitTLS;
Version : TTaurusTLSSSLVersion.TLSv1_3;
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 : '';
Scopes : 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access openid email profile';
//'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;
Version : TTaurusTLSSSLVersion.TLSv1_2;
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 : '';
// Scopes : 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access';
Scopes : 'wl.imap wl.emails wl.offline_access openid email profile';
SmtpHost : 'smtp-mail.outlook.com';
SmtpPort : 587;
PopHost : 'outlook.office365.com';
PopPort : 995;
ImapHost : 'imap-mail.outlook.com';
ImapPort : 993;
AuthName : 'Hotmail';
TLS : utUseExplicitTLS;
Version : TTaurusTLSSSLVersion.TLSv1_2;
)
);
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;
edtFromAddress.Text := EmailOAuthDataModule.SendAddress;
edtFromName.Text := EmailOAuthDataModule.ReadString('FromName', '');
edtSubject.Text := EmailOAuthDataModule.ReadString('Subject', '');
edtRecipientAddress.Text := EmailOAuthDataModule.ReadString('RecipientAddress', '');
edtRecipientName.Text := EmailOAuthDataModule.ReadString('RecipientName', '');
UpdateButtonsEnabled;
end;
procedure TForm2.UpdateButtonsEnabled;
begin
btnAuthenticate.Enabled := not EmailOAuthDataModule.HasRefreshToken;
btnClearAuthToken.Enabled := EmailOAuthDataModule.HasRefreshToken;
btnSendViaREST.Enabled := rgEmailProviders.ItemIndex = 1;
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
if string(edtRecipientAddress.Text).IsEmpty then
begin
ShowMessage('Recipient Email Address Required');
Exit;
end;
EmailOAuthDataModule.SendMessage(edtFromAddress.Text, edtFromName.Text,
edtRecipientAddress.Text, edtRecipientName.Text,
edtSubject.Text, mmoBody.Text, '');
EmailOAuthDataModule.WriteString('FromName', edtFromName.Text);
EmailOAuthDataModule.WriteString('Subject', edtSubject.Text);
EmailOAuthDataModule.WriteString('RecipientAddress', edtRecipientAddress.Text);
EmailOAuthDataModule.WriteString('RecipientName', edtRecipientName.Text);
end;
procedure TForm2.btnSendViaRESTClick(Sender: TObject);
begin
EmailOAuthDataModule.SendEmailUsingREST(edtFromAddress.Text, edtFromName.Text,
edtRecipientAddress.Text, edtRecipientName.Text,
edtSubject.Text, mmoBody.Text, '');
EmailOAuthDataModule.WriteString('FromName', edtFromName.Text);
EmailOAuthDataModule.WriteString('Subject', edtSubject.Text);
EmailOAuthDataModule.WriteString('RecipientAddress', edtRecipientAddress.Text);
EmailOAuthDataModule.WriteString('RecipientName', edtRecipientName.Text);
end;
procedure TForm2.btnSendHTMLMsgClick(Sender: TObject);
var
msg, body : string;
InlineImagePaths: array of string;
attachFile: string;
begin
if string(edtRecipientAddress.Text).IsEmpty then
begin
ShowMessage('Recipient Email Address Required');
Exit;
end;
msg := '<html><img src="{img0}"><br>Hello</html>';
body := 'Body';
SetLength(InlineImagePaths, 1);
InlineImagePaths[0] := '..\..\Images\SMTPServiceLogos.png';
attachFile := '..\..\README.md';
EmailOAuthDataModule.SendEmailWithAttachment(edtFromAddress.Text, edtFromName.Text,
edtRecipientAddress.Text, edtRecipientName.Text,
edtSubject.Text, body, msg, InlineImagePaths, attachFile);
EmailOAuthDataModule.WriteString('FromName', edtFromName.Text);
EmailOAuthDataModule.WriteString('Subject', edtSubject.Text);
EmailOAuthDataModule.WriteString('RecipientAddress', edtRecipientAddress.Text);
EmailOAuthDataModule.WriteString('RecipientName', edtRecipientName.Text);
end;
procedure TForm2.LogMsg(const msg: string);
begin
mmoLogging.Lines.Add(msg);
end;
procedure TForm2.rgEmailProvidersClick(Sender: TObject);
begin
EmailOAuthDataModule.SelectedProvider := rgEmailProviders.ItemIndex;
EmailOAuthDataModule.Provider := Providers[rgEmailProviders.ItemIndex];
EmailOAuthDataModule.SetupAuthenticator;
edtFromAddress.Text := EmailOAuthDataModule.SendAddress;
UpdateButtonsEnabled;
end;
end.