1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
function send_mail():Boolean;
var
FileName : String;
begin
try
Result := False;
//FileName := 'myfile.txt';
//Setup SMTP
Form1.IdSMTP1 := Form1.IdSMTP1.Create(nil);
Form1.IdSMTP1.Host := 'smtp.live.com';
Form1.IdSMTP1.Port := 587; //Default email port
Form1.IdSMTP1.Username := 'mon_mail@hotmail.fr'; // SMTP user name
Form1.IdSMTP1.Password := 'secret'; //
Form1.IdMEssage1.From.Address := 'mon_mail@hotmail.fr';
Form1.IdMEssage1.Recipients.EMailAddresses := 'mon_mail@hotmail.fr';
Form1.IdMEssage1.Subject := 'Test Email from Delphi XE2';
Form1.IdMEssage1.Body.Text := 'Hi! This is test email from delphi XE2';
//Attach a file
//if FileExists(FileName) then TIdAttachmentFile.Create(MailMessage.MessageParts, FileName);
//Send email
try
try
Form1.IdSMTP1.Connect;
Form1.IdSMTP1.Send(Form1.IdMEssage1);
Result := True;
except
on E:Exception do
begin
ShowMessage('Cannot send E-Mail: ' + E.Message);
Result := False;
end;
end;
finally
if Form1.IdSMTP1.Connected then Form1.IdSMTP1.Disconnect;
end;
except
on E : Exception do
begin
ShowMessage('Error in the function SendEmailDelphi: ' + E.Message);
Result := False;
end;
end;
end; |
Partager