Delphi編寫實現的郵件群發程序代碼,主要是使用Indy組件來實現,發送郵件使用SMTP協議,使用TidMessage組件配合TIdSMTP組件來完成,本郵件群發程序運行界面圖:
完整的Delphi郵件群發代碼:
001
unit
Unit1;
002
interface
003
uses
004
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
005
Dialogs, StdCtrls, ExtCtrls, IdMessage, IDBaseComponent, IdComponent,
006
IdTCPConnection, IdTCPClient, IdMessageClIEnt, IdSMTP, ComCtrls, Menus, IniFiles;
007
type
008
TForm1 =
class
(TForm)
009
IdSMTP1: TIdSMTP;
010
IdMessage1: TIdMessage;
011
ListBox1: TListBox;
012
le_title: TLabeledEdit;
013
me_content: TMemo;
014
Label1: TLabel;
015
GroupBox1: TGroupBox;
016
GroupBox2: TGroupBox;
017
le_smtp: TLabeledEdit;
018
le_user: TLabeledEdit;
019
le_email: TLabeledEdit;
020
le_port: TLabeledEdit;
021
Button1: TButton;
022
Button2: TButton;
023
Button3: TButton;
024
Button4: TButton;
025
Button5: TButton;
026
OpenDialog1: TOpenDialog;
027
SaveDialog1: TSaveDialog;
028
StatusBar1: TStatusBar;
029
Button6: TButton;
030
PopupMenu1: TPopupMenu;
031
N1: TMenuItem;
032
N2: TMenuItem;
033
N3: TMenuItem;
034
N4: TMenuItem;
035
le_pass: TLabeledEdit;
036
cb_authentication: TCheckBox;
037
procedure
Button1Click(Sender: TObject);
038
Button2Click(Sender: TObject);
039
Button5Click(Sender: TObject);
040
FormShow(Sender: TObject);
041
Button6Click(Sender: TObject);
042
FormCloseQuery(Sender: TObject;
var
CanClose:
Boolean
);
043
N1Click(Sender: TObject);
044
N2Click(Sender: TObject);
045
N3Click(Sender: TObject);
046
Button3Click(Sender: TObject);
047
Button4Click(Sender: TObject);
048
private
049
function
SendEmail(aAddr:
string
):
boolean
;
050
{ Private declarations }
051
public
052
{ Public declarations }
053
end
054
055
Form1: TForm1;
056
implementation
057
{$R *.dfm}
058
TForm1
.
059
begin
060
if
not
OpenDialog1
Execute
then
Exit;
//如果沒有選擇文件則退出
061
ListBox1
Items
LoadFromFile(OpenDialog1
FileName);
062
063
064
065
Count =
0
exit;
066
SaveDialog1
//選擇了保存文件
067
SaveToFile(SaveDialog1
068
069
070
//保存配置
071
with
TIniFile
Create(ExtractFilePath(ParamStr(
)) +
'comm.ini'
)
do
072
073
WriteString(
'comm'
,
'smtp'
, le_smtp
Text);
074
'port'
, le_port
075
'user'
, le_user
076
'pass'
, le_pass
077
'email'
, le_email
078
WriteBool(
, cb_authentication
Checked);
079
080
081
082
//讀取配置
083
084
085
le_smtp
Text := ReadString(
'smtp.163.com'
086
le_port
'25'
087
le_user
''
088
le_pass
089
le_email
090
cb_authentication
Checked := ReadBool(
False
091
092
093
094
095
Close;
096
097
098
099
Tag =
100
CanClose :=
True
else
101
//正在發送狀態不能退出程序
102
103
ShowMessage(
'請先中斷發送'
104
105
106
107
108
s:
109
110
s := InputBox(
'輸入對話框'
'請輸入郵件地址:'
'@'
111
(Length(s) >
5
and
(Pos(
, s) < Pos(
'.'
, s))
112
Add(s);
113
114
115
116
DeleteSelected;
117
118
119
120
Clear;
121
122
123
124
Tag :=
//用Form1的Tag屬性作為發送中斷的標志可以省一個全局變量
125
126
127
128
IdMessage1
Body
Assign(me_content
Lines);
129
From
Text := le_email
Text;
130
ReplyTo
EMailAddresses := le_email
131
RecipIEnts
EMailAddresses := aAddr;
132
Subject := le_title
133
134
135
136
i:
integer
137
138
139
140
showmessage(
'發送列表為空,群發失敗!'
141
142
143
me_content
Text =
144
145
'輸入郵件內容!'
146
147
148
Button3
Enabled :=
149
Checked
//服務器驗證
150
151
IdSMTP1
AuthenticationType := atLogin;
152
Username := le_user
153
PassWord := le_pass
154
155
156
AuthenticationType := atNone;
157
158
Host := le_smtp
159
Port := StrToIntDef(le_port
Text,
25
//如果轉換錯誤默認25
160
Connect;
//連接
161
try
162
1
//設置中斷標志
163
for
i :=
to
Count -
164
//循環發送
165
//如果中斷則退出群發
166
SendEmail(ListBox1
Strings[i]);
167
Send(IdMessage1);
168
ItemIndex := i;
169
StatusBar1
SimpleText := Format(
'正在發送... (%d/%d) %s'
, [i, ListBox1
, ListBox1
Strings[i]]);
170
Application
ProcessMessages;
171
172
finally
173
174
Disconnect;
175
176
177
SimpleText :=
'發送完成...'
178
179
日前整理倉庫,翻出了一些 Delphi 產品,以前購買的 D
在窗體上添加 Panel1、PaintBox1 和 Bu
之前已經有了兩種多線程的同步方法:Critic
/*Title:Insun的故事 *Author:In
設為首頁加入收藏站內搜索網站地圖首頁編程語言 web開發
//先來個例子:procedure TForm