我写了一个发邮件的函数,包你满意
type
TLoginEmailServer=record
SMTPHost:string;
SMTPPort:integer;
Username:string;
Password:string;
SmtpAuthType:integer;
end;
function SendEmail(poSMTPServer:TLoginEmailServer;poBody:Tstrings;psFromEmial,
psToEmail,psSubject:string;psContentType:string;
CCToEmail:string;poAttachmentPath:TStrings):integer;
var
loIdMsgSend: TIdMessage;
loSMTP: TIdSMTP;
i:integer;
begin
Result:=3;
loIdMsgSend:=nil;
loSMTP:=nil;
try
loIdMsgSend:=TIdMessage.Create(nil);
loSMTP:=TIdSMTP.Create(nil);
with loIdMsgSend do
begin
ContentType:=psContentType;
From.Text := psFromEmial;
ReplyTo.EMailAddresses := psFromEmial;
Recipients.EMailAddresses := psToEmail;
CCList.EMailAddresses:=CCToEmail;
Subject := psSubject;
Priority := mpHigh;
ReceiptRecipient.Text := '';
Body.Assign(poBody);
if Assigned(poAttachmentPath) then
begin
for i := 0 to poAttachmentPath.Count-1 do
begin
TIdAttachment.Creat(loIdMsgSend.MessageParts,poAttachmentPath.Strings);
end;
end;
end;
with loSMTP do
begin
Host :=poSMTPServer.SMTPHost;
Port := poSMTPServer.SMTPPort;
if poSMTPServer.SmtpAuthType=1 then
AuthenticationType:=atLogin
else
AuthenticationType:=atNone;
Username := poSMTPServer.Username;
Password := poSMTPServer.Password;
try
Connect;
Send(loIdMsgSend);
except
result:=2;
exit;
end;
Result:=0;
finally
loIdMsgSend.Free;
loSMTP.Free;
end;
end;