If you have ever wanted to send email messages from your Delphi application, this article is for you. Jacques Noah explains how to use the open source Indy suite to set up an full fledged Mail Client.
Creating an Email Client with Borland Delphi - Coding (Page 3 of 4 )
Now that we've finished building the GUI we can start coding. Add "idreplysmtp" to your uses list, then double click on btMsg and add the following code:
procedure Tform1.btMsgClick(Sender: TObject); begin
//setup idSMTP connection parameters idSMTP.Host :=your host name; idSMTP.Port := 25; //smtp service usually runs on this port idSMTP.Password:=your password;
//check if receipt confirmation is required if checkbox1.checked then
//if required, set the sendback email address to your email address idmessage.ReceiptRecipient.Text:=edfrom.Text;
//send the message try try idSMTP.Connect; idSMTP.send(idmessage);
//if an exception occurs… except on E: EIdSMTPReplyError do begin
//…then show the message ShowMessage(E.Message); end; end; finally
//disconnect from server if IdSMTP.Connected then IdSMTP.Disconnect; end; end;
Let's go through this bit of code:
The first thing I've done is set up the server connection by providing the password and host name. The hostname is usually written this way: mail.xxx.com or smtp.xxxx.com.
Then I've continued to fill the idmessage component with the email headers, which in this case are: from, to, CC, BCC, subject and body. Please note that I also put a condition in place to check whether the user requires a delivery confirmation. This will enable you to check whether the message has actually been delivered to the recipient. I connected to the server and sent the message and then I closed the connection to the server. It is very important that you close the server connection, as you will get an error if you want to send another message. It is also good memory management. The "idreplysmtp" unit contains the definitions for all possible errors or exceptions that can be raised throughout this operation.
Now double click the browse button and add the following code:
procedure Tform1.btBrowseClick(Sender: TObject); begin if opendialog.Execute then TIdAttachmentFile.Create(idmessage.MessageParts, opendialog.FileName); AddAttachments; end;
The browse button calls up the opendialog and allows you to select a file to attach to your message. Then it goes on to call the AddAttachments procedure to add the filename(s) to the listview.
Add this procedure somewhere in the implementation section:
procedure Tform1.AddAttachments; var li: TListItem; idx: Integer; begin //clear the attachment listview lvAttachments.Items.Clear; //loop through Idmessage and count parts for idx := 0 to Pred(Idmessage.MessageParts.Count) do begin li := lvAttachments.Items.Add; // Check if Idmessage contains any attachments… if Idmessage.MessageParts.Items[idx] is TIdAttachmentFile then begin //if so, get the file names… li.Caption := TIdAttachmentFile (Idmessage.MessageParts.Items[idx]).Filename; //and add them to the listview li.SubItems.Add(TIdAttachmentFile (Idmessage.MessageParts.Items[idx]).ContentType); end else begin li.Caption := Idmessage.MessageParts.Items[idx].ContentType; end; end; end;
In short, the above procedure adds files to the listview component by looping through the idMessage component, checking whether it contains any attachments. If it does contain attachments, then it gets the filenames and lists them in the listview. This way you can see what files you've attached to your message. Make sure to add "procedure AddAttachments" in the public section of your form.
That's it! You have a fully functioning email application. All of our requirements have been met.