Sign in Join
Wiki copytemporary

Synapse - Email Examples: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Example 1 - sending to Gmail
22
3-(forum user **ludob**). This works for me with Gmail using [[Synapse]].
4-
5-``` pascal
3+(forum user **ludob**). This works for me with Gmail using [[Synapse]].
4+```
65uses
7- ..., smtpsend, ssl_openssl;
8-
9-function SendMail(const User, Password, MailFrom, MailTo, SMTPHost, SMTPPort, MailData: string): Boolean;
10-var
11- SMTP: TSMTPSend;
12- sl:TStringList;
13-begin
14- Result:=False;
15- SMTP:=TSMTPSend.Create;
16- sl:=TStringList.Create;
17- try
18- sl.text:=Maildata;
19- SMTP.UserName:=User;
20- SMTP.Password:=Password;
21- SMTP.TargetHost:=SMTPHost;
22- SMTP.TargetPort:=SMTPPort;
23- SMTP.AutoTLS:=true;
24- if SMTPPort<> '25' then
25- SMTP.FullSSL:=true;
26- if SMTP.Login then
6+ ..., smtpsend, ssl_openssl;
7+
8+ function SendMail(const User, Password, MailFrom, MailTo, SMTPHost, SMTPPort, MailData: string): Boolean;
9+ var
10+ SMTP: TSMTPSend;
11+ sl:TStringList;
2712 begin
28- Result:=SMTP.MailFrom(MailFrom, Length(MailData)) and
29- SMTP.MailTo(MailTo) and
30- SMTP.MailData(sl);
31- SMTP.Logout;
13+ Result:=False;
14+ SMTP:=TSMTPSend.Create;
15+ sl:=TStringList.Create;
16+ try
17+ sl.text:=Maildata;
18+ SMTP.UserName:=User;
19+ SMTP.Password:=Password;
20+ SMTP.TargetHost:=SMTPHost;
21+ SMTP.TargetPort:=SMTPPort;
22+ SMTP.AutoTLS:=true;
23+ if SMTPPort<> '25' then
24+ SMTP.FullSSL:=true;
25+ if SMTP.Login then
26+ begin
27+ Result:=SMTP.MailFrom(MailFrom, Length(MailData)) and
28+ SMTP.MailTo(MailTo) and
29+ SMTP.MailData(sl);
30+ SMTP.Logout;
31+ end;
32+ finally
33+ SMTP.Free;
34+ sl.Free;
35+ end;
3236 end;
33- finally
34- SMTP.Free;
35- sl.Free;
36- end;
37-end;
3837```
3938
4039## Example 2 - sending attachments
4140
42-(forum user **y.ivanov**). Here is my small helper unit for [[Synapse]]. The password is the sender password (for SMTP).
43-
44-``` pascal
41+(forum user **y.ivanov**). Here is my small helper unit for [[Synapse]]. The password is the sender password (for SMTP).
42+```
4543unit mailsendu;
46-
47-{$mode objfpc}{$H+}
48-
49-interface
50-
51-uses
52- Classes, SysUtils, smtpsend;
53-
54-type
55-
56- { TMySMTPSend }
57-
58- TMySMTPSend = class(TSMTPSend)
59- private
60- FSendSize: Integer;
61- protected
62- function SendToRaw(const AFrom, ATo: String; const AMailData: TStrings)
63- : Boolean;
64- public
65- function SendMessage(AFrom, ATo, ASubject: String; AContent,
44+
45+ {$mode objfpc}{$H+}
46+
47+ interface
48+
49+ uses
50+ Classes, SysUtils, smtpsend;
51+
52+ type
53+
54+ { TMySMTPSend }
55+
56+ TMySMTPSend = class(TSMTPSend)
57+ private
58+ FSendSize: Integer;
59+ protected
60+ function SendToRaw(const AFrom, ATo: String; const AMailData: TStrings)
61+ : Boolean;
62+ public
63+ function SendMessage(AFrom, ATo, ASubject: String; AContent,
64+ AAttachments: TStrings): Boolean;
65+
66+ property SendSize: Integer read FSendSize write FSendSize;
67+
68+ end;
69+
70+ implementation
71+
72+ uses
73+ ssl_openssl3, mimemess, mimepart, synautil, synachar;
74+
75+ { TMySMTPSend }
76+
77+ function TMySMTPSend.SendToRaw(const AFrom, ATo: String; const AMailData
78+ : TStrings): Boolean;
79+ var
80+ S, T: String;
81+ begin
82+ Result := False;
83+ if Self.Login then
84+ begin
85+ FSendSize := Length(AMailData.Text);
86+ if Self.MailFrom(GetEmailAddr(AFrom), FSendSize) then
87+ begin
88+ S := ATo;
89+ repeat
90+ T := GetEmailAddr(Trim(FetchEx(S, ',', '"')));
91+ if T <> '' then
92+ Result := Self.MailTo(T);
93+ if not Result then
94+ Break;
95+ until S = '';
96+ if Result then
97+ Result := Self.MailData(AMailData);
98+ end;
99+ Self.Logout;
100+ end;
101+ end;
102+
103+ function TMySMTPSend.SendMessage(AFrom, ATo, ASubject: String; AContent,
66104 AAttachments: TStrings): Boolean;
67-
68- property SendSize: Integer read FSendSize write FSendSize;
69-
70- end;
71-
72-implementation
73-
74-uses
75- ssl_openssl, mimemess, mimepart, synautil, synachar;
76-
77-{ TMySMTPSend }
78-
79-function TMySMTPSend.SendToRaw(const AFrom, ATo: String; const AMailData
80- : TStrings): Boolean;
81-var
82- S, T: String;
83-begin
84- Result := False;
85- if Self.Login then
86- begin
87- FSendSize := Length(AMailData.Text);
88- if Self.MailFrom(GetEmailAddr(AFrom), FSendSize) then
105+ var
106+ Mime: TMimeMess;
107+ P: TMimePart;
108+ I: Integer;
89109 begin
90- S := ATo;
91- repeat
92- T := GetEmailAddr(Trim(FetchEx(S, ',', '"')));
93- if T <> '' then
94- Result := Self.MailTo(T);
95- if not Result then
96- Break;
97- until S = '';
98- if Result then
99- Result := Self.MailData(AMailData);
110+ Mime := TMimeMess.Create;
111+ try
112+ // Set some headers
113+ Mime.Header.CharsetCode := UTF_8;
114+ Mime.Header.ToList.Text := ATo;
115+ Mime.Header.Subject := ASubject;
116+ Mime.Header.From := AFrom;
117+
118+ // Create a MultiPart part
119+ P := Mime.AddPartMultipart('mixed', Nil);
120+
121+ // Add as first part the mail text
122+ Mime.AddPartTextEx(AContent, P, UTF_8, True, ME_8BIT);
123+
124+ // Add all attachments:
125+ if Assigned(AAttachments) then
126+ for I := 0 to Pred(AAttachments.Count) do
127+ Mime.AddPartBinaryFromFile(AAttachments[I], P);
128+
129+ // Compose message
130+ Mime.EncodeMessage;
131+
132+ // Send using SendToRaw
133+ Result := Self.SendToRaw(AFrom, ATo, Mime.Lines);
134+
135+ finally
136+ Mime.Free;
137+ end;
100138 end;
101- Self.Logout;
102- end;
103-end;
104-
105-function TMySMTPSend.SendMessage(AFrom, ATo, ASubject: String; AContent,
106- AAttachments: TStrings): Boolean;
107-var
108- Mime: TMimeMess;
109- P: TMimePart;
110- I: Integer;
111-begin
112- Mime := TMimeMess.Create;
113- try
114- // Set some headers
115- Mime.Header.CharsetCode := UTF_8;
116- Mime.Header.ToList.Text := ATo;
117- Mime.Header.Subject := ASubject;
118- Mime.Header.From := AFrom;
119-
120- // Create a MultiPart part
121- P := Mime.AddPartMultipart('mixed', Nil);
122-
123- // Add as first part the mail text
124- Mime.AddPartTextEx(AContent, P, UTF_8, True, ME_8BIT);
125-
126- // Add all attachments:
127- if Assigned(AAttachments) then
128- for I := 0 to Pred(AAttachments.Count) do
129- Mime.AddPartBinaryFromFile(AAttachments[I], P);
130-
131- // Compose message
132- Mime.EncodeMessage;
133-
134- // Send using SendToRaw
135- Result := Self.SendToRaw(AFrom, ATo, Mime.Lines);
136-
137- finally
138- Mime.Free;
139- end;
140-end;
141-
142-end.
139+
140+ end.
143141```
144142
145-This is how to use that unit:
146-
147-``` pascal
143+This is how to use that unit:
144+```
148145program mailsend_test;
149-
150-uses
151- Classes, SysUtils, mailsendu, blcksock;
152-
153-type
154-
155- { TSink }
156-
157- TSink = class(TObject)
158- procedure Progress(Sender: TObject; Reason: THookSocketReason;const Value: String);
159- end;
160-
161-var
162- Content, Attach: TStringList;
163- SMTP: TMySMTPSend;
164- Sink: TSink;
165- Written: Integer;
166-
167-{ TSink }
168-
169-procedure TSink.Progress(Sender: TObject; Reason: THookSocketReason;
170- const Value: String);
171-begin
172- case Reason of
173- {:Socket connected to IP and Port. Connected IP and Port is in parameter in
174- format like: 'localhost.somewhere.com:25'.}
175- HR_Connect: Written := 0;
176- {:report count of bytes writed to socket. Number is in parameter string. If
177- you need is in integer, you must use StrToInt function!}
178- HR_WriteCount:
179- begin
180- Written := Written + StrToInt(Value);
181- WriteLn('Written ', Written, ' of ', SMTP.SendSize, ' bytes');
146+
147+ uses
148+ Classes, SysUtils, mailsendu, blcksock;
149+
150+ type
151+
152+ { TSink }
153+
154+ TSink = class(TObject)
155+ procedure Progress(Sender: TObject; Reason: THookSocketReason;const Value: String);
182156 end;
183- {:report situation where communication error occured. When raiseexcept is
184- @true, then exception is called after this Hook reason.}
185- HR_Error: WriteLn('Error: ', Value);
186- end;
187-end;
188-
189-begin
190- Sink := TSink.Create;
191- Content := TStringList.Create;
192- Content.Add('Hello!');
193- Content.Add('This is a SMTP send test.');
194- Content.Add('Hello from the other side!');
195- Content.Add('Regards,');
196- Attach := TStringList.Create;
197- Attach.Add('mismisc.pas');
198- Attach.Add('notused.pas');
199-
200- SMTP := TMySMTPSend.Create;
201- try
202- SMTP.TargetHost := 'smtp.googlemail.com';
203- SMTP.TargetPort := '465';
204- SMTP.Username := 'user@gmail.com';
205- SMTP.Password := 'password here';
206- SMTP.FullSSL := True;
207- SMTP.Sock.OnStatus := @Sink.Progress;
208- SMTP.Sock.RaiseExcept := True;
209- try
210- if SMTP.SendMessage(
211- 'user@gmail.com', // AFrom
212- 'recipient@domain.com', // ATo
213- 'Test subject FullSSL 2', // ASubject
214- Content,
215- Attach)
216- then
217- WriteLn('Success.')
218- else
219- begin
220- WriteLn('Failure!');
157+
158+ var
159+ Content: TStringList = Nil;
160+ Attach: TStringList = Nil;
161+ SMTP: TMySMTPSend = Nil;
162+ Sink: TSink = Nil;
163+ Written: Integer;
164+
165+ { TSink }
166+
167+ procedure TSink.Progress(Sender: TObject; Reason: THookSocketReason;
168+ const Value: String);
169+ begin
170+ case Reason of
171+ {:Socket connected to IP and Port. Connected IP and Port is in parameter in
172+ format like: 'localhost.somewhere.com:25'.}
173+ HR_Connect: Written := 0;
174+ {:report count of bytes writed to socket. Number is in parameter string. If
175+ you need is in integer, you must use StrToInt function!}
176+ HR_WriteCount:
177+ begin
178+ Written := Written + StrToInt(Value);
179+ WriteLn('Written ', Written, ' of ', SMTP.SendSize, ' bytes');
180+ end;
181+ {:report situation where communication error occured. When raiseexcept is
182+ @true, then exception is called after this Hook reason.}
183+ HR_Error: WriteLn('Error: ', Value);
221184 end;
222- except
223- on E: Exception do
224- WriteLn('EXCEPTION: ', E.Message);
225185 end;
226-
227- with SMTP do
186+
228187 begin
229- WriteLn;
230- WriteLn(' ResultCode: ', ResultCode);
231- WriteLn('ResultString: ', ResultString);
232- WriteLn(' FullResult: ', FullResult.Text);
233- WriteLn(' AuthDone: ', AuthDone) ;
234- end;
235- finally
236- SMTP.Free;
237- end;
238-
239-end.
188+ try
189+ Sink := TSink.Create;
190+ Content := TStringList.Create;
191+ Content.Add('Hello!');
192+ Content.Add('This is a SMTP send test.');
193+ Content.Add('Hello from the other side!');
194+ Content.Add('Regards,');
195+ Attach := TStringList.Create;
196+ Attach.Add('mismisc.pas');
197+ Attach.Add('notused.pas');
198+ SMTP := TMySMTPSend.Create;
199+ SMTP.TargetHost := 'smtp.googlemail.com';
200+ SMTP.TargetPort := '465';
201+ SMTP.Username := 'user@gmail.com';
202+ SMTP.Password := 'password here';
203+ SMTP.FullSSL := True;
204+ SMTP.Sock.OnStatus := @Sink.Progress;
205+ SMTP.Sock.RaiseExcept := True;
206+ try
207+ if SMTP.SendMessage(
208+ 'user@gmail.com', // AFrom
209+ 'recipient@domain.com', // ATo
210+ 'Test subject FullSSL 2', // ASubject
211+ Content,
212+ Attach)
213+ then
214+ WriteLn('Success.')
215+ else
216+ begin
217+ WriteLn('Failure!');
218+ end;
219+ except
220+ on E: Exception do
221+ WriteLn('EXCEPTION: ', E.Message);
222+ end;
223+
224+ with SMTP do
225+ begin
226+ WriteLn;
227+ WriteLn(' ResultCode: ', ResultCode);
228+ WriteLn('ResultString: ', ResultString);
229+ WriteLn(' FullResult: ', FullResult.Text);
230+ WriteLn(' AuthDone: ', AuthDone) ;
231+ end;
232+ finally
233+ FreeAndNil(Content);
234+ FreeAndNil(Attach);
235+ FreeAndNil(Sink);
236+ FreeAndNil(SMTP);
237+ end;
238+
239+ end.
240240```
241241
242242## Example 3 - using the XMailer wrapper
243243
244-(forum user **Silvio Clécio**). I use [XMailer plugin](https://github.com/MFernstrom/xmailer) (it uses Synapse framework). Works fine with Gmail, Hotmail, Yahoo etc.
244+(forum user **Silvio Clécio**). I use [XMailer plugin](<https://github.com/pascal-libs/xmailer>) (it uses Synapse framework). Works fine with Gmail, Hotmail, Yahoo etc.
245245
246-It is also available in the [[Online Package Manager]].
246+It is also available in the [[Online Package Manager]].
247247
248248## Example 4 - with error checking for debugging
249249
250-This version is "advanced" in so far as it does a lot of error checking and shows you the responses from the remote mail server. It is very useful for debugging especially when you do not have access to the remote mail server's logs.
251-
252-``` pascal
250+This version is "advanced" in so far as it does a lot of error checking and shows you the responses from the remote mail server. It is very useful for debugging especially when you do not have access to the remote mail server's logs.
251+```
253252unit Unit1;
254-
255-{$mode objfpc}{$H+}
256-
257-interface
258-
259-uses
260- Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
261-
262-type
263-
264- { TForm1 }
265-
266- TForm1 = class(TForm)
267- Button1: TButton;
268- Memo1: TMemo;
269- procedure Button1Click(Sender: TObject);
270- private
271- public
272- end;
273-
274-var
275- Form1: TForm1;
276-
277-implementation
278-
279-{$R *.lfm}
280-uses
281- // "ssl_openssl" unit is required to use SSL / TLS
282- ssl_openssl, SMTPsend;
283-
284-// MailData is the text of the mail.
285-procedure SendMail(User, Password, MailFrom, MailTo, SMTPHost, SMTPPort: string; MailData: string);
286-var
287- SMTP: TSMTPSend;
288- email_lines: TStringList;
289-begin
290- SMTP := TSMTPSend.Create;
291- email_lines := TStringList.Create;
292-
293- try
294- email_lines.text := Maildata;
295- SMTP.UserName := User;
296- SMTP.Password := Password;
297- SMTP.TargetHost := SMTPHost;
298- SMTP.TargetPort := SMTPPort;
299- //SMTP.AutoTLS := True; // upgrade to SSL/TLS if remote server supports it
300-
301- if Trim(SMTPPort) <> '25' then
302- SMTP.FullSSL := true; // if sending to port 25, don't use encryption
303-
304- Form1.Memo1.Clear; // clear memo text
305-
306- if not SMTP.Login() then
307- begin
308- Form1.Memo1.Append('SMTP ERROR: Login:' + SMTP.EnhCodeString);
309- Form1.Memo1.Append('SMTP Login: Failed - does server exist? does it accept mail?');
310- Exit
311- end
312- else
313- begin
314- Form1.Memo1.Append('SMTP Login: OK');
315- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
316- end;
317-
318- if(SMTP.ESMTP) then
319- Form1.Memo1.Append('SMTP: ' + SMTPHost + ' supports ESMTP')
320- else
321- Form1.Memo1.Append('SMTP: ' + SMTPHost + ' supports plain SMTP');
322-
323- // Insist on SSL/TLS connection to remote server
324- // - if the server might not support it, omit and then
325- // uncomment SMTP.AutoTLS above
326- if not SMTP.StartTLS() then
327- Form1.Memo1.Append('SMTP ERROR: StartTLS:' + SMTP.EnhCodeString)
328- else
329- begin
330- Form1.Memo1.Append('SMTP StartTLS: OK');
331- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
332- end;
333-
334- if not SMTP.MailFrom(MailFrom, Length(MailFrom)) then
335- Form1.Memo1.Append('SMTP ERROR: MailFrom:' + SMTP.EnhCodeString)
336- else
337- begin
338- Form1.Memo1.Append('SMTP MailFrom: OK');
339- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
340- end;
341-
342- if not SMTP.MailTo(MailTo) then
343- Form1.Memo1.Append('SMTP ERROR: MailTo:' + SMTP.EnhCodeString)
344- else
345- begin
346- Form1.Memo1.Append('SMTP MailTo: OK');
347- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
348- end;
349-
350- if not SMTP.MailData(email_lines) then
351- Form1.Memo1.Append('SMTP ERROR: MailData:' + SMTP.EnhCodeString)
352- else
353- begin
354- Form1.Memo1.Append('SMTP MailData: OK');
355- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
253+
254+ {$mode objfpc}{$H+}
255+
256+ interface
257+
258+ uses
259+ Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
260+
261+ type
262+
263+ { TForm1 }
264+
265+ TForm1 = class(TForm)
266+ Button1: TButton;
267+ Memo1: TMemo;
268+ procedure Button1Click(Sender: TObject);
269+ private
270+ public
356271 end;
357-
358- if not SMTP.Logout() then
359- Form1.Memo1.Append('SMTP ERROR: Logout:' + SMTP.EnhCodeString)
360- else
361- begin
362- Form1.Memo1.Append('SMTP Logout: OK');
363- Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
272+
273+ var
274+ Form1: TForm1;
275+
276+ implementation
277+
278+ {$R *.lfm}
279+ uses
280+ // "ssl_openssl" unit is required to use SSL / TLS
281+ ssl_openssl, SMTPsend;
282+
283+ // MailData is the text of the mail.
284+ procedure SendMail(User, Password, MailFrom, MailTo, SMTPHost, SMTPPort: string; MailData: string);
285+ var
286+ SMTP: TSMTPSend;
287+ email_lines: TStringList;
288+ begin
289+ SMTP := TSMTPSend.Create;
290+ email_lines := TStringList.Create;
291+
292+ try
293+ email_lines.text := Maildata;
294+ SMTP.UserName := User;
295+ SMTP.Password := Password;
296+ SMTP.TargetHost := SMTPHost;
297+ SMTP.TargetPort := SMTPPort;
298+ //SMTP.AutoTLS := True; // upgrade to SSL/TLS if remote server supports it
299+
300+ if Trim(SMTPPort) <> '25' then
301+ SMTP.FullSSL := true; // if sending to port 25, don't use encryption
302+
303+ Form1.Memo1.Clear; // clear memo text
304+
305+ if not SMTP.Login() then
306+ begin
307+ Form1.Memo1.Append('SMTP ERROR: Login:' + SMTP.EnhCodeString);
308+ Form1.Memo1.Append('SMTP Login: Failed - does server exist? does it accept mail?');
309+ Exit
310+ end
311+ else
312+ begin
313+ Form1.Memo1.Append('SMTP Login: OK');
314+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
315+ end;
316+
317+ if(SMTP.ESMTP) then
318+ Form1.Memo1.Append('SMTP: ' + SMTPHost + ' supports ESMTP')
319+ else
320+ Form1.Memo1.Append('SMTP: ' + SMTPHost + ' supports plain SMTP');
321+
322+ // Insist on SSL/TLS connection to remote server
323+ // - if the server might not support it, omit and then
324+ // uncomment SMTP.AutoTLS above
325+ if not SMTP.StartTLS() then
326+ Form1.Memo1.Append('SMTP ERROR: StartTLS:' + SMTP.EnhCodeString)
327+ else
328+ begin
329+ Form1.Memo1.Append('SMTP StartTLS: OK');
330+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
331+ end;
332+
333+ if not SMTP.MailFrom(MailFrom, Length(MailFrom)) then
334+ Form1.Memo1.Append('SMTP ERROR: MailFrom:' + SMTP.EnhCodeString)
335+ else
336+ begin
337+ Form1.Memo1.Append('SMTP MailFrom: OK');
338+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
339+ end;
340+
341+ if not SMTP.MailTo(MailTo) then
342+ Form1.Memo1.Append('SMTP ERROR: MailTo:' + SMTP.EnhCodeString)
343+ else
344+ begin
345+ Form1.Memo1.Append('SMTP MailTo: OK');
346+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
347+ end;
348+
349+ if not SMTP.MailData(email_lines) then
350+ Form1.Memo1.Append('SMTP ERROR: MailData:' + SMTP.EnhCodeString)
351+ else
352+ begin
353+ Form1.Memo1.Append('SMTP MailData: OK');
354+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
355+ end;
356+
357+ if not SMTP.Logout() then
358+ Form1.Memo1.Append('SMTP ERROR: Logout:' + SMTP.EnhCodeString)
359+ else
360+ begin
361+ Form1.Memo1.Append('SMTP Logout: OK');
362+ Form1.Memo1.append('-- Remote response: ' + SMTP.FullResult[0]);
363+ end;
364+
365+ finally
366+ SMTP.Free;
367+ email_lines.Free;
364368 end;
365-
366- finally
367- SMTP.Free;
368- email_lines.Free;
369- end;
370-end;
371-
372-procedure TForm1.Button1Click(Sender: TObject);
373-begin
374- SendMail(
375- '', // Name if authentication required
376- '', // Password if authentication required
377- 'trev@example.com', // MailFrom
378- 'trev@example.org', // MailTo
379- 'shadow.example.org', // Mail server to send to
380- '25', // Mail server port
381- 'To: trev@example.org' // Otherwise To: is blank on receipt
382- + LineEnding + 'Subject: Test' // Otherwise Subject: is blank on receipt
383- + LineEnding +'Gday!' // Email body
384- + LineEnding);
385-end;
386-
387-end.
369+ end;
370+
371+ procedure TForm1.Button1Click(Sender: TObject);
372+ begin
373+ SendMail(
374+ '', // Name if authentication required
375+ '', // Password if authentication required
376+ 'trev@example.com', // MailFrom
377+ 'trev@example.org', // MailTo
378+ 'shadow.example.org', // Mail server to send to
379+ '25', // Mail server port
380+ 'To: trev@example.org' // Otherwise To: is blank on receipt
381+ + LineEnding + 'Subject: Test' // Otherwise Subject: is blank on receipt
382+ + LineEnding +'Gday!' // Email body
383+ + LineEnding);
384+ end;
385+
386+ end.
388387```
389388
390389**Example Memo1 output on success**
391-
392-```text
390+```
393391SMTP Login: OK
394--- Remote response: 250-shadow.example.org Hello macmini8.example.org [192.168.1.21], pleased to meet you
395-SMTP: shadow.example.org supports ESMTP
396-SMTP StartTLS: OK
397--- Remote response: 220 2.0.0 Ready to start TLS
398-SMTP MailFrom: OK
399--- Remote response: 250 2.1.0 <trev@example.com>... Sender ok
400-SMTP MailTo: OK
401--- Remote response: 250 2.1.5 <trev@example.org>... Recipient ok
402-SMTP MailData: OK
403--- Remote response: 250 2.0.0 22G97AqL046395 Message accepted for delivery
404-SMTP Logout: OK
405--- Remote response: 221 2.0.0 shadow.example.org closing connection
392+ -- Remote response: 250-shadow.example.org Hello macmini8.example.org [192.168.1.21], pleased to meet you
393+ SMTP: shadow.example.org supports ESMTP
394+ SMTP StartTLS: OK
395+ -- Remote response: 220 2.0.0 Ready to start TLS
396+ SMTP MailFrom: OK
397+ -- Remote response: 250 2.1.0 <trev@example.com>... Sender ok
398+ SMTP MailTo: OK
399+ -- Remote response: 250 2.1.5 <trev@example.org>... Recipient ok
400+ SMTP MailData: OK
401+ -- Remote response: 250 2.0.0 22G97AqL046395 Message accepted for delivery
402+ SMTP Logout: OK
403+ -- Remote response: 221 2.0.0 shadow.example.org closing connection
406404```
407405
408406**Example Memo1 output on failure**
409-
410-```text
407+```
411408SMTP Login: OK
412--- Remote response: 250-shadow.example.org Hello 103-216-191-138.dyn.launtel.net.au [103.216.191.138], pleased to meet you
413-SMTP: shadow.example.org supports ESMTP
414-SMTP StartTLS: OK
415--- Remote response: 220 2.0.0 Ready to start TLS
416-SMTP MailFrom: OK
417--- Remote response: 250 2.1.0 <trev@example.com>... Sender ok
418-SMTP ERROR: MailTo:Persistent Transient Failure-Delivery not authorized, message refused
419--- Remote response: 451 4.7.1 Spam alert: 103-116-191-138.dyn.launtel.net.au [103.116.191.138] mail delivery delayed
420-SMTP ERROR: MailData:Permanent Failure-Other undefined Status
421--- Remote response: 503 5.0.0 Need RCPT (recipient)
422-SMTP Logout: OK
423--- Remote response: 221 2.0.0 shadow.example.org closing connection
409+ -- Remote response: 250-shadow.example.org Hello 103-216-191-138.dyn.launtel.net.au [103.216.191.138], pleased to meet you
410+ SMTP: shadow.example.org supports ESMTP
411+ SMTP StartTLS: OK
412+ -- Remote response: 220 2.0.0 Ready to start TLS
413+ SMTP MailFrom: OK
414+ -- Remote response: 250 2.1.0 <trev@example.com>... Sender ok
415+ SMTP ERROR: [MailTo:Persistent](<MailTo:Persistent>) Transient Failure-Delivery not authorized, message refused
416+ -- Remote response: 451 4.7.1 Spam alert: 103-116-191-138.dyn.launtel.net.au [103.116.191.138] mail delivery delayed
417+ SMTP ERROR: MailData:Permanent Failure-Other undefined Status
418+ -- Remote response: 503 5.0.0 Need RCPT (recipient)
419+ SMTP Logout: OK
420+ -- Remote response: 221 2.0.0 shadow.example.org closing connection
424421```
425422
426423## See also
427424
428-- [RFC2821](https://www.ietf.org/rfc/rfc2821.txt) - Simple Mail Transfer Protocol (SMTP).
425+ * [RFC2821](<https://www.ietf.org/rfc/rfc2821.txt>) \- Simple Mail Transfer Protocol (SMTP).
429426
430-- [RFC3207](https://www.ietf.org/rfc/rfc3207.txt) - SMTP Service Extension for Secure SMTP over Transport Layer Security (TLS).
427+ * [RFC3207](<https://www.ietf.org/rfc/rfc3207.txt>) \- SMTP Service Extension for Secure SMTP over Transport Layer Security (TLS).
431428
432-- [RFC5246](https://www.ietf.org/rfc/rfc5246.txt) - The Transport Layer Security (TLS) Protocol Version 1.2.
429+ * [RFC5246](<https://www.ietf.org/rfc/rfc5246.txt>) \- The Transport Layer Security (TLS) Protocol Version 1.2.
433430
434-- [RFC8446](https://www.ietf.org/rfc/rfc8446.txt) - The Transport Layer Security (TLS) Protocol Version 1.3.
431+ * [RFC8446](<https://www.ietf.org/rfc/rfc8446.txt>) \- The Transport Layer Security (TLS) Protocol Version 1.3.
435432
436-- [[Synapse]]
433+ * [[Synapse]]
437434
438-- [Synapse documentation](https://synapse.ararat.cz/doku.php/public:howto:tmimepart) on sending attachments.
435+ * [Synapse documentation](<https://synapse.ararat.cz/doku.php/public:howto:tmimepart>) on sending attachments.
439436
440-- [Article (PDF)](https://www.freepascal.org/~michael/articles/lazmail/lazmail-en.pdf) that covers sending email, including attachments, using Synapse.
437+ * [Article (PDF)](<https://www.freepascal.org/~michael/articles/lazmail/lazmail-en.pdf>) that covers sending email, including attachments, using Synapse.
441438
442-- [[lNet examples]] - email example using [[lNet]] library.
439+ * [[lNet examples]] \- email example using [[lNet]] library.
443440
444441---
445-*From the Free Pascal wiki, page "Synapse - Email Examples", as it stood on 2022-05-19 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
446-Category: Example programs
447-Category: Networking
442+*From the Free Pascal wiki, page "Synapse - Email Examples", as the Internet Archive kept it on 2026-02-04, last changed on 2025-08-15. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history