先创建一个空的Delphi应用程序
放5个TMemos, 8 个TEdits, 1个 TListBox, 3 个TButtons, a TCheckBox, a TNMSMTP, a TRadioGroup, and a TOpenDialog 在主窗体
Edit1: Host 属性
Edit2: 你的邮箱名字
Edit3: PostMessage.Date 属性
Edit4: PostMessage.FromAddress 属性
Edit5: PostMessage.FromName 属性
Edit6: PostMessage.LocalProgram 属性
Edit7: PostMessage.ReplyTo属性
Edit8: PostMessage.Subject 属性。邮件的主题
Memo1: PostMessage.ToAddress 属性
Memo2: PostMessage.ToBlindCarbonCopy 同时发送的信箱
Memo3: PostMessage.ToCarbonCopy 密送信箱
Memo4: PostMessage.Body 内容
Memo5: 显示状态
ListBox1: PostMessage.Attachments 附件
Button1: 连接、不连接 按钮
Button2: 发送按钮
Button3: 清空按钮
CheckBox1: 设置参数的值
OpenDialog1: 添加附件
RadioGroup1: 指定文件的发送方式
添加2 items 在 RadioGroup1的 Items 属性: MIME 和 UUEncode
Insert the following code into Button1's OnClick event:
procedure TForm1.Button1Click(Sender: TObject);
begin
if NMSMTP1.Connected then
NMSMTP1.Disconnect
else
begin
NMSMTP1.Host := Edit1.Text;
NMSMTP1.UserID := Edit2.Text;
NMSMTP1.Connect;
end;
end;
当Button1被单击时,如果NMSMTP1已经连上了邮件服务器,则把他断开,否则,连上Edit1指定的邮件服务器
procedure TForm1.Button2Click(Sender: TObject);
begin
if NMSMTP1.Connected then
begin
NMSMTP1.ClearParams := CheckBox1.Checked;
NMSMTP1.SubType := mtPlain;
case RadioGroup1.ItemIndex of
0: NMSMTP1.EncodeType := uuMime;
1: NMSMTP1.EncodeType := uuCode;
end;
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit5.Text;
NMSMTP1.PostMessage.ToAddress.Text := Memo1.Text;
NMSMTP1.PostMessage.ToCarbonCopy.Text := Memo3.Text;
NMSMTP1.PostMessage.ToBlindCarbonCopy.Text := Memo2.Text;
NMSMTP1.PostMessage.Body.Text := Memo4.Text;
NMSMTP1.PostMessage.Attachments.Text := ListBox1.Items.Text;
NMSMTP1.PostMessage.Subject := Edit8.Text;
NMSMTP1.PostMessage.LocalProgram := Edit6.Text;
NMSMTP1.PostMessage.Date := Edit3.Text;
NMSMTP1.PostMessage.ReplyTo := Edit7.Text;
NMSMTP1.SendMail;
end
else
ShowMessage('You need to connect before you can send your message');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
NMSMTP1.ClearParameters;
Edit3.Clear;
Edit4.Clear;
Edit5.Clear;
Edit6.Clear;
Edit7.Clear;
Edit8.Clear;
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
Memo4.Clear;
Memo5.Clear;
ListBox1.Clear;
end;
procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_INSERT then
if OpenDialog1.Execute then
ListBox1.Items.Add(OpenDialog1.FileName);
if Key = VK_DELETE then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
I
procedure TForm1.NMSMTP1AttachmentNotFound(Filename: String);
begin
Memo5.Lines.Add('File attachment '+FileName+' not found');
end;
procedure TForm1.NMSMTP1AuthenticationFailed(var Handled: Boolean);
var
S: String;
begin
S := NMSMTP1.UserID;
if InputQuery('Authentication Failed', 'Invalid User ID. New User ID: ', S) then
begin
NMSMTP1.UserID := S;
Handled := TRUE;
end;
end;
procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
Memo5.Lines.Add('Connected');
end;
procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
Memo5.Lines.Add('Sending Message');
end;
procedure TForm1.NMSMTP1EncodeStart(Filename: String);
begin
Memo5.Lines.Add('Encoding '+FileName);
end;
procedure TForm1.NMSMTP1EncodeEnd(Filename: String);
begin
Memo5.Lines.Add(FileName+' encoded');
end;
procedure TForm1.NMSMTP1Failure(Sender: TObject);
begin
Memo5.Lines.Add('Message delivery failure');
end;
procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
Memo5.Lines.Add('Message sent successfully');
end;
procedure TForm1.NMSMTP1HeaderIncomplete(var handled: Boolean; hiType: Integer);
var
S: String;
begin
case hiType of
hiFromAddress:
if InputQuery('Missing From Address', 'Enter From Address: ', S) then
begin
NMSMTP1.PostMessage.FromAddress := S;
Handled := TRUE;
end;
hiToAddress:
if InputQuery('Missing To Address', 'Enter To Address: ', S) then
begin
NMSMTP1.PostMessage.ToAddress.Text := S;
Handled := TRUE;
end;
end;
end;
procedure TForm1.NMSMTP1RecipientNotFound(Recipient: String);
begin
Memo5.Lines.Add('Recipient '+Recipient+' not found');
end;