廣告

2024 年 5 月
 12345
6789101112
13141516171819
20212223242526
2728293031  

彙整

寄信用Indy元件 TIdsmtp 與 TIdmessage 使用範例(動態創建)

紅字部份為手動加入程式,建立個按鈕,按鈕程序如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdMessage, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    MsgIdSMTP: TIdSMTP;
    IdMessage: TIdMessage;

    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  //動態創建
  MsgIdSMTP := TIdSMTP.Create(self); 
  IdMessage := TIdMessage.Create(self);

  MsgIdSMTP.Host := ‘mail.xxx.com.tw’; //設定mail主機
  MsgIdSMTP.Port := 25; //設定使用 port 25寄信

  //寄信訊息設置
  with IDMessage do
  begin
    From.Name := ‘WINPEX IT’; //寄件人顯示名稱
    From.Address := ‘roger@xxx.com.tw’; //寄件人
    Subject := ‘IDMessage Test!!’; //主旨
    Body.Add(‘IDMessage Test!!’+#10#13+’IDMessage Test!!’);
    Recipients.EMailAddresses := ‘roger@xxx.com.tw’;
  end;

  //連線寄信
  with MsgIdSMTP do
  begin
    try
      connect;
      Send(IdMessage);
      Disconnect;
    except on e: exception do
      begin
        if connected then try disconnect; except end;
        MessageDlg(FormatDateTime(‘hh:nn ‘, now) + #13#10 + ‘ 錯誤訊息郵件傳送失敗!’ + #13#10 + E.Message, mtError, [mbOk], 0);
      end;
    end;
  end;

  MsgIdSMTP.Free;
  IdMessage.Free;

end;

end.

收到信畫面:

1

讀者也會看的其它文章:

    Comments are closed.