邮件发送-控件(WEB)
# FastWeb之邮件发送-控件(WEB)
# 1. 说明
该范例使用SMTP实现邮件发送的功能。使用该范例前,请先确认用来发送邮件的邮箱是否开启了SMTP的功能,只有开启了SMTP功能才可以在FastWeb中实现邮件的发送。目前邮件发送支持两种发送模式。
- 纯文本模式:正文内容为纯文本。
- HTML文本模式:正文的内容为HTML文本。
# 2. 设计明细
开启FastWeb设计器,分别加入下插图之控件。或者点击左上角的[导入]
选择模板文件来打开对应模板。

1:TUgEdit组件,控件名称为UgEdit01
。
2:TUgEdit组件,控件名称为UgEdit02
。
3:TUgEdit组件,控件名称为UgEdit03
。
4:TUgEdit组件,控件名称为UgEdit04
。
5:TUgMemo组件,控件名称为UgMemo01
。
6:TUgHTMLMemo组件,控件名称为UgHTMLMemo01
。
7:TUgButton组件,控件名称为UgButton01
。
8:TUgComboBox组件,控件名称为UgComboBox01
。
9:TUgFileUploadButton组件,控件名称为UgFileUploadButton01
。
10:TUgEmailMsg组件,控件名称为UgEmailMsg01
。
UgWebRunFrame属性设置
Height
:设置页面高度=467
。Width
:设置页面宽度=475
。
1:UgEdit01属性设置
EmptyText
:设置当编辑框为空时显示的文本内容,设置为请输入收件人
。FieldLabel
:设置标签显示的文本=收件人
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。
2:UgEdit02属性设置
EmptyText
:设置当编辑框为空时显示的文本内容,设置为输入抄送人(可留空)
。FieldLabel
:设置标签显示的文本=抄送
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。
3:UgEdit03属性设置
Text
:设置当编辑框显示的文本内容,设置为欢迎使用FastWeb邮件发送控件
。FieldLabel
:设置标签显示的文本=主题
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。
4:UgEdit04属性设置
EmptyText
:设置当编辑框为空时显示的文本内容,设置为在正文为纯文本时可添加附件
。FieldLabel
:设置标签显示的文本=附件
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。ReadOnly
:是否设置为只读,设置为是
。
5:UgMemo01属性设置
FieldLabel
:设置标签显示的文本=正文
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。
6:UgHTMLMemo属性设置
FieldLabel
:设置标签显示的文本=正文
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。Visible
:设置控件是否可见,设置为False
。
7:UgButton01属性设置
Caption
:设置按钮显示的字幕内容,设置为发送邮件
。
8:UgComboBox01属性设置
FieldLabel
:设置标签显示的文本=正文格式
。FieldLabelAlign
:设置标签显示的相对位置。设置为laTop
。Items
:设置复选框的可选项,点击属性右侧的[√]
,打开编辑框,在其中输入如下图所示的选项。
9:UgFileUploadButton01属性设置
Caption
:设置按钮的文字内容,设置为选择附件
。
# 3. 程序设计
# 3.1. 程序初始设置
该程式无程序初始设置。
# 3.2. 事件设置
- ⑦UgButton01-OnClick事件
点击[发送邮件]
按钮,发送邮件。
//JScript
function UgButton01OnClick(sender)
//发送邮件
{
if (UgEdit01.Text == ""){
ShowMessage(UGMM.LT("收件人不能为空,请填写收件人邮箱地址。"));
return;
}
AttFile = new TStringlist();
Try{
UgEmailMsg01.Host = "smtp.163.com";
UgEmailMsg01.Port = 25;
UgEmailMsg01.Username = "sample@163.com";
UgEmailMsg01.Password = "password";
UgEmailMsg01.Address = UgEdit01.Text;
UgEmailMsg01.Subject = UgEdit03.Text;
UgEmailMsg01.CCAddressList = UgEdit02.Text;
UgEmailMsg01.Receipt = True;
if (UgComboBox01.ItemIndex == 1){
UgEmailMsg01.BodyHtml = True;
UgEmailMsg01.Body = UgHTMLMemo01.Lines.Text;//正文HTML
}
if (UgComboBox01.ItemIndex == 0){
UgEmailMsg01.Body = UgMemo01.Lines.Text;//正文纯文本
}
AttFile.Add(UgEdit04.Text);
UgEmailMsg01.Attachments = AttFile;
if (UgEmailMsg01.SendEmail)
ShowMessage(UGMM.LT("邮件发送成功!"));
else
ShowMessage(UGMM.LT("邮件发送失败!"));
}
Finally{
FreeAndNil(AttFile);
}
}
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
//PasScript
procedure UgButton01OnClick(sender: tobject);
//发送邮件
var
AttFile: TStringlist;
begin
if UgEdit01.Text = '' Then
Begin
ShowMessage(UGMM.LT('收件人不能为空,请填写收件人邮箱地址。'));
Exit;
End;
AttFile := TStringlist.Create;
Try
UgEmailMsg01.Host := 'smtp.163.com';
UgEmailMsg01.Port := 25;
UgEmailMsg01.Username := 'sample@163.com';
UgEmailMsg01.Password := 'password';
UgEmailMsg01.Address := UgEdit01.Text;
UgEmailMsg01.Subject := UgEdit03.Text;
UgEmailMsg01.CCAddressList := UgEdit02.Text;
UgEmailMsg01.Receipt := True;
if UgComboBox01.ItemIndex = 1 Then
UgEmailMsg01.BodyHtml := True;
UgEmailMsg01.Body := UgHTMLMemo01.Lines.Text;//正文HTML
if UgComboBox01.ItemIndex = 0 Then
UgEmailMsg01.Body := UgMemo01.Lines.Text;//正文纯文本
AttFile.Add(UgEdit04.Text);
UgEmailMsg01.Attachments := AttFile;
if UgEmailMsg01.SendEmail then
ShowMessage(UGMM.LT('邮件发送成功!'))
else
ShowMessage(UGMM.LT('邮件发送失败!'));
Finally
FreeAndNil(AttFile);
End;
end;
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
// Make sure to add code blocks to your code group
- ⑧UgComboBox01-OnChange事件
点击切换正文格式中的内容时,修正对应的填写项目。
//JScript
function UgComboBox01OnChange(sender)
{
if (UgComboBox01.ItemIndex == 0)
//纯文本格式
{
UgMemo01.Visible = True;
UgHTMLMemo01.Visible = False;
UgFileUploadButton01.Enabled = True;
}
if (UgComboBox01.ItemIndex == 1){
//HTML格式,会清空附件地址
UgMemo01.Visible = False;
UgHTMLMemo01.Visible = True;
//UgFileUploadButton01.Enabled = False;
//UgEdit04.Text = "";
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//PasScript
procedure UgComboBox01OnChange(sender: tobject);
begin
if UgComboBox01.ItemIndex = 0 Then
//纯文本格式
Begin
UgMemo01.Visible := True;
UgHTMLMemo01.Visible := False;
UgFileUploadButton01.Enabled := True;
End;
IF UgComboBox01.ItemIndex = 1 Then
//HTML格式,会清空附件地址
Begin
UgMemo01.Visible := False;
UgHTMLMemo01.Visible := True;
End;
end;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Make sure to add code blocks to your code group
- ⑨UgFileUploadButton01-OnCompleted事件
当上传文件的按钮完成上传操作时,将文件转存至指定目录并显示目录地址。
//JScript
function UgFileUploadButton01OnCompleted(sender,astreamtfilestream)
//上传附件
var
DestName : string;
DestFolder : string;
{
//远程文件路径
DestFolder=UGSM.StartPath+"temp\\";
DestName=DestFolder+UgFileUploadButton01.FileName;
//上传文件
CopyFile(UGCM.GetFileStreamFileName(AStream), DestName, False);
ugedit04.Text = DestName;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
//PasScript
procedure UgFileUploadButton01OnCompleted(sender: tobject;astream: tfilestream);
//上传附件
var
DestName : string;
DestFolder : string;
begin
//远程文件路径
DestFolder:=UGSM.StartPath+'temp\';
DestName:=DestFolder+UgFileUploadButton01.FileName;
//上传文件
CopyFile(UGCM.GetFileStreamFileName(AStream), DestName, False);
ugedit04.Text := DestName;
end;
2
3
4
5
6
7
8
9
10
11
12
13
14
// Make sure to add code blocks to your code group
# 4. 运行结果
使用鼠标在FastWeb菜单,点击[保存至数据库]
按钮,将其保存至数据库,点击[调试运行]
确认能够正常打开。

在其中填写收件人的邮箱地址,填写正文内容,如使用纯文本格式可点击[选择附件]
,在打开的对话框中选择文件进行添加。添加完成后点击[发送邮件]
,如出现了邮件发送成功!
字样的对话框则发送完成,指定收件人可接收到该邮件。