Ti riporto il codice che ho utilizzato per inviare una mail con allegato.
codice:
procedure TForm1.bbSendClick(Sender: TObject);
var
FilePath: string;
Recipient: TIdEmailAddressItem;
begin
// SMTP setup
if cbMailLogon.Checked then
IndySMTP.AuthenticationType := atLogin
else
IndySMTP.AuthenticationType := atNone;
IndySMTP.Host := edHost.Text;
IndySMTP.Password := edPassword.Text;
IndySMTP.Port := StrToIntDef(edPort.Text, 25);
IndySMTP.Username := edUser.Text;
// Message setup
IndyMsg.Body.Clear;
IndyMsg.Body.Add('Test');
IndyMsg.Organization := edOrganization.Text;
IndyMsg.Subject := 'Simple mailer test';
IndyMsg.From.Address := edSenderAddress.Text;
IndyMsg.From.Name := edSenderName.Text;
Recipient := IndyMsg.Recipients.Add;
Recipient.Address := edToAddress.Text;
Recipient.Name := edToName.Text;
FilePath := ExtractFilePath(Application.ExeName) + 'data\demo.pdf';
TIdAttachment.Create(IndyMsg.MessageParts, FilePath);
IndySMTP.Connect;
try
IndySMTP.Send(IndyMsg);
finally
IndySMTP.Disconnect;
end;
end;
I componenti Indy sono dotati anche di una Guida in linea, che ho consultato per poter capire come funzionava il componente.
Nel codice, i dati per la spedizione del messaggio vengono presi ovviamente da controlli Edit che si trovano sul form (non posso riportare in chiaro i dati privati); il componente IndyMsg è di tipo TIdMessage e può essere inizializzato con i dati del messaggio da inviare, mentre il componente IndySMTP è di tipo TIdSMTP, che permette all'applicazione di fare da client per un server di posta SMTP allo scopo di inviare una email.
Per il significato delle singole proprietà e metodi utilizzati, ti rimando alla Guida in linea.
Ciao!