发邮件(50分)

  • 主题发起人 主题发起人 恒翔
  • 开始时间 开始时间

恒翔

Unregistered / Unconfirmed
GUEST, unregistred user!
老大们,有没有现成的发电子邮件的源码阿,给贴出来让我参考一下阿,谢谢了
 
在线程里用idsmtp发邮件.我自用的,没有问题.

procedure TMailThread.Execute;
var IdSMTP:TIdSMTP;
IdMsg:TIdmessage;
att:string;
Stop:boolean;
begin
{ Place thread code here }
FreeOnTerminate := True;

synchronize(LoadFromMain);
Idsmtp:=TIdsmtp.Create(nil);
IdMsg:=TIdmessage.Create(nil);

try

//构造邮件体
IdMsg.Subject := FSubject;
IdMsg.Body.Text := FContent;
IdMsg.From.Address := FMail;
IdMsg.Recipients.EMailAddresses := GetEmail(FToMail);
att:=FAttachment;
if FileExists(att) then TIdAttachment.Create(IdMsg.MessageParts, att);

//填充SMTP
IdSMTP.Host := GetHost;
IdSMTP.Port := 25;
IdSMTP.Username := GetUsername;
IdSMTP.Password := GetPS;

//连接到SMTP服务器
try
IdSMTP.Connect;
except
SendForm.Sending := false;
exit;
end;

//验证身分
if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN')<>-1) then
begin
IdSMTP.AuthenticationType :=atLogin;
try
if not IdSMTP.Authenticate then //未通过验证
begin
if IdSMTP.Connected then IdSMTP.Disconnect;
exit;
end;
except
if IdSMTP.Connected then IdSMTP.Disconnect;
exit;
end;
end;

//发送信件
try
IdSMTP.Send(IdMsg);
synchronize(DoSendLogTrue);
except
synchronize(DoSendLogFalse);
if IdSMTP.Connected then IdSMTP.Disconnect;
end;
if IdSMTP.Connected then IdSMTP.Disconnect;
finally
IdSMTP.Free;
IdMsg.Free;
end;
end;
 
带附件
procedure SendMail;
const ErrJpg ='ErrJpg.jpg';
var
FileName :string;
p :TLoginEmailServer;
f :TStrings;
fromEmail,toEmail,ccListEmail,Subject :String;
i :Integer;
begin
FileName :=GetAppPath + ErrJpg;
if FileExists(FileName) then DeleteFile(FileName);
try
CapFullScreen(FileName);
Subject :='错误信息';
with p do
begin
SMTPHost :='*.*.com.cn';
SMTPPort :=25;
Username :='*@*.com.cn';
Password :='****';
SmtpAuthType :=1;
end;

f :=TStringList.Create;
try
f.Add(FileName);
fromEmail :='*@*.com.cn';
toEmail :='*@*.com.cn';
ccListEmail :='';
Subject :='错误信息';
i :=SendEmail(p,Memo1.Lines,fromEmail,toEmail,Subject,'',ccListEmail,f);
if i<>0 then MessageBox(Handle,'邮件发送失败!','错误',MB_OK +MB_ICONERROR)
else MessageBox(Handle,'邮件发送成功!','提示',MB_OK +MB_ICONINFORMATION);
finally
FreeAndNil(f);
end;

finally
if FileExists(FileName) then DeleteFile(FileName);
end;
 
iseek老大,你那里的几个过程是怎么写的阿。LoadFromMain,DoSendLogTrue,DoSendLogFalse,这个里面都是什么内容阿
 
LoadFromMain是从主窗体取一些相关数据
DoSendLogTrue是发送成功记录
DoSendLogFalse是发送失败记录
这些自定义过程你可以不用管,自己按顺序填充邮件体、用户名、密码什么的就可以发送邮件了.当然你完全可以不必在线程里发送的.
 
procedure TMailThread.Execute;
var IdSMTP:TIdSMTP;
IdMsg:TIdmessage;
att:string;
Stop:boolean;
begin
{ Place thread code here }
FreeOnTerminate := True;

synchronize(LoadFromMain);
Idsmtp:=TIdsmtp.Create(nil);
IdMsg:=TIdmessage.Create(nil);

try

//构造邮件体
IdMsg.Subject := FSubject;
IdMsg.Body.Text := FContent;
IdMsg.From.Address := FMail;
IdMsg.Recipients.EMailAddresses := GetEmail(FToMail);
att:=FAttachment;
if FileExists(att) then TIdAttachment.Create(IdMsg.MessageParts, att);

//填充SMTP
IdSMTP.Host := GetHost;
IdSMTP.Port := 25;
IdSMTP.Username := GetUsername;
IdSMTP.Password := GetPS;

//连接到SMTP服务器
try
IdSMTP.Connect;
except
SendForm.Sending := false;
exit;
end;

//验证身分
if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN')<>-1) then
begin
IdSMTP.AuthenticationType :=atLogin;
try
if not IdSMTP.Authenticate then //未通过验证
begin
if IdSMTP.Connected then IdSMTP.Disconnect;
exit;
end;
except
if IdSMTP.Connected then IdSMTP.Disconnect;
exit;
end;
end;

//发送信件
try
IdSMTP.Send(IdMsg);
synchronize(DoSendLogTrue);
except
synchronize(DoSendLogFalse);
if IdSMTP.Connected then IdSMTP.Disconnect;
end;
if IdSMTP.Connected then IdSMTP.Disconnect;
finally
IdSMTP.Free;
IdMsg.Free;
end;
end;
这个在执行的时候报错啊,503 bad sequence of commands.已经显示连接成功了.
 
SakEmail 下个里面demo很详细
 
谢谢各位老大了
 
后退
顶部