Indy 10 - Invio email con allegati

un modo semplice (valido per Windows e Linux), utilizzando le Librerie Indy

unico neo delle Indy che hanno poca compatibilita' verso le rel. precedenti quando fanno
una nuova versione.

Codice: [Seleziona]

rel. 9
uses IdMessage
TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text);

rel.10
uses IdAttachmentFile
 TIdAttachmentFile.Create(IdMessage.MessageParts, EdAllegato.Text);

[b]IdAttachmentFile[/b] specifico della versione 10 di Indy, hanno modificato
le classi di gestione allegati presenti fino alla rel. 9 nella unit IdMessage


TIdSMTP -> pannello Indy Clients Protocols (nz)
TIdMessage -> pannello Indy Misc Protocols

aggiungere nelle uses IdAttachmentFile

EdMittente, EdDestinatario, EdConoscenza, EdOggetto,
EdCorpo, EdAllegato: sono normali TEdit
MemoStato -> componente standard TMemo

nel componete TIdSmtp ATTIVARE L'Evento OnStatus

Codice: [Seleziona]

//si possono visualizzare le operazioni durante la connessione
//al server di posta
//
procedure Tfrm_InvioMail.IdSMTPStatus(ASender: TObject;
  const AStatus: TIdStatus; const AStatusText: string);
begin
  MemoStato.Lines.Insert(0,'Status: ' + AStatusText);
end;        

procedure Tfrm_InvioMail.act_InvioMailExecute(Sender: TObject);
begin
  MemoStato.Clear;

  IdSMTP.Host := EdHost.Text;  //indirizzo posta uscita del server
  IdSMTP.Port := 25;           //porta utilizzata dal servizio email

  //configurazione messaggio mail
  IdMessage.From.Address := EdMittente.Text;
  IdMessage.Recipients.EMailAddresses := EdDestinatario.Text + ',' + EdConoscenza.Text;

  IdMessage.Subject := EdOggetto.Text;
  IdMessage.Body.Text := EdCorpo.Text;

  //per selezioneare l'allegato si puo' utilizzare il compponente standard OpenDialog
  //
  if FileExists(EdAllegato.Text) then
    TIdAttachmentFile.Create(IdMessage.MessageParts, EdAllegato.Text);

  //invio mail
  try
    try
      IdSMTP.Connect;           //connessione
      IdSMTP.Send(IdMessage);
    except on E:Exception do
      //
      //segnalazione eventuale problema di connessione
      MemoStato.Lines.Insert(0, 'ERROR: ' + E.Message);
    end;
  finally
    //segnalazione invio effettuato
     MemoStato.Lines.Insert(0, 'ERROR: ' + E.Message);

    if IdSMTP.Connected then IdSMTP.Disconnect;  //disconnessione dal server di posta
  end;
end;


SMF 2.0.8 | SMF © 2011, Simple Machines
Privacy Policy
SMFAds for Free Forums
TinyPortal © 2005-2012

Go back to article