delphi indy_使用Delphi和Indy发送电子邮件(和附件)

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...

以下是创建“电子邮件发件人”的说明,其中包括用于直接从Delphi应用程序发送电子邮件和附件的选项。 在开始之前,请考虑替代方案...

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.

假设您有一个对某些数据库数据进行操作的应用程序,除其他任务外。 用户需要从您的应用程序中导出数据, 通过电子邮件(例如错误报告)发送数据。 如果没有下面概述的方法,则必须将数据导出到外部文件,然后使用电子邮件客户端发送数据。

从Delphi发送电子邮件 ( 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. 

您可以通过多种方式直接从Delphi直接发送电子邮件,但最简单的方法是使用ShellExecute API。 这将使用计算机上安装的默认电子邮件客户端发送电子邮件。 尽管可以接受这种方法,但是您无法以这种方式发送附件。

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.

另一种技术是使用Microsoft Outlook和OLE发送电子邮件,这一次具有附件支持,但是随后要求使用MS Outlook。

Yet another option is to use Delphi's built-in support for the Windows Simple Mail API. This works only if the user has a MAPI-compliant email program installed.

另一个选择是使用Delphi对Windows Simple Mail API的内置支持。 仅当用户安装了MAPI兼容的电子邮件程序时,此方法才有效。

The technique we're 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.

我们在这里讨论的技术使用Indy (Internet Direct)组件-一个很棒的Internet组件套件,包含用Delphi编写并基于阻塞套接字的流行Internet协议。

TIdSMTP(Indy)方法 ( 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."

使用Indy组件(Delphi 6+附带)发送(或检索)电子邮件就像在窗体上拖放一两个组件,设置一些属性并“单击按钮”一样容易。

To send an email with attachments from Delphi using Indy, we'll 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.

要使用Indy从Delphi发送带有附件的电子邮件,我们需要两个组件。 首先, TIdSMTOP用于连接SMTP服务器并与之通信(发送邮件)。 其次, TIdMessage处理消息的存储和编码。

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 )

I've 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 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).

注意:该链接是直接下载到项目的ZIP文件。 您应该可以毫无问题地打开它,但是如果不能打开,请使用7-Zip打开存档,以便提取出项目文件(存储在名为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.

从设计时屏幕快照中可以看到,要使用TIdSMTP组件发送电子邮件,至少需要指定SMTP邮件服务器(主机)。 邮件本身需要填写常规的电子邮件部分,例如“ 发件人” ,“ 收件人” ,“ 主题 ”等。

Here's the code that handles sending one email with an attachment:


procedure TMailerForm.btnSendMailClick(Sender: TObject) ;
  //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
      SMTP.Connect(1000) ;
      SMTP.Send(MailMessage) ;
    except on E:Exception do
      StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message) ;
    if SMTP.Connected then SMTP.Disconnect;
end; (* btnSendMail 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 file for storage.



delphi indy

  • 0
  • 0
  • 0
  • 一键三连
  • 扫一扫,分享海报

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
钱包余额 0