uses
IdAttachmentFile, IdText, IdAttachment, IdMessageParts;
function TForm2.prepareMessage(AToMail, ASubject: string; ABody: TStrings; AFileStream: TStream; AFileName: string): Boolean;
var
CCListMail: string;
HTMLFormat: Boolean;
begin
Result := False;
HTMLFormat := true;
CCListMail := '';
with IdMessage do begin
clear;
Body.clear;
Recipients.EMailAddresses := AToMail;
From.Address := FromMail;
From.Name := FromNombre;
ReplyTo.Add.Address := FromMail;
if (CCListMail <> '') then
BccList.EMailAddresses := CCListMail;
Subject := ASubject;
with TIdAttachmentFile.Create(MessageParts) do begin
AFileStream.Seek(0, 0);
LoadFromStream(AFileStream);
FileName := ExtractFileName(AFileName);
end;
if HTMLFormat then begin
ContentType := 'multipart/mixed';
with TIdText.Create(MessageParts, nil) do begin
ContentType := 'text/html';
Body.AddStrings(ABody);
end;
Body.Add('This is a multi-part message in MIME format.');
end
else begin
ContentType := 'text/plain';
Body.AddStrings(ABody);
end;
end;
result := True;
end;
function TForm2.SendMail: Boolean;
begin
result := false;
getSMTPConfig;
IdSMTP.Connect;
if IdSMTP.connected then
try
IdSMTP.Send(IdMessage);
result := True;
finally
IdSMTP.Disconnect(true);
end;
end;
procedure LoadBitmapIntoStream(stream: TStream; AFileName: string);
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.create;
try
Bitmap.LoadFromFile(AFileName);
Bitmap.SaveToStream(stream);
finally
Bitmap.Free;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
Email, asunto: string;
FileStream: TStream;
MailFileName: string;
begin
FileStream := TMemoryStream.Create;
try
LoadBitmapIntoStream(FileStream, 'c:\a\icon_title2.bmp');
Email := Destinatario; asunto := 'El asunto del correo. Esto es una prueba de correo';
MailFileName := 'MyImagen.bmp';
if not prepareMessage(Email, asunto, memo1.Lines, FileStream, MailFileName) then
showMessage('Error en el proceso de preparacion del email')
else if not SendMail then
showMessage('No se pudo enviar el EMAIL')
else
ShowMessage('Correo enviado con exito');
finally
FileStream.Free;
end;
end;