Hola,
Por si te sirve de ayuda, adjunto esta clase para enviar emails desde una cuenta de gmail (puerto 465) que facilmente es adaptable a otro servicio de mail. No te olvides de incluir las librerias openSSL en la carpeta de tu ejecutable.
Código Delphi
[-]
unit cfs.Email.gmail;
interface
uses
System.SysUtils, System.Classes, IdTCPConnection, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTP, IdMessage, IdIOHandler,
IdBaseComponent, IdComponent, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdText, IdAttachmentFile;
type
TcfsGmail = class
private
FFromName: string;
FIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
FIdSMTP: TIdSMTP;
public
constructor Create(const UserName, Password, FromName: string);
destructor Destroy; override;
procedure Connect;
procedure Send(ToAddresses: array of string; const Subject, PlainBody: string; const HTMLBody: string = ''; const AttachmentFile: string = '');
end;
implementation
constructor TcfsGmail.Create(const UserName, Password, FromName: string);
begin
FFromName := FromName;
FIdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
FIdSSLIOHandlerSocket.SSLOptions.Method := sslvSSLv23;
FIdSSLIOHandlerSocket.SSLOptions.Mode := sslmClient;
FIdSSLIOHandlerSocket.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
FIdSMTP := TIdSMTP.Create(nil);
FIdSMTP.IOHandler := FIdSSLIOHandlerSocket;
FIdSMTP.UseTLS := utUseImplicitTLS;
FIdSMTP.AuthType := satDefault;
FIdSMTP.Host := 'smtp.gmail.com';
FIdSMTP.Port := 465;
FIdSMTP.Username := UserName;
FIdSMTP.Password := Password;
end;
destructor TcfsGmail.Destroy;
begin
if Assigned(FIdSMTP) then
begin
try
FIdSMTP.Disconnect;
except
end;
UnLoadOpenSSLLibrary;
FreeAndNil(FIdSMTP);
end;
if Assigned(FIdSSLIOHandlerSocket) then
FreeAndNil(FIdSSLIOHandlerSocket);
inherited;
end;
procedure TcfsGmail.Connect;
begin
FIdSMTP.Connect;
FIdSMTP.Authenticate;
end;
procedure TcfsGmail.Send(ToAddresses: array of string; const Subject, PlainBody: string; const HTMLBody: string = ''; const AttachmentFile: string = '');
var
IdMessage: TIdMessage;
IdText: TIdText;
Address: string;
AttachFileExist: Boolean;
MultipartAlternative: Boolean;
begin
if not FIdSMTP.Connected then
Connect;
IdMessage := TIdMessage.Create(nil);
try
IdMessage.From.Address := FIdSMTP.Username;
IdMessage.From.Name := FFromName;
for Address in ToAddresses do
begin
if Address <> '' then
IdMessage.Recipients.Add.Text := Address;
end;
IdMessage.Subject := Subject;
AttachFileExist := False;
if AttachmentFile <> '' then
AttachFileExist := FileExists(AttachmentFile);
MultipartAlternative := False;
if (PlainBody <> '') and (HTMLBody <> '') then
MultipartAlternative := True;
IdMessage.ContentType := 'multipart/alternative';
if AttachFileExist then
begin
if MultipartAlternative then
IdMessage.ContentType := 'multipart/related; type="multipart/alternative"'
else
IdMessage.ContentType := 'multipart/mixed';
end;
if MultipartAlternative and AttachFileExist then
begin
IdText := TIdText.Create(IdMessage.MessageParts);
IdText.ContentType := 'multipart/alternative';
end;
if PlainBody <> '' then
begin
IdText := TIdText.Create(IdMessage.MessageParts);
IdText.ContentType := 'text/plain; charset="UTF-8"';
IdText.Body.Text := PlainBody;
if MultipartAlternative and AttachFileExist then
IdText.ParentPart := 0;
end;
if HTMLBody <> '' then
begin
IdText := TIdText.Create(IdMessage.MessageParts);
IdText.ContentType := 'text/html; charset="UTF-8"';
IdText.Body.Text := HTMLBody;
if MultipartAlternative and AttachFileExist then
IdText.ParentPart := 0;
end;
if AttachFileExist then
TIdAttachmentFile.Create(IdMessage.MessageParts, AttachmentFile);
FIdSMTP.Send(IdMessage);
finally
IdMessage.Free;
end;
end;
end.