• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi工作笔记利用indy10发送邮件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


//邮件发送
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;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
初学delphi应该看什么书发布时间:2022-07-18
下一篇:
delphi和INNOSETUP文件关联操作发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap