欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Delphi - Indy TIdMessage和TIdSMTP实现邮件的发送

程序员文章站 2022-03-03 08:21:23
idMessage / idSMTP 首先对idMessage类的各种属性进行赋值(邮件的基本信息,如收件人、邮件主题、邮件正文等),其次通过idSMTP连接邮箱服务器,最后通过idSMTP的Send方法将idMessage发送出去。 界面布局如下: 代码如下: ......

idmessage / idsmtp

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

界面布局如下: 

Delphi - Indy TIdMessage和TIdSMTP实现邮件的发送

代码如下:

  1 unit umain;
  2 
  3 interface
  4 
  5 uses
  6   windows, messages, sysutils, variants, classes, graphics, controls, forms,
  7   dialogs, extctrls, rzpanel, rzshelldialogs, idmessage, idbasecomponent,
  8   idcomponent, idtcpconnection, idtcpclient, idmessageclient, idsmtp,
  9   rzbutton, stdctrls, rzedit, rzbtnedt, mask, rzlabel;
 10 
 11 type
 12   tmainfrm = class(tform)
 13     gbmsgset: trzgroupbox;
 14     gbsrvset: trzgroupbox;
 15     lbsubject: trzlabel;
 16     lbrsd: trzlabel;
 17     lbcc: trzlabel;
 18     lbbcc: trzlabel;
 19     lbath: trzlabel;
 20     lbbdy: trzlabel;
 21     lbusername: trzlabel;
 22     lbhost: trzlabel;
 23     lbpsd: trzlabel;
 24     edtsub: trzedit;
 25     edtrsd: trzedit;
 26     edtcc: trzedit;
 27     edtbcc: trzedit;
 28     beath: trzbuttonedit;
 29     mmbdy: trzmemo;
 30     btnsendmail: trzbitbtn;
 31     edtun: trzedit;
 32     edthst: trzedit;
 33     edtpsd: trzedit;
 34     idsmtp: tidsmtp;
 35     idmessage: tidmessage;
 36     odmain: trzopendialog;
 37     procedure beathbuttonclick(sender: tobject);
 38     procedure btnsendmailclick(sender: tobject);
 39   private
 40     { private declarations }
 41   public
 42     { public declarations }
 43   end;
 44 
 45 var
 46   mainfrm: tmainfrm;
 47 
 48 implementation
 49 
 50 {$r *.dfm}
 51 
 52 procedure tmainfrm.beathbuttonclick(sender: tobject);
 53 begin
 54   with odmain do
 55   begin
 56     execute;
 57     if filename <> '' then
 58     begin
 59       beath.text := filename;
 60     end;
 61   end;
 62 end;
 63 
 64 procedure tmainfrm.btnsendmailclick(sender: tobject);
 65 begin
 66   try
 67     if (trim(edtcc.text) = '') and (trim(edtrsd.text) = '') and (trim(edtbcc.text) = '') then
 68     begin
 69       messagedlg('you should input rsd, please check,thanks!', mtinformation, [mbok], 0);
 70       edtrsd.setfocus;
 71       exit;
 72     end;
 73     with idmessage do
 74     begin
 75       clear;
 76       subject := edtsub.text;
 77       from.text := edtun.text;
 78       recipients.emailaddresses := edtrsd.text;
 79       cclist.emailaddresses := edtcc.text;
 80       bcclist.emailaddresses := edtbcc.text;
 81       priority := tidmessagepriority(4);
 82       if trim(beath.text) <> '' then
 83       begin
 84         tidattachment.create(messageparts, trim(beath.text));
 85       end;
 86       body.assign(mmbdy.lines);
 87     end;
 88   except
 89     on e: exception do
 90     begin
 91       messagedlg('msg set failed with err information [' + e.message + ']', mtwarning, [mbok], 0);
 92       exit;
 93     end;
 94   end;
 95   try
 96     if (trim(edtun.text) = '') or (trim(edthst.text) = '') or (trim(edtpsd.text) = '') then
 97     begin
 98       messagedlg('you should input un, please check,thanks!', mtinformation, [mbok], 0);
 99       edtun.setfocus;
100       exit;
101     end;
102     with idsmtp do
103     begin
104       if connected then disconnect;
105       authenticationtype := atlogin;
106       port := 25;
107       username := edtun.text;
108       password := edtpsd.text;
109       host := edthst.text;
110       connect;
111     end;
112   except
113     on e: exception do
114     begin
115       messagedlg('srv set failed with err information [' + e.message + ']', mtwarning, [mbok], 0);
116       exit;
117     end;
118   end;
119 
120   try
121     idsmtp.send(idmessage);
122     idsmtp.disconnect;
123     messagedlg('ok!', mtinformation, [mbok], 0);
124   except
125     on e: exception do
126     begin
127       messagedlg('send failed with err information [' + e.message + ']', mtwarning, [mbok], 0);
128       exit;
129     end;
130   end;
131 
132 end;
133 
134 end.