procedure TCCBGSQDForm.Button1Click(Sender: TObject);
var
SendMailExchang: TMessageExchanger;
lMessage: TMessage;
lAccount: TMailAccount;
MailSystem: TMailSystem;
lMailContent: TMemoryStream;
begin
SendMailExchang := TMessageExchanger.Create(nil);
lMessage := TMessage.Create(nil);
lAccount := TMailAccount.Create;
MailSystem := TMailSystem.Create;
try
SendMailExchang.MessageKind := TMessageKind.mkSend;
lAccount.SMTPHost:='smtp.163.com';
lAccount.SMTPPort:=25;
lAccount.POPHost:='pop.163.com';
lAccount.POPPort:=101;
lAccount.MailAddress:='zhjkill@163.com';
lAccount.UserID:='zhjkill';
lAccount.Password:='******';
lAccount.AccountID := 'zhjkill@163.com';
lAccount.Verify := True;
lAccount.VerifyAccount := 'zhjkill@163.com';
lAccount.VerifyPassword := '******';
lMessage.From := 'zhjkill@163.com'; //发件人
lMessage.SendTo := 'zhjkill@163.com'; //收件人
lMessage.Subject := '告警提示!'; //mail tatil
lMessage.Body.Text := '邮件测试!多多打扰请谅解!'; //mail context
lMessage.ContentFormatType := TContentFormatType.ftText; // 设置正文内容格式
lMessage.Date := business.System.SysUtils.DateToStr(business.System.SysUtils.now());
//根据邮件的发件人获取发送邮件的帐号,
//此帐号是从Business邮件系统中设置的帐号中获得的
//如果使用自定义的发送帐户,请重新设置lAccount对象的相关属性
lAccount := MailSystem.FindAccountByAddress(lMessage.From);
if lAccount <> nil then
begin
lMailContent := TMemoryStream.Create; //用于存放邮件的正文
lMailContent.Clear;
lMessage.SaveToStream(lMailContent);
lMailContent.Position := 0; //移动流的指针
try
if lAccount.Verify then
begin
if lAccount.DifferPOP then
SendMailExchang.SMTPMessages.Add(lAccount.VerifyAccount,
lAccount.VerifyPassword, lAccount.SMTPHost,
lMessage.From, lMessage.SendTo, lMailContent, lAccount.SMTPPort)
else
SendMailExchang.SMTPMessages.Add(lAccount.UserID, lAccount.Password,
lAccount.SMTPHost, lMessage.From, lMessage.SendTo, lMailContent,
lAccount.SMTPPort);
end
else
SendMailExchang.SMTPMessages.Add(lAccount.SMTPHost, lMessage.From,
lMessage.SendTo, lMailContent, lAccount.SMTPPort);
finally
lMailContent.Free;
lAccount.Free;
end;
SendMailExchang.Active := True; //发送邮件
dialogs.showmessage('邮件发送成功!');
end;
finally
lMessage.Free;
MailSystem.Free;
SendMailExchang.Free; // SendMailExchang对象一定要最后释放
end;
end;
改成这样还是不行
运行到if lAccount <> nil then 时lAccount 就成空对象 |