楼主,你用我下面的代码试试吧。(我试了之后是没有问题的,应该是你代码的问题,我的版本是3102)
static function TYJFS.SendWebMail(lFrom,lSendTO,lBCC,lCC,lSubject : String;
lSMTP,lPop3 : String; lSMTPPort,lPop3Port : Integer;
lAccoundID,lMailAddress,lUserID,lPassword : String; lVerify : Boolean;
lVerifyAccount,lVerifyPassword : String;
lMemo : TMemo;lFileName : String) : Boolean;
var
SendMailExchang: TMessageExchanger; //用于发送邮件的交换机
lAccount: TMailAccount;
MailSystem: TMailSystem;
lMailContent: TMemoryStream;
lMessage: TMessage;
lFrm : TZZFSYJ;
begin
Result := False;
MailSystem := TMailSystem.Create;
SendMailExchang := TMessageExchanger.Create(nil);
lMessage := TMessage.Create(nil);
lAccount := TMailAccount.Create;
lFrm := TZZFSYJ.Create(nil);
try
lFrm.Show;
if Borland.Delphi.Windows.MessageBox(0,'确定开始发送邮件吗?','是否发邮件提示',Borland.Delphi.Windows.MB_YESNO) =Borland.Delphi.Windows.IDNO then
begin
lFrm.Free;
lMessage.Free;
MailSystem.Free;
SendMailExchang.Free; // SendMailExchang对象一定要最后释放
Exit;
end;
SendMailExchang.MessageKind := TMessageKind.mkSend;
lMessage.From := lFrom; //发件人
lMessage.SendTo := lSendTO; //收件人
//lMessage.BCC := lBCC;
lMessage.CC := lCC;
lMessage.Subject := lSubject; //邮件标题
lMessage.Body.Text := Trim(lMemo.Text); //正文
lMessage.ContentFormatType := TContentFormatType.ftText; // 设置正文内容格式
lMessage.Date := business.System.SysUtils.DateToStr(Business.System.SysUtils.Time);
//根据邮件的发件人获取发送邮件的帐号,
//此帐号是从Business邮件系统中设置的帐号中获得的
//如果使用自定义的发送帐户,请重新设置lAccount对象的相关属性
//lAccount := MailSystem.FindAccountByAddress(lMessage.From);
lAccount.SMTPHost := lSMTP;
lAccount.SMTPPort := lSMTPPort;
lAccount.POPHost := lPop3;
lAccount.POPPort := lPop3Port;
lAccount.AccountID := lAccoundID;
lAccount.MailAddress := lMailAddress;
lAccount.UserID := lUserID;
lAccount.Password := lPassword;
lAccount.Verify := lVerify;
lAccount.VerifyAccount := lVerifyAccount;
lAccount.VerifyPassword := lVerifyPassword;
if FileExists(lFileName) then
lMessage.AddAttachment(lFileName);
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,false)//这里的最后一个参数,不知道你的版本中是否有,如果你的版本中不需要就去掉它
else
SendMailExchang.SMTPMessages.Add(lAccount.UserID, lAccount.Password,
lAccount.SMTPHost, lMessage.From, lMessage.SendTo, lMailContent,
lAccount.SMTPPort,false);
end
else
SendMailExchang.SMTPMessages.Add(lAccount.SMTPHost, lMessage.From,
lMessage.SendTo, lMailContent, lAccount.SMTPPort,false);
finally
lMailContent.Free;
lAccount.Free;
end;
SendMailExchang.Active := True; //发送邮件
Sleep(3000);
end;
Result := True;
except
Result := False;
end;
lFrm.Free;
lMessage.Free;
MailSystem.Free;
SendMailExchang.Free; // SendMailExchang对象一定要最后释放
end; |