To recreate this example, you will need to create a new blank Delphi application.
Place 5 TMemos, 8 TEdits, a TListBox, 3 TButtons, a TCheckBox, a TNMSMTP, a TRadioGroup, and a TOpenDialog on the form.
If you wish to label the controls, they do the following:
Edit1: Host property
Edit2: User ID property
Edit3: PostMessage.Date property
Edit4: PostMessage.FromAddress property
Edit5: PostMessage.FromName property
Edit6: PostMessage.LocalProgram property
Edit7: PostMessage.ReplyTo property
Edit8: PostMessage.Subject property
Memo1: PostMessage.ToAddress property.
Memo2: PostMessage.ToBlindCarbonCopy property
Memo3: PostMessage.ToCarbonCopy property
Memo4: PostMessage.Body property
Memo5: Status window
ListBox1: PostMessage.Attachments property
Button1: Connect/Disconnect button
Button2: SendMail button
Button3: Clears edit fields and parameters
CheckBox1: sets value of ClearParams property
OpenDialog1: Adds files to attach to the E-Mail
RadioGroup1: Specifies the file encode method**
**Add 2 items to RadioGroup1's Items property: MIME and UUEncode
(In that order)
Insert the following code into Button1's OnClick event:
procedure TForm1.Button1Click(Sender: TObject);
begin
if NMSMTP1.Connected then
NMSMTP1.Disconnect
else
begin
NMSMTP1.Host := Edit1.Text;
NMSMTP1.UserID := Edit2.Text;
NMSMTP1.Connect;
end;
end;
When Button1 is clicked, if NMSMTP1 is connected as specified by the Connected property, the Disconnect method is called. If there is no connection present, the Host property is set to the value in Edit1, the UserID property is set to the value of Edit2, and the Connect method is called to connect to the SMTP host.
Insert the following code into Button2's OnClick event:
procedure TForm1.Button2Click(Sender: TObject);
begin
if NMSMTP1.Connected then
begin
NMSMTP1.ClearParams := CheckBox1.Checked;
NMSMTP1.SubType := mtPlain;
case RadioGroup1.ItemIndex of
0: NMSMTP1.EncodeType := uuMime;
1: NMSMTP1.EncodeType := uuCode;
end;
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit5.Text;
NMSMTP1.PostMessage.ToAddress.Text := Memo1.Text;
NMSMTP1.PostMessage.ToCarbonCopy.Text := Memo3.Text;
NMSMTP1.PostMessage.ToBlindCarbonCopy.Text := Memo2.Text;
NMSMTP1.PostMessage.Body.Text := Memo4.Text;
NMSMTP1.PostMessage.Attachments.Text := ListBox1.Items.Text;
NMSMTP1.PostMessage.Subject := Edit8.Text;
NMSMTP1.PostMessage.LocalProgram := Edit6.Text;
NMSMTP1.PostMessage.Date := Edit3.Text;
NMSMTP1.PostMessage.ReplyTo := Edit7.Text;
NMSMTP1.SendMail;
end
else
ShowMessage('You need to connect before you can send your message');
end;
When Button2 is clicked, if NMSMTP1 is connected, the ClearParams property is set to the value of CheckBox1.Checked, to determine whether the parameters of PostMessage will be cleared after a successful SendMail or not. The SubType property is set to mtPlain, signifying that the message being sent is plain ASCII text with no special formatting. The EncodeType property is set to either uuMime or uuCode, depending on which item in RadioGroup1 is selected. The PostMessage property contains sub-properties that define an E-Mail message. The FromAddress sub-property is set to the E-Mail address entered in Edit4. The
FromName sub-property is set to the name entered in Edit5. The ToAddress sub-property is a TStringList, to allow multiple recipients of a message, so it's Text is set to the value of Memo1's Text property. The ToCarbonCopy and ToBlindCarbonCopy sub-properties are also TStringLists. The ToCarbonCopy property is set to the addresses entered in Memo3.Text, and the ToBlindCarbonCopy property is set to the addresses entered in Memo2.Text. The Body sub-property of PostMessage contains the body of the E-Mail, and is set to the value of Memo4.Text. The list of files that are in ListBox1 are set to the Attachments sub-property. The Subject sub-property is set to the value entered into Edit8. The LocalProgram sub-property is set to the value entered in Edit6. The Date sub-property is set to the value entered in Edit3. The date entered will be stored in the header, even if the date is not a date, simply text. The ReplyTo sub-property is set to the value of Edit7. Finally, the message is sent with the SendMail method.
If there is no connection present, a message box is displayed informing the user that a connection is required to send a message.
Insert the following code into Button3's OnClick event:
procedure TForm1.Button3Click(Sender: TObject);
begin
NMSMTP1.ClearParameters;
Edit3.Clear;
Edit4.Clear;
Edit5.Clear;
Edit6.Clear;
Edit7.Clear;
Edit8.Clear;
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
Memo4.Clear;
Memo5.Clear;
ListBox1.Clear;
end;
When Button3 is clicked, the ClearParameters method clears the parameters of the PostMessage property, and also clears the contents of the input fields on the form to receive a new mail message.
Insert the following code into ListBox1's OnKeyDown event:
procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_INSERT then
if OpenDialog1.Execute then
ListBox1.Items.Add(OpenDialog1.FileName);
if Key = VK_DELETE then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
If the Insert key is pressed while ListBox1 has focus, OpenDialog1 is displayed so the user can pick a file. If the user clicks the OK button in the Open Dialog, the filename is added to ListBox1's Items.
If the Delete key is pressed while ListBox1 has focus, the currently selected filename is removed from the list.
Insert the following code into NMSMTP1's OnAttachmentNotFound event:
procedure TForm1.NMSMTP1AttachmentNotFound(Filename: String);
begin
Memo5.Lines.Add('File attachment '+FileName+' not found');
end;
If one of the files specified in ListBox1 does not exist when the Button2 is clicked, the OnAttachmentNotFound event is called. In this case, the status window, Memo5, is updated to inform the user that a specified attachment was not found, and which file it was that was not found.
Insert the following code into NMSMTP1's OnAuthenticationFailed event:
procedure TForm1.NMSMTP1AuthenticationFailed(var Handled: Boolean);
var
S: String;
begin
S := NMSMTP1.UserID;
if InputQuery('Authentication Failed', 'Invalid User ID. New User ID: ', S) then
begin
NMSMTP1.UserID := S;
Handled := TRUE;
end;
end;
If the User ID specified by the UserID property is invalid, or the UserID property is blank and a User ID is required, the OnAuthenticationFailed event is called. In this case, the InputQuery function is used to give the user the opportunity to recitfy the error. If the user enters a new User ID, the Handled property is set to true, and authentication is attempted again. If the user just clicks the cancel button, the UserID property is not reset, and an exception is raised.
Insert the following code into NMSMTP1's OnConnect event:
procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
Memo5.Lines.Add('Connected');
end;
When a connection is established with the SMTP host, the OnConnect event notifies the user of the connect by adding a line that reads Connected to Memo5.
Insert the following code into NMSMTP1's OnSendStart event:
procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
Memo5.Lines.Add('Sending Message');
end;
When a message is about to be sent, the OnSendStart event is called. In this instance, Memo5 is updated to inform the user that the message is being sent.
Insert the following code into NMSMTP1's OnEncodeStart event:
procedure TForm1.NMSMTP1EncodeStart(Filename: String);
begin
Memo5.Lines.Add('Encoding '+FileName);
end;
If a message has file attachments, when they begin encoding, the OnEncodeStart event is called. In this instance, Memo5 displays the name of the file being encoded.
Insert the following code into NMSMTP1's OnEncodeEnd event:
procedure TForm1.NMSMTP1EncodeEnd(Filename: String);
begin
Memo5.Lines.Add(FileName+' encoded');
end;
If a message has file attachments, when they complete encoding, the OnEncodeEnd event is called. In this instance, Memo5 displays the name of the file that has finished being encoded.
Insert the following code into NMSMTP1's OnFailure event:
procedure TForm1.NMSMTP1Failure(Sender: TObject);
begin
Memo5.Lines.Add('Message delivery failure');
end;
If an outgoing message fails to be sent, the OnFailure event is called. In this instance, Memo5 updates to inform the user of the failure.
Insert the following code into NMSMTP1's OnSuccess event:
procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
Memo5.Lines.Add('Message sent successfully');
end;
When an outgoing message has been sent successfully, the OnSuccess event is called. In this example, Memo5 is updated to inform the user that the message was sent successfully.
Insert the following code into NMSMTP1's OnHeaderIncomplete event:
procedure TForm1.NMSMTP1HeaderIncomplete(var handled: Boolean; hiType: Integer);
var
S: String;
begin
case hiType of
hiFromAddress:
if InputQuery('Missing From Address', 'Enter From Address: ', S) then
begin
NMSMTP1.PostMessage.FromAddress := S;
Handled := TRUE;
end;
hiToAddress:
if InputQuery('Missing To Address', 'Enter To Address: ', S) then
begin
NMSMTP1.PostMessage.ToAddress.Text := S;
Handled := TRUE;
end;
end;
end;
If the PostMessage property is missing information that is critical to the outgoing message being sent successfully, the OnHeaderIncomplete event is called. In this example, the hiType parameter is checked, and the user is given the opportunity to fill in the missing information. If the user fills in the information that is missing, the Handled parameter is set to TRUE, and the message continues to be sent. If the user clicks the cancel button instead of entering the missing information, an exception gets raised.
Insert the following code into NMSMTP1's OnRecipientNotFound event:
procedure TForm1.NMSMTP1RecipientNotFound(Recipient: String);
begin
Memo5.Lines.Add('Recipient '+Recipient+' not found');
end;
If one of the recipients of the outgoing message in either the ToAddress, ToBlindCarbonCopy, or ToCarbonCopy fields are known by the SMTP host to not exist, the OnRecipientNotFound event is called. In this example, Memo5 is updated to inform the user which recipient could not be found. If only one recipient is specified, an exception is raised because no valid recipients were found for the outgoing message.
Example Description:
When this application is run, enter the requested information into the Edit boxes and Memos. For the ToAddress, ToCarbonCopy, and TBlindCarbonCopy fields, multiple addresses may be entered, but they must each be on a separate line (carriage return/line feeds between them). Click Button1 to connect and Button2 to send the message. Button3 is used to clear the input fields so a new message may be entered. Clicking Button1 a second time will disconnect from the SMTP host.