* * * *

Privacy Policy

Blog italiano

Clicca qui se vuoi andare al blog italiano su Lazarus e il pascal.

Forum ufficiale

Se non siete riusciti a reperire l'informazione che cercavate nei nostri articoli o sul nostro forum vi consiglio di visitare il
Forum ufficiale di Lazarus in lingua inglese.

Lazarus 1.0

Trascinare un file nel programma
DB concetti fondamentali e ZeosLib
Recuperare codice HTML da pagina web
Mandare mail con Lazarus
Stabilire il sistema operativo
Esempio lista in pascal
File INI
Codice di attivazione
Realizzare programmi multilingua
Lavorare con le directory
Utilizzare Unità esterne
TTreeView
TTreeview e Menu
Generare controlli RUN-TIME
LazReport, PDF ed immagini
Intercettare tasti premuti
Ampliare Lazarus
Lazarus e la crittografia
System Tray con Lazarus
UIB: Unified Interbase
Il file: questo sconosciuto
Conferma di chiusura di un applicazione
Liste e puntatori
Overload di funzioni
Funzioni a parametri variabili
Proprietà
Conversione numerica
TImage su Form e Panel
Indy gestiore server FTP lato Client
PopUpMenu sotto Pulsante (TSpeedButton)
Direttiva $macro
Toolbar
Evidenziare voci TreeView
Visualizzare un file Html esterno
StatusBar - aggirare l'errore variabile duplicata
Da DataSource a Excel
Le permutazioni
Brute force
Indy 10 - Invio email con allegati
La gestione degli errori in Lazarus
Pascal Script
Linux + Zeos + Firebird
Dataset virtuale
Overload di operatori
Lavorare con file in formato JSON con Lazarus
Zeos ... dietro le quinte (prima parte)
Disporre le finestre in un blocco unico (come Delphi)
Aspetto retrò (Cmd Line)
Lazarus 1.0
Come interfacciare periferica twain
Ubuntu - aggiornare free pascal e lazarus
fpcup: installazioni parallele di lazarus e fpc
Free Pascal e Lazarus sul Raspberry Pi
Cifratura: breve guida all'uso dell'algoritmo BlowFish con lazarus e free pascal.
Creare un server multithread
guida all'installazione di fpc trunk da subversion in linux gentoo
Indice
DB concetti fondamentali e connessioni standard
Advanced Record Syntax
DB concetti fondamentali e DBGrid
DB concetti fondamentali e TDBEdit, TDBMemo e TDBText
Advanced Record Syntax: un esempio pratico
Superclasse form base per programmi gestionali (e non)
Superclasse form base per programmi gestionali (e non) #2 - log, exception call stack, application toolbox
Superclasse form base per programmi gestionali (e non) #3 - traduzione delle form
Superclasse form base per programmi gestionali (e non) #4 - wait animation
Un dialog per la connessione al database:TfmSimpleDbConnectionDialog
Installare lazarus su mac osx sierra
immagine docker per lavorare con lazarus e free pascal
TDD o Test-Driven Development
Benvenuto! Effettua l'accesso oppure registrati.
Maggio 22, 2024, 07:59:48 pm

Inserisci il nome utente, la password e la durata della sessione.

418 Visitatori, 0 Utenti

Autore Topic: Problema mail  (Letto 25218 volte)

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Problema mail
« il: Maggio 13, 2012, 02:24:31 pm »
Stavo seguendo l'how to di xinyiman rigardo al mandare email con lazarus usando synapse.
Dopo aver scaricato synapse e messo tutte le librerie nella cartella ho creato mylibmail ed non volendo usare i form ho tolto tutte le librerie tranne quelle per il socket, class e sysutils.
Ho dovuto quindi togliere anche un application.processmessages
(In qualunque caso quelle librerie non le trovava, qualcuno mi darebbe anche delucidazioni in merito? :) grazie).

Ho quindi creato un programma che senza form manda un email a me stesso da me stesso tramite hotmail (smtp.live.com, porta 25, teoricamente ssl attiva)
Però da errore, sul login e logout con ssl=true e solo nel logout con ssl=false.
Posto libreria
Codice: [Seleziona]
unit MyLibMail;
{$mode objfpc}{$H+}
interface

uses
    blcksock, smtpsend, pop3send, ssl_openssl //librerie di synapse
    , MimePart, MimeMess,SynaChar
    ,Classes, SysUtils;

  type
      MyMailObject=class
      private
             StrList: TStringList;
             Mime : TMimeMess;
             SMTP : TSMTPSend;
             MyUser, MyPassword, MySMTPHost, MyPorta, MyNome, MyFrom, MyRisposta, MyOggetto, MyCorpoMail: string;
             MySicurezza: integer;
             MyTesto, MyToList, MyCCList, MyAllegatoList:TStringList;
             tmp : TMemoryStream;
             function RetNomeFile(PathFile: string): string;
      public
            constructor Create();
            destructor Destroy();
            function MySendMail(var Errore: string): boolean;
            procedure SetMyUser(UserP: string);
            procedure SetMyPassword(PasswordP: string);
            procedure SetMySMTPHost(SMTPHostP: string);
            procedure SetMyPorta(PortaP: string);
            procedure SetSSLTLS(SicurezzaP: boolean);
            procedure SetMyNome(NomeP: string);
            procedure SetMyFrom(FromP: string);
            procedure SetMyRisposta(RispostaP: string);
            procedure AddDestinatario(DestP: string);
            procedure AddMyAllegatoList(AllegatoListP: string);
            procedure SetMyOggetto(OggettoP: string);
            procedure AddRowCorpoMail(RigaP: string);
      end;

implementation
              constructor MyMailObject.Create();
              begin
                   StrList := TStringList.Create;
                   MyToList := TStringList.Create;
                   MyTesto := TStringList.Create;
                   MyCCList := TStringList.Create;
                   MyAllegatoList := TStringList.Create;
                   Mime := TMimeMess.Create;
                   SMTP := TSMTPSend.Create;
                   SetMyUser('');
                   SetMyNome('');
                   SetMyFrom('');
                   SetMyPassword('');
                   SetMyPorta('25');
                   SetMySMTPHost('');
                   SetMyOggetto('');
                   SetSSLTLS(false);
                   SetMyRisposta('');
              end;

              Destructor MyMailObject.Destroy();
              begin
                   //Free finale degli oggetti
                   StrList.Free; //Prova
                   MyTesto.Free;
                   MyToList.Free;
                   MyCCList.Free;
                   MyAllegatoList.Free;
                   Mime.Free; //Prova
                   smtp.Free;
              end;

              procedure MyMailObject.SetMyUser(UserP: string);
              begin
                   MyUser:=UserP;
              end;

              procedure MyMailObject.SetMyPassword(PasswordP: string);
              begin
                   MyPassword:=PasswordP;
              end;

              procedure MyMailObject.SetMySMTPHost(SMTPHostP: string);
              begin
                   MySMTPHost:=SMTPHostP;
              end;

              procedure MyMailObject.SetMyPorta(PortaP: string);
              begin
                   MyPorta:=PortaP;
              end;

              procedure MyMailObject.SetSSLTLS(SicurezzaP: boolean);
              begin
                   if SicurezzaP=TRUE then
                      MySicurezza:=2
                   else
                      MySicurezza:=1;
              end;

              procedure MyMailObject.SetMyNome(NomeP: string);
              begin
                   MyNome:=NomeP;
              end;

              procedure MyMailObject.SetMyFrom(FromP: string);
              begin
                   MyFrom:=FromP;
              end;

              procedure MyMailObject.SetMyRisposta(RispostaP: string);
              begin
                   MyRisposta:=RispostaP;
              end;

              procedure MyMailObject.AddDestinatario(DestP: string);
              begin
                   MyToList.Add(DestP);
              end;

              procedure MyMailObject.AddMyAllegatoList(AllegatoListP: string);
              begin
                   MyAllegatoList.Add(AllegatoListP);
              end;

              procedure MyMailObject.SetMyOggetto(OggettoP: string);
              begin
                   MyOggetto:=OggettoP;
              end;

              procedure MyMailObject.AddRowCorpoMail(RigaP: string);
              begin
                   StrList.Add(RigaP);
              end;

              function MyMailObject.MySendMail(var Errore: string): boolean;
              var
                 ret: boolean;
                 k: integer;
              begin
                   ret:=FALSE;
                   Errore:='';
                try
                  //====================================
                  //If authorization is required, then fill in username
                  smtp.UserName := MyUser;
                  //Specify user's password
                  smtp.Password := MyPassword;
                  //Specify target server IP (or symbolic name)
                  smtp.TargetHost := MySMTPHost;
                  //Specify target server port
                  if (Trim(MyPorta) = '') then begin
                    //Porta non impostata
                    smtp.TargetPort := '25'; //Porta di default
                  end
                  else begin
                     smtp.TargetPort := MyPorta;
                  end;
                  //Enable SSL|TLS protocols
                  smtp.autoTLS := True;
                  //smtp.Timeout := 60;

                  if (MySicurezza = 2) then begin
                    //SSL/TLS
                    smtp.FullSSL := True;
                  end;

                  //Connect to SMTP server
                  if not smtp.Login() then Errore:=Concat(Errore, #13#10 , 'SMTP ERROR: Login:' , smtp.EnhCodeString);
                  //if not smtp.StartTLS () then showmessage('SMTP ERROR: StartTLS:' + smtp.EnhCodeString);

                  //If you successfully pass authorization to the remote server
                  if smtp.AuthDone then begin
                    //Corpo mail
                    for k := 0 to (MyTesto.Count - 1) do begin
                      StrList.Add(MyTesto[k]);
                    end;
                    //Mime.Header.CharsetCode := UTF_8; //Da' errore
                    Mime.Header.From := MyNome + ' <' + MyFrom + '>';
                    //E-mail per rispondere (eventuale)
                    if (Trim(MyRisposta) = '') then begin
                      //E-Mail di risposta non indicata
                      Mime.Header.ReplyTo := MyFrom; //Indirizzo di risposta = indirizzo mittente
                    end
                    else begin
                      //E-Mail di risposta indicata
                      Mime.Header.ReplyTo := MyRisposta;
                    end;
                    //To
                    for k := 0 to (MyToList.Count - 1) do begin
                      Mime.Header.ToList.Add(Trim(MyToList[k]));
                    end;
                    //CC (eventuale)
                    if (MyCCList.Count > 0) then begin
                      for k := 0 to (MyCCList.Count - 1) do begin
                        Mime.Header.CCList.Add(Trim(MyCCList[k]));
                      end;
                    end;
                    //Oggetto
                    Mime.Header.Subject := MyOggetto;
                    //Corpo mail
                    Mime.AddPartMultipart(MyCorpoMail, Nil);
                    Mime.AddPartText(StrList, Mime.MessagePart);
                    //Eventuali allegati
                    if (MyAllegatoList.Count > 0) then begin
                      //Ci sono allegati
                      {//Questo blocco funziona correttamente, ma non e' possibile impostare il nome degli allegati che vengono poi visualizzati dal destinatario
                      for k := 0 to (MyAllegatoList.Count - 1) do begin
                        hdAttach := Trim(MyAllegatoList[k]);
                        if (hdAttach <> '') then begin
                          Mime.AddPartBinaryFromFile(hdAttach, Mime.MessagePart);
                        end;
                      end;
                      }
                      tmp := TMemoryStream.Create;
                      for k := 0 to (MyAllegatoList.Count - 1) do begin
                        try
                          tmp.Clear; //Cmq. non sembra necessario
                          tmp.LoadFromFile(Trim(MyAllegatoList[k]));
                          Mime.AddPartBinary(tmp, RetNomeFile(MyAllegatoList[k]), Mime.MessagePart); //Nome da visualizzare allegato
                        finally
                          //tmp.Free;
                        end;
                      end;
                      tmp.Free;
                    end;
                    //Codifica messaggio
                    Mime.EncodeMessage;
                    //Invio: From
                    if not SMTP.MailFrom(MyFrom, Length(Mime.Lines.Text)) then exit;
                    //Invio: To
                    for k := 0 to (MyToList.Count - 1 ) do begin
                      if not SMTP.MailTo(Trim(MyToList[k])) then exit;
                    end;
                    //Invio: CC
                    if (MyCCList.Count > 0) then begin
                      //Ci sono indirizzi CC
                      for k := 0 to (MyCCList.Count - 1) do begin
                        if not SMTP.MailTo(Trim(MyCCList[k])) then exit;
                      end;
                    end;
                    //Invio: Corpo messaggio + eventuali allegati
                    if not SMTP.MailData(Mime.Lines) then exit;
                  end;
                  //Logout
                  if not smtp.Logout() Then
                    Errore:=Concat(Errore, #13#10 , 'SMTP ERROR: Logout:' , smtp.EnhCodeString);
                  //Se arrivati qui tutto OK
                  ret := True; //OK
                  Result:=ret;
                finally
                  //Processa messaggi
                  {Application.ProcessMessages;}
                end;
              end;

              function MyMailObject.RetNomeFile(PathFile: string): string;
              var
                 car, car2, ret: string;
                 i: integer;
              begin
                   ret:='';
                   car:='/';
                   {$IFDEF WIN32}
                            car:='\';
                   {$ENDIF}
                   for i:=1 to Length(PathFile) do
                   begin
                        car2:=PathFile[i];
                        if car2=car then
                        begin
                             ret:='';
                        end
                        else
                        begin
                             ret:=Concat(ret, car2);
                        end;
                   end;
                   RetNomeFile:=ret;
              end;

end.
                                                                         

e programma

Codice: [Seleziona]
program mail;
uses MyLibMail;
var
   app: MyMailObject;
   Errore: string;
begin
     app:=MyMailObject.Create;
     {setto i parametri}
     app.SetMyUser('gio.sch@hotmail.it');
     app.SetMyNome('gio.sch');
     app.SetMyFrom('gio.sch@hotmail.it');
     app.SetMyPassword('********');
     app.SetMyPorta('25'); //set to hotmail
     app.SetMySMTPHost('smtp.live.com'); //set to hotmail
     app.SetMyOggetto('Log');
     //app.SetSSLTLS(TRUE);
     app.AddDestinatario('gio.sch@hotmail.it');

     //app.AddMyAllegatoList('c:\1.txt');
     //app.AddMyAllegatoList('c:\2.txt');

     app.AddRowCorpoMail('PIPPO');
     app.AddRowCorpoMail('PLUTO2');

     { invio la mail }
     app.MySendMail(Errore);
     writeln(errore);
     { libero la memoria }
     app.Destroy();
     readln;
end.               

Spero sappiate aiutarmi a capire dove sbaglio :)
« Ultima modifica: Maggio 13, 2012, 04:06:42 pm da giosch »

xinyiman

  • Administrator
  • Hero Member
  • *****
  • Post: 3253
  • Karma: +12/-0
Re:Problema mail
« Risposta #1 il: Maggio 13, 2012, 09:06:40 pm »
Per capire, qual'è errore?!
Riporta l'errore esatto! Comunque la mail te la manda?!
Ieri è passato, domani è futuro, oggi è un dono...

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Re:Problema mail
« Risposta #2 il: Maggio 13, 2012, 09:54:33 pm »
con ssl false
scrive su prompt

SMTP ERROR: Login:Success-Other undefined Status
SMTP ERROR: Logout:???-Other undefined Status

con ssl true

SMTP ERROR: Login:???-Other undefined Status
SMTP ERROR: Logout:???-Other undefined Status

Cosa sbaglio? L'uso della tua unit o i dati per l'email? o cos'altro?
Devo dire che non sono molto ferrato sull'uso di protocolli per la rete...

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:Problema mail
« Risposta #3 il: Maggio 13, 2012, 10:06:38 pm »
Scusa la domanda banale, ma usi hotmail.it.
Se ricordo bene, l'smtp e il pop sono a pagamento su hotmail.
Credo che dia il 2 di picche per questo motivo.
Se hai l'abbonamento ... non so cosa dirti, aspettiamo il guru di turno ;)

EDIT:
Aggiungo un'altra osservazione ... ti stai mandando la mail da solo?
Non so se hotmail permetta di mandare delle mail con FROM e TO uguali ...
Non ricordo le restrizioni del protocollo. Forse xinyiman è in grado di dare delucidazioni in merito ;)
« Ultima modifica: Maggio 13, 2012, 10:11:07 pm da Stilgar »
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

xinyiman

  • Administrator
  • Hero Member
  • *****
  • Post: 3253
  • Karma: +12/-0
Re:Problema mail
« Risposta #4 il: Maggio 14, 2012, 07:57:22 am »
Sinceramente io hotmail non l'ho mai usato. Ho sempre mandato mail da GMail. Quindi delle sue varie restrizioni dovrebbe magari informarsi sul sito di hotmail e vedere se trova notizie in questo senso.
Ieri è passato, domani è futuro, oggi è un dono...

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Re:Problema mail
« Risposta #5 il: Maggio 14, 2012, 03:10:49 pm »
Ok, ho creato un account gmail e ho settato la porta su 465 e messo ssl true
Codice: [Seleziona]
program mail;
uses MyLibMail;
var
   app: MyMailObject;
   Errore: string;
begin
     app:=MyMailObject.Create;
     {setto i parametri}
     app.SetMyUser('xxxxxxxxx@gmail.com');
     app.SetMyNome('xxxxxxxxx@gmail.com');
     app.SetMyFrom('xxxxxxxxx@gmail.com');
     app.SetMyPassword('xxxxxxxx');
     app.SetMyPorta('465'); //set to gmail
     app.SetMySMTPHost('smtp.gmail.com'); //set to gmail
     app.SetMyOggetto('Log');
     app.SetSSLTLS(TRUE);
     app.AddDestinatario('gio.sch@hotmail.it');

     //app.AddMyAllegatoList('c:\1.txt');
     //app.AddMyAllegatoList('c:\2.txt');

     app.AddRowCorpoMail('PIPPO');

     { invio la mail }
     app.MySendMail(Errore);
     writeln(errore);
     { libero la memoria }
     app.Destroy();
     readln;
end.                         


però continua a dirmi lo stesso errore (il secondo)
Codice: [Seleziona]
SMTP ERROR: Login: ???-Other undefined Status
SMTP ERROR: Logout: ???-Other undefined Status

Legolas

  • Global Moderator
  • Sr. Member
  • *****
  • Post: 366
  • Karma: +3/-0
Re:Problema mail
« Risposta #6 il: Maggio 14, 2012, 03:32:01 pm »
Dico la cosa più banale, ma spesso è quella che può sfuggire (almeno a me capita spesso...  :'( ): non è che c'è qualche firewall che impedisce l'invio?

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:Problema mail
« Risposta #7 il: Maggio 14, 2012, 03:35:00 pm »
Legolas, basta che non inizi a smontare il differenziale di casa perchè è staccata la presa della lavatrice ;)
In effetti le impostazioni si Windows potrebbero rompere le scatole.
Ma chi ne capisce di + è xinyiman;)
« Ultima modifica: Maggio 14, 2012, 04:16:37 pm da Stilgar »
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Re:Problema mail
« Risposta #8 il: Maggio 14, 2012, 03:48:35 pm »
esssendo profonamente ignorante in materia, non credo che il firewall di windows blocchi la porta per smtp....
Ma comunque, qualche soluzione? A vuoi funziona questo algoritmo?

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:Problema mail
« Risposta #9 il: Maggio 14, 2012, 04:18:53 pm »
In linea di principio, il firewall di Window, blocca l'accesso ad una porta, ma la regola è impostata per programma.
Puoi anche mettere regole più complesse basate sugli IP, ma credo esuli dal problema.
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Re:Problema mail
« Risposta #10 il: Maggio 14, 2012, 04:33:56 pm »
scusa, ma blocca tutte le porte? Non solo quelle senza un client ? Comunque non vorrei farlo manualmente... É possibile fare in modo che il mio programma apra la porta e comunichi col server smtp ?

Stilgar

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2382
  • Karma: +10/-0
Re:Problema mail
« Risposta #11 il: Maggio 14, 2012, 04:51:23 pm »
In genere quando un programma prova ad accedere (da XP in poi) ad una porta, Windows chiede cosa si deve fare.
Bloccare o consentire.
Con w2k non ricordo come si comportava ... :( Sorry
Al mondo ci sono 10 tipi di persone ... chi capisce il binario e chi no.

Legolas

  • Global Moderator
  • Sr. Member
  • *****
  • Post: 366
  • Karma: +3/-0
Re:Problema mail
« Risposta #12 il: Maggio 14, 2012, 04:57:02 pm »
Spulciando google ho trovato questo:
http://www.lazarusforum.de/viewtopic.php?p=26751#p26751

Il colpevole in questo caso è l'antivirus McAfee. Magari prova a disattivare temporaneamente il tuo antivirus e vedi se funge

giosch

  • Newbie
  • *
  • Post: 23
  • Karma: +0/-0
Re:Problema mail
« Risposta #13 il: Maggio 14, 2012, 05:30:58 pm »
Disattivando realtime protecion di avira non cambia nulla. Ho provato con un account email.it (quindi smtp.email.it) ma sempre lo stesso errore...
Quando lo eseguo non escono finestre di richieste da parte del firewall, quindi mi sembra strano che lo blocchi...
Idee? o meglio, Soluzioni? ;)

EDIT
aggiunta manualmente la regola mail.exe come eccezione nel firewall  ma non va comunque.
« Ultima modifica: Maggio 14, 2012, 05:37:22 pm da giosch »

nomorelogic

  • Global Moderator
  • Hero Member
  • *****
  • Post: 2879
  • Karma: +20/-4
Re:Problema mail
« Risposta #14 il: Maggio 14, 2012, 05:55:54 pm »
so che usando SSL/TLS bisogna specificare anche il metodo di autenticazione (pass normale/cifrata/kerberos/bla bla bla)
già controllato se può essere quello?
Imagination is more important than knowledge (A.Einstein)

 

Recenti

How To

Utenti
  • Utenti in totale: 789
  • Latest: iembod
Stats
  • Post in totale: 18842
  • Topic in totale: 2243
  • Online Today: 647
  • Online Ever: 900
  • (Gennaio 21, 2020, 08:17:49 pm)
Utenti Online
Users: 0
Guests: 418
Total: 418

Disclaimer:

Questo blog non rappresenta una testata giornalistica poiché viene aggiornato senza alcuna periodicità. Non può pertanto considerarsi un prodotto editoriale ai sensi della legge n. 62/2001.