You are on page 1of 2
s4ier2017 ritps Awww thoughto.comisensing-emall-messages-wih-atachments-10581247print ThoughtCo. Send Email Messages (and Attachments) Using Delphi & Indy Full Source Code For an Em: I Sender Application by Zarko Gajic Updated January 05, 2017 Below are instructions for creating an “email sender” that includes an option for sending email messages and attachments directly from a Delphi application. Before we begin, consider the alternative. ‘Suppose you have an application that operates on some database data, among other tasks. Users need to export data from your application and send the data through an email (like an error report). Without the approach outlined below, you have to export the data to an external file and then use an email client to send it. ‘SENDING EMAIL FROM DELPHI ‘There are many ways you can send an email directly from Delphi, but the simplest way is to use the ShellExecute API. This will send the email using the default email client installed on the computer. While this approach is acceptable, you're unable to send attachments this way. Another technique uses Microsoft Outlook and OLE to send the email, this time with attachment support, but MS. ‘Outlook is then required to be used, Yet another option is to use Delphi's builtin suppor for the Windows Simple Mail API. This works only ifthe user has @ MAPI-compliant email program installed, The technique we're discussing here uses Indy (Internet Direct) components - a great intemet component suite comprised of popular internet protocols written in Delphi and based on blocking sockets. ‘THE TIDSMTP (INDY) METHOD ‘Sending (or retrieving) email messages with Indy components (which ships with Delphi 6+) is as easy as dropping a ‘component or two on a form, setting some properties, and "clicking a button." To send an email with attachments from Delphi using Indy, we'll need two components. First, the TIASMTOP is used to connect and communicate (send mail) with an SMTP server. Second, the TidMessage handles the storing and encoding of the messages. When the message is constructed (when TldMessage is “illed” with data), the email is delivered to an SMTP server using the TidSMTP. EMAIL SENDER SOURCE CODE I've created a simple mail sender project that | explain below, You can download the full source code here, Note: That link is a direct download to the ZIP file for the project. You should be able to open it without any problems, but if you can't, use 7-Zip to open the archive so you can extract out the project files (which are stored in a folder called ‘SendMail) hps:/lwww thoughtca.comlsending-emai-massages-vilh-allachments-1058124?print 12 s4ier2017 nitps mw thoughto.comisensing-emall-messages-wih-atachments-1058124?print {As you can see from the designtime screenshot, to send an emaillusing the TIdSMTP component, you at least need to specify the SMTP mall server (host). The message itself needs the regular email parts filled out, like the From, To, Subject, etc. Here's the code that handles sending one email with an attachment: procedure TMailerForm.btnSendMallClick(Sender: TObject) ; begin StatusMemo.Clear; setup SMTP SMTPHost := ledHost.Text; SMTP Port = 25; setup mail message MailMessage.From.Address := ledFrom Text; MailMessage Recipients.EMailAddresses ladTo.Text + ' + led CC. Text; MailMessage Subject := ledSubject.Text; MailMessage Body Text := Body Text if FiloExists(/odAttachment.Text) then TidAttachment.Creato(MallMessage, MessageParts, ledAttachment.Text) ; Usend mail ty try SMTP.Connect(1000) ; SMTP-Send(MailMessage) ; except on E:Exception do StatusMemo.Lines.Insert(0, "ERROR: ' + E.Message) ; ond: finally if SMTP.Connected then SMTPDisconnect; end; end; (* binSendMail Click *) Note: Inside the source code, you'll find two extra procedures that are used to make the values of the Host, From, and To edit boxes persistent, using an INI fle for storage. hps:lwww thoughtca.comlsending-emai-massages-vilh-allachments-1058124?print

You might also like