你可以使用 IdSMTP 和 IdMessage 控件来写,主要的发送部分代码如下:
(要注意有的邮箱可能不支持SMTP,不让你在客户端发邮件)
//复制邮件信息
with IdMessage1 do
begin
Body.Assign(Memo1.Lines); //邮件正文
From.Text:=UserEmail;
ReplyTo.EMailAddresses:=UserEmail;
Recipients.EMailAddresses:=Edit1.Text;
Subject:= Edit2.Text; //主题
Priority:= TIdMessagePriority(ComboBox1.ItemIndex);
CCList.EMailAddresses:=Edit3.Text;
BccList.EMailAddresses:= Edit4.Text;
if CheckBox1.Checked then
begin
ReceiptRecipient.Text:=From.Text;
end
else
ReceiptRecipient.Text:='';
end;
//设置与SMTP服务器的连接属性
with IdSMTP1 do
begin
Username:=SmtpServerUser;
PassWord:=SmtpServerPassWord;
Host:=SmtpServerName;
Port:= SmtpServerPort;
end;
//建立连接,发送邮件
Try
Try
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;
except
MessageDlg('发送邮件出现错误!',mtError,[mbYes],0);
end;