日期: 2020-06-23 12:56:53
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实现邮件的发送,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对奥多码网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!