Delphi - Indy TIdMessage和TIdSMTP实现邮件的发…
2019-08-29 08:54:42来源:博客园 阅读 ()
Delphi - Indy TIdMessage和TIdSMTP实现邮件的发送
idMessage / idSMTP
首先对idMessage类的各种属性进行赋值(邮件的基本信息,如收件人、邮件主题、邮件正文等),其次通过idSMTP连接邮箱服务器,最后通过idSMTP的Send方法将idMessage发送出去。
界面布局如下:
代码如下:
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.
原文链接:https://www.cnblogs.com/jeremywucnblog/p/11427658.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Delphi10.3的ListView学习(大图标) 2020-06-05
- Delphi10.3的SpeedButton/BitBtn学习 2020-06-05
- Delphi10.3状态栏上显示进度条/图片 2020-06-02
- Delphi10.3下自带系统托盘 TrayIcon的使用 2020-06-01
- delphi10.3安装使用mySQL 2020-05-31
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash