Thursday, March 12, 2020
Sending Emails With Attachments Using Delphi Indy
Sending Emails With Attachments Using Delphi Indy 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, youre 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 Delphis built-in support for the Windows Simple Mail API. This works only if the userà has a MAPI-compliant email program installed. The technique were discussing here uses Indyà (Internet Direct) components - a great internet 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, well need two components. First, the TIdSMTOP 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 TIdMessageà is filled with data), the email is delivered to an SMTP server using the TIdSMTP. Email Sender Source Code Ive created a simple mail sender project that I 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 cant, use 7-Zip to open the archive so you can extract out the project files (which are stored in a folder called SendMail). As you can see from the design-time screenshot, to send an email using the TIdSMTP component, you at least need to specify the SMTP mail server (host). The message itself needs the regular email parts filled out, like the From, To, Subject, etc. Heres the code that handles sending one email with an attachment: procedure TMailerForm.btnSendMailClick(Sender: TObject) ; begin à à StatusMemo.Clear; à à //setup SMTP à à SMTP.Host : ledHost.Text; à à SMTP.Port : 25; à à //setup mail message à à MailMessage.From.Address : ledFrom.Text; à à MailMessage.Recipients.EMailAddresses : ledTo.Text , ledCC.Text; à à MailMessage.Subject : ledSubject.Text; à à MailMessage.Body.Text : Body.Text; à à if FileExists(ledAttachment.Text) then TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text) ; à à //send mail à à try à à à à try à à à à à à SMTP.Connect(1000) ; à à à à à à SMTP.Send(MailMessage) ; à à à à except on E:Exception do à à à à à à StatusMemo.Lines.Insert(0, ERROR: E.Message) ; à à à à end; à à finally à à à à if SMTP.Connected then SMTP.Disconnect; à à end; end; (* btnSendMail Click *) Note:à Inside the source code, youll find two extra procedures that are used to make the values of the Host, From, and To edit boxes persistent, using an INI file for storage.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.