//邮件发送
type TEmailState = (esBegin,esSending,esEnd,esFaild); EMailState = TEmailState;
TSendEmail= class private FHost:string;//主机 FUserName:String;//用户名 FEmail: TStringList;//收件人邮箱列表 FEmailMsg: string; //邮件正文 FSenderEmail: string;//发送人 FSubject: string;//主题 FiPort: Integer;//端口 FPassword: string;//密码 FszPort: string;// SMTP:TIdSMTP; //indy10 SMTP控件 EMessage:TIdMessage;// EAttachment:TIDAttachment;//没用 FAddons:TStringList;//附件列表(文件路径) FEState: EMailState;//邮件状态 FEmailCount: integer;//发送数据 FSendingCount: integer;//正在发送数量 FisSendedCount: integer;//已发数量 procedure SetEmail(const Value: TStringList); procedure SetEmailMsg(const Value: string); procedure SetSenderEmail(const Value: string); procedure SetSubject(const Value: string); procedure SetHost(const Value: String); procedure SetiPort(const Value: Integer); procedure SetPassword(const Value: string); procedure SetszPort(const Value: string); procedure SetUserName(const Value: String); procedure SetEState(const Value: EMailState); procedure SetEmailCount(const Value: integer); procedure SetisSendedCount(const Value: integer); procedure SetSendingCount(const Value: integer); //邮件帐号列表 protected function CheckEmail():Boolean;//取消 function Login(AUserName,APassword,AHost:string;APort:Integer=25):Boolean; //登陆是否成功 取消 public //地址列表,附件列表 constructor Create(UserName,From,PassWord,Host,MasterID:String;EmailAddressList,AddonsPath:TStringList;Port:Integer=25);virtual; destructor Destroy;override; procedure FillEMessage; //填充消息 function StartSendEmail():Boolean; //开始发送 function LoadEmailState():string; //开始 procedure OnStartSendEmail(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Integer); //发送中 procedure OnSendingEmail(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Integer); // 发送完毕 procedure OnEndEmail(ASender: TObject; AWorkMode: TWorkMode); property Email:TStringList read FEmail write SetEmail; property EmailMsg:string read FEmailMsg write SetEmailMsg; property SenderEmail:string read FSenderEmail write SetSenderEmail; property Subject:string read FSubject write SetSubject;//主题 property Host:String read FHost write SetHost; property UserName:String read FUserName write SetUserName; Property Password:string read FPassword write SetPassword; property iPort:Integer read FiPort write SetiPort; property szPort:string read FszPort write SetszPort; property EState:EMailState read FEState write SetEState; property EmailCount:integer read FEmailCount write SetEmailCount; property SendingCount:integer read FSendingCount write SetSendingCount; property isSendedCount:integer read FisSendedCount write SetisSendedCount;
//构造函数 constructor TSendEmail.Create(UserName,From,PassWord,Host,MasterID:String;EmailAddressList,AddonsPath:TStringList;Port:Integer=25); begin SMTP := TIdSMTP.Create(); SMTP.HeloName :='Send';//indy bug防止被过滤 SMTP.OnWorkBegin := self.OnStartSendEmail; SMTP.OnWork := self.OnSendingEmail; SMTP.OnWorkEnd := Self.OnEndEmail; //SMTP.UseEhlo := False;为True才能通过验证 //self.MasterID := MasterID;
EMessage := TIdMessage.Create; FAddons := TStringList.Create; Femail := TStringList.Create;
self.SenderEmail := From; self.FHost := Host; self.FUserName := UserName; Self.FPassword := Password; self.FAddons.AddStrings(AddonsPath); self.FEmail.AddStrings(EmailAddressList); self.FiPort := Port; self.FszPort:= IntToStr(Port); end;
procedure TSendEmail.FillEMessage; var I: Integer; test :string; begin inherited; EMessage.ClearBody;//清空正文 EMessage.CharSet := 'gb2312';//字体集gb2312; EMessage.Subject := FSubject;//主题 // Emessage.ContentType :='text/html'; EMessage.Body.Text := Self.FEmailMsg;//消息正文 EMessage.From.Address :=FUserName;//发送人 EMessage.From.Name :=self.SenderEmail; EMessage.Recipients.EMailAddresses:=FEmail[0];//(FEmail);//收件人 //群发 填充bccList可以群发 for I := 1 to FEmail.Count - 1 do begin EMessage.BccList.Add.Address := FEmail[i]; end; //添加附件 //Emessage.ContentType :='multipart/mixed'; for I := 0 to FAddons.Count - 1 do begin if FileExists(Faddons[i]) then TIdAttachmentFile.Create(EMessage.MessageParts,FAddons[i]); end; end;
function TSendEmail.StartSendEmail:Boolean; var I: Integer; SendEmail:String; begin Result := False;
{ if not CheckEmail then raise Exception.Create('服务器验证失败'); } //填充信息 FillEMessage; SMTP.Host := self.Host; //主机 SMTP.Port := self.iPort; //端口 //SASL验证 SMTP.Username := self.UserName; SMTP.Password := self.Password; SMTP.AuthType := atDefault;//验证
//开始发送邮件 {SMTP.Username := FUserName; SMTP.Password := FPassword; SMTP.Host := FHost; SMTP.Port := FiPort; } if SMTP.Connected then SMTP.Disconnect(); try SMTP.Connect; except raise Exception.Create('连接失败,请检查网络是否连接'); end; try try if SMTP.Authenticate then //验证 begin SMTP.Send(EMessage); Result := True; end; except raise Exception.Create('认证失败,请检查用户名或密码是否正确'); end; finally SMTP.Disconnect(); end;
end;
|
请发表评论