Delphi - Indy idMessage和idSMTP实现邮件的发送

编程学习 2021-07-05 13:20www.dzhlxh.cn编程入门
这篇文章主要介绍了Delphi - Indy idMessage和idSMTP实现邮件的发送,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

idMessage / idSMTP

首先对idMessage类的各种属性进行赋值(邮件的基本信息,如收件人、邮件主题、邮件正文等),其次通过idSMTP连接邮箱服务器,最后通过idSMTP的Send方法将idMessage发送出去。

界面布局如下:

代码如下:

unit uMain;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, ExtCtrls, RzPanel, RzShellDialogs, IdMessage, IdBaseComponent,
 IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,
 RzButton, StdCtrls, RzEdit, RzBtnEdt, Mask, RzLabel;
type
 TMainFrm = class(TForm)
  gbMsgSet: TRzGroupBox;
  gbSrvSet: TRzGroupBox;
  lbSubject: TRzLabel;
  lbRsd: TRzLabel;
  lbCc: TRzLabel;
  lbBCc: TRzLabel;
  lbAth: TRzLabel;
  lbBdy: TRzLabel;
  lbUserName: TRzLabel;
  lbHost: TRzLabel;
  lbPsd: TRzLabel;
  edtSub: TRzEdit;
  edtRsd: TRzEdit;
  edtCc: TRzEdit;
  edtBCc: TRzEdit;
  beAth: TRzButtonEdit;
  mmBdy: TRzMemo;
  btnSendMail: TRzBitBtn;
  edtUN: TRzEdit;
  edtHst: TRzEdit;
  edtPsd: TRzEdit;
  IdSMTP: TIdSMTP;
  IdMessage: TIdMessage;
  odMain: TRzOpenDialog;
  procedure beAthButtonClick(Sender: TObject);
  procedure btnSendMailClick(Sender: TObject);
 private
  { Private declarations }
 public
  { Public declarations }
 end;
var
 MainFrm: TMainFrm;
implementation
{$R *.dfm}
procedure TMainFrm.beAthButtonClick(Sender: TObject);
begin
 with odMain do
 begin
  Execute;
  if FileName <> '' then
  begin
   beAth.Text := FileName;
  end;
 end;
end;
procedure TMainFrm.btnSendMailClick(Sender: TObject);
begin
 try
  if (Trim(edtCc.Text) = '') and (Trim(edtRsd.Text) = '') and (Trim(edtBCc.Text) = '') then
  begin
   MessageDlg('You should input Rsd, please check,thanks!', mtInformation, [mbOK], 0);
   edtRsd.SetFocus;
   Exit;
  end;
  with IdMessage do
  begin
   Clear;
   Subject := edtSub.Text;
   From.Text := edtUN.Text;
   Recipients.EMailAddresses := edtRsd.Text;
   CCList.EMailAddresses := edtCC.Text;
   BccList.EMailAddresses := edtBCc.Text;
   Priority := TIdMessagePriority(4);
   if Trim(beAth.Text) <> '' then
   begin
    TIdAttachment.Create(MessageParts, Trim(beAth.Text));
   end;
   Body.Assign(mmBdy.Lines);
  end;
 except
  on E: Exception do
  begin
   MessageDlg('Msg Set Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], 0);
   Exit;
  end;
 end;
 try
  if (Trim(edtUN.Text) = '') or (Trim(edtHst.Text) = '') or (Trim(edtPsd.Text) = '') then
  begin
   MessageDlg('You should input UN, please check,thanks!', mtInformation, [mbOK], 0);
   edtUN.SetFocus;
   Exit;
  end;
  with IdSMTP do
  begin
   if Connected then Disconnect;
   AuthenticationType := atLogin;
   Port := 25;
   UserName := edtUN.Text;
   Password := edtPsd.Text;
   Host := edtHst.Text;
   Connect;
  end;
 except
  on E: Exception do
  begin
   MessageDlg('Srv Set Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], 0);
   Exit;
  end;
 end;
 try
  IdSMTP.Send(IdMessage);
  IdSMTP.Disconnect;
  MessageDlg('OK!', mtInformation, [mbOK], 0);
 except
  on E: Exception do
  begin
   MessageDlg('Send Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], 0);
   Exit;
  end;
 end;
end;

end.

总结

以上所述是长沙网络推广给大家介绍的Delphi - Indy idMessage和idSMTP实现邮件的发送,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
如果你觉得本文对你有帮助,欢迎网络推广网站推广转载,烦请注明出处,谢谢!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板