在做mes系统时,利用D7 IdMessage&IdSMTP发送邮件,从盒子上学习了不少的东西,自己整理了一下,想share给新手,学习参考!

时间:2021-01-09 18:02:04

在做mes系统时,利用D7 IdMessage&IdSMTP发送邮件,由于电脑中病毒,程序出异常,后调试ok,实现效果,初次接触D7时,从盒子上学习了不少的东西,自己整理了一下,想share给新手,学习参考 !

unit Unit1;

interface

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

type
  TSMTPForm = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    GroupBox5: TGroupBox;
    btnSend: TButton;
    AttachAdd: TButton;
    GroupBox6: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    HostName: TEdit;
    HostPort: TEdit;
    EditFrom: TEdit;
    EditTo: TEdit;
    EditUser: TEdit;
    EditSubject: TEdit;
    MemoMesg: TMemo;
    AttachmentList: TListBox;
    MemoInfo: TMemo;
    EditPass: TMaskEdit;
    MailMessage: TIdMessage;
    IdSMTP: TIdSMTP;
    AddAttachDlg: TOpenDialog;
    Label9: TLabel;
    EditCC: TEdit;
    Label10: TLabel;
    EditBCC: TEdit;
    procedure btnSendClick(Sender: TObject);
    procedure IdSMTPConnected(Sender: TObject);
    procedure IdSMTPDisconnected(Sender: TObject);
    procedure AttachAddClick(Sender: TObject);
    procedure AttachmentListKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure IdSMTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
    procedure IdSMTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
    procedure IdSMTPWork(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SMTPForm: TSMTPForm;

implementation

{$R *.dfm}

procedure TSMTPForm.btnSendClick(Sender: TObject);

begin
   MemoInfo.Clear;
//1:對所必須要的信息進行進行檢驗

  //校驗服務器屬性
  if (Trim(HostName.Text)='') or  (Trim(HostPort.Text)='') then
    begin
    ShowMessage('請設置所要連接的SMTP服務器屬性!');
    HostPort.Text:='25';
    HostName.SetFocus;
    Exit;
    end;
   //檢測地址信息
  if (Trim(EditFrom.Text)='') or (Trim(EditTo.Text)='') then
     begin
     ShowMessage('請輸入收信人或者發信人地址!');
     EditFrom.SetFocus ;
     Exit;
     end;
   //用戶賬號檢驗
  if (Trim(EditUser.Text)='') or (Trim(EditPass.Text)='') then
     begin
     ShowMessage('請正確輸入用戶登錄帳號和密碼!');
     EditUser.SetFocus;
     Exit;
     end;
   //對MailMessage屬性進行賦值
    with MailMessage do
    begin
      Subject := EditSubject.Text; //郵件主題
      Body.Assign(MemoMesg.Lines);//郵件正文
      From.Address:=Trim(EditFrom.Text);     //發信人地址
      Recipients.EMailAddresses := Trim(editTo.Text); //收件人地址
      CCList.EMailAddresses :=EditCC.Text;
      BCCList.EMailAddresses:=EditBCC.Text;
    //  for indexnum:=0 to AttachmentList.Items.Count  do
     //     TIdAttachment.Create(MailMessage.MessageParts,);

    end;

    //設置連接到服務器屬性
   with  IdSMTP do
    begin
    Host:=Trim(HostName.Text);   //SMTP服務器地址
    Port:=StrToInt(Trim(HostPort.Text));     //SMTP服務器端口
    UserName:=Trim(EditUser.Text);           //用戶賬號
    Password:=Trim(EditPass.Text);           // 用戶密碼
    end;
    //連接到服務
    MemoInfo.Lines.Add('準備連接到服務器!'+HostName.Text);
    try
      IdSMTP.Connect();  //調用 Connect連接服務
      except             //連接失敗
         begin
          MemoInfo.Lines.Add('無法連接到服務器!'+HostName.Text);
          Exit;
        end ;
      end;
     //身份驗證
     //檢測SMTP服務器是否需要驗證
      if (IdSMTP.AuthSchemesSupported.IndexOf ( 'LOGIN' ) <> -1)   then
         begin   //服務器要求驗證
          MemoInfo.Lines.Add('服務器要求驗證');
          IdSMTP.AuthenticationType:=atlogin;
           end
       else
           begin  //服務器不要求驗證
               MemoInfo.Lines.Add('服務器不需要驗證');
           end;
        MemoInfo.Lines.Add('開始驗證');
      try
          if IdSMTP.Authenticate then    //驗證通過
             MemoInfo.Lines.Add('服務器驗證通過')
          else
             MemoInfo.Lines.Add('服務器驗證失敗'); //驗證失敗
    except
        begin
            MemoInfo.Lines.Add('服務器驗證失敗');
            IdSMTP.Disconnect;

            exit;
         end;
      end;
      //發送信件
       try
        IdSMTP.Send(MailMessage);
      
       except
          MemoInfo.Lines.Add('發送失敗');
          end;
          IdSMTP.Disconnect;
       //發送完成

end;

procedure TSMTPForm.IdSMTPConnected(Sender: TObject);
begin
 MemoInfo.Lines.Add('已經連接到服務器!'+HostName.Text)
end;

procedure TSMTPForm.IdSMTPDisconnected(Sender: TObject);
begin
    MemoInfo.Lines.Add('斷開連接');
end;

procedure TSMTPForm.AttachAddClick(Sender: TObject);
begin
if AddAttachDlg.Execute then
      begin
       AttachmentList.Items.Add(AddAttachDlg.FileName);
       TIdAttachment.Create(MailMessage.MessageParts, AddAttachDlg.FileName);

      end;
end;

procedure TSMTPForm.AttachmentListKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
  var
   index:Integer;
  begin
   //有效性檢查
   if AttachmentList.ItemIndex=-1 then
      exit;
   //如果按Delete鍵,則刪除該附件
   if Key<>VK_DELETE then
   exit;
   index:=AttachmentList.ItemIndex;
   AttachmentList.Items.Delete(index);  //刪除列表中項
   MailMessage.MessageParts.Delete(index);
end;

procedure TSMTPForm.IdSMTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
 MemoInfo.Lines.Add('發送信件成功');
end;

procedure TSMTPForm.IdSMTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCountMax: Integer);
begin
 MemoInfo.Lines.Add('開始發送信件');
end;

procedure TSMTPForm.IdSMTPWork(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
 MemoInfo.Lines.Add('正在發送信件');
end;

end.