idMessage / idSMTP
首先对idMessage类的各种属性进行赋值(邮件的基本信息,如收件人、邮件主题、邮件正文等),其次通过idSMTP连接邮箱服务器,最后通过idSMTP的Send方法将idMessage发送出去。
界面布局如下:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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实现邮件的发送,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
原文链接:https://www.cnblogs.com/jeremywucnblog/archive/2019/08/29/11427658.html