Delphi發送郵件程序代碼,自制的郵件發送客戶端程序,支持發送附件。程序完工後執行窗體程序,裡面包括郵件主題、發件人地址、姓名、郵件地址、email正文等信息供用戶輸入填寫後,可發送郵件,
01
unit
Unit1;
02
interface
03
uses
04
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
05
Dialogs, StdCtrls, IdMessage, IDBaseComponent, IdComponent,
06
IdTCPConnection, IdTCPClient, IdMessageClIEnt, IdSMTP;
07
type
08
TForm1 =
class
(TForm)
09
Label1: TLabel;
10
edt_SMTP: TEdit;
11
Label2: TLabel;
12
edt_UserName: TEdit;
13
Label3: TLabel;
14
edt_Psw: TEdit;
15
Label4: TLabel;
16
edt_Subject: TEdit;
17
Label5: TLabel;
18
Memo1: TMemo;
19
Label6: TLabel;
20
edt_File: TEdit;
21
Button1: TButton;
22
Button2: TButton;
23
IdSMTP1: TIdSMTP;
24
IdMessage1: TIdMessage;
25
OpenDialog1: TOpenDialog;
26
Label7: TLabel;
27
edt_From: TEdit;
28
Label8: TLabel;
29
edt_To: TEdit;
30
procedure
Button1Click(Sender: TObject);
31
procedure
Button2Click(Sender: TObject);
32
procedure
IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
33
private
34
{ Private declarations }
35
public
36
{ Public declarations }
37
end
;
38
var
39
Form1: TForm1;
40
implementation
41
{$R *.dfm}
42
procedure
TForm1
.
Button1Click(Sender: TObject);
43
begin
44
// 添加附件
45
OpenDialog1
.
FileName :=
''
;
46
OpenDialog1
.
Filter :=
'所有文件|*.*'
;
47
if
OpenDialog1
.
Execute
then
48
begin
49
edt_File
.
Text := OpenDialog1
.
FileName;
50
end
;
51
end
;
52
procedure
TForm1
.
Button2Click(Sender: TObject);
53
begin
54
// 發件人地址
55
IdMessage1
.
From
.
Address := edt_From
.
Text;
56
// 發件人姓名
57
IdMessage1
.
From
.
Name := edt_UserName
.
Text;
58
// 收件人地址
59
IdMessage1
.
RecipIEnts
.
EMailAddresses := edt_To
.
Text;
60
// 主題
61
IdMessage1
.
Subject := edt_Subject
.
Text;
62
// 填寫正文
63
IdMessage1
.
Body
.
Add(Memo1
.
Text);
64
// 添加附件
65
TidAttachment
.
Create(IdMessage1
.
MessageParts, edt_File
.
Text);
66
// SMTP服務器
67
IdSMTP1
.
Host := edt_SMTP
.
Text;
68
// 賬號
69
IdSMTP1
.
Username := edt_UserName
.
Text;
70
// 密碼
71
IdSMTP1
.
PassWord := edt_Psw
.
Text;
72
// 登錄時驗證身份
73
IdSMTP1
.
AuthenticationType := atLogin;
74
// 連接服務器
75
IdSMTP1
.
Connect;
76
// 發送郵件
77
IdSMTP1
.
Send(IdMessage1);
78
end
;
79
procedure
TForm1
.
IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
80
begin
81
ShowMessage(
'發送完畢!'
);
82
// 連接關閉
83
IdSMTP1
.
Disconnect;
84
end
;
85
end
.
你可新建一個Delphi發送郵件的工程文件,將以上代碼引入工程內。