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.
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.
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.
SMTP ERROR: Login: ???-Other undefined Status
SMTP ERROR: Logout: ???-Other undefined Status
so che un modo era questo, ma non è così comodo, cercavo altro...
In linea di massima ti potrebbe bastare avere le dll necessarie nella stessa directory dell'eseguibile, quindi non avresti niente da dovere installare.
Piccoli cracker crescono
Occhio a non farti beccare dai prof!
Legolaaaaaaaaaaaaaaaas, porca paletta, gli hai detto come farsi spedire per mail i compiti in classe il giorno prima :(
Come diceva Legolas, non serve installare una DLL ... ma farla trovare al tuo eseguibile.
Altra cosa ... il tuo programma spione, deve rimanere attivo, ma qui si parla di servizi e i servizi hanno qualche rogna in + sulla security di Windows ...
LOL ;D
In effetti bisognerebbe impegnare tempo e risorse nello studio, piuttosto che nel cercare modi (rischiosi) di aggirare il problema ::)
Avrei bisogno di far girare il programma in molti computer, anche in alcuni senza autorizzazzione per l'installazione di programma (a scuola per esempio)intendevo che gli account degli studenti non hanno le autorizzazioni per installare qualunque programma...vedono solo i propri file e i propri progetti.
EDIT :Sono curioso, è proprio quello che cercavo :) sapresti darmi qualche informazione in merito o linkarmi una wiki?
Ti sconsiglio di usare le DLL come risorse embedded dell'applicazione.
http://it.wikipedia.org/wiki/Trojangiosch, non è che non devi sapere. Ma fare una cosa che non serve, perchè vuoi fare il figo in classe ... (o così sembra).
se è questo che intendi non ci siamo proprio...volevo solo sapere, ma se credete che io non debba sapere come allora...
se è questo che intendi non ci siamo proprio...volevo solo sapere, ma se credete che io non debba sapere come allora...
Ho afferrato il concetti grazie :)http://it.wikipedia.org/wiki/Trojangiosch, non è che non devi sapere. Ma fare una cosa che non serve, perchè vuoi fare il figo in classe ... (o così sembra).
se è questo che intendi non ci siamo proprio...volevo solo sapere, ma se credete che io non debba sapere come allora...
Vuoi dirmi che la gnocca della classe non è capace a lanciare un exe, e rischia di mandare in esecuzione la DLL....?
Ma daiiiiiii
Ti stiamo solo sconsigliando nel fare una clamorosa ... zzzata?