|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
Inviare mail con C++
Da un'applicazione consolle com'è possibile inviare una mail ?
|
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Dec 2002
Messaggi: 40
|
Ci sono un po' di modi:
- Usare la MAPI, richiede pero' che hai installato Outlook (di Office, non l'express). - Gestirti il protocollo SMTP usando i socket - Trovare una libreria terze parti che gestisce l'SMTP da sola
__________________
www.arialinks.com ![]() |
![]() |
![]() |
![]() |
#3 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Re: Inviare mail con C++
Quote:
![]() Codice:
#include <winsock.h> #include <ostream.h> #include <stdio.h> #include <string> using namespace std; SOCKET to_server_socket = 0; //char server_name[] = "smtp.fastwebnet.it"; // Changer SERVEUR par le nom du serveur int port = 25; char sender[100],server_name[100],rcpt[100]; FILE* fp; void bcopy( void * source, void * destination, int size ) { char * src = ( char * ) source; char * dst = ( char * ) destination; for( int i=0; i<size; i++ ) dst[i] = src[i]; } void bzero( void * destination, int size ) { char * dst = ( char * ) destination; for( int i=0; i<size; i++ ) dst[i] = 0x00; } void Process( char * buffer ) { Sleep( 1000 ); cerr << "Envoye < " << buffer << endl; int size = strlen( buffer ); int retVal = send( to_server_socket, buffer, size, 0 ); char buf[ 1024 ]; buf[0] = 0x00; while( !buf[0] ) int yeah = recv( to_server_socket, buf, 1024, 0 ); cerr << "Recu > " << buf << endl << endl; } int main( int argc, char *argv[] ) { int not = 0; unsigned long ioctl_blocking = 1; fp = fopen("mailer.ini","r"); fscanf(fp,"%s",server_name); fclose(fp); fp = fopen("sender.ini","r"); fscanf(fp,"%s",sender); fclose(fp); fp = fopen("rcpt.ini","r"); fscanf(fp,"%s",rcpt); fclose(fp); WSADATA wsaData; if( int err = WSAStartup( 0x0101, &wsaData ) ) { cerr << "WSAStartup failed... Error: " << err << endl; exit( -1 ); } struct sockaddr_in serverSockAddr; // addresse de la socket struct hostent * serverHostEnt; // description du host serveur long hostAddr; // addr du serveur bzero( &serverSockAddr, sizeof( serverSockAddr ) ); // initialise a zero serverSockAddr hostAddr = inet_addr( server_name ); // converti l'adresse ip 9.100.1.1 en entier long // if( ( long ) hostAddr != ( long ) -1 ) // bcopy( &hostAddr, &serverSockAddr.sin_addr, sizeof( hostAddr ) ); //else // si on a donne un nom //{ serverHostEnt=NULL; while(!serverHostEnt) { serverHostEnt = gethostbyname( server_name ); if ( serverHostEnt == NULL ) { cerr << "ca chie gethost" << endl; //exit( 0 ); } else cout<<"CONNESSO!"; //Sleep(10000); } bcopy( serverHostEnt->h_addr, &serverSockAddr.sin_addr, serverHostEnt->h_length ); //} serverSockAddr.sin_port = htons( port ); // host to network port serverSockAddr.sin_family = AF_INET; // AF_*** : INET=internet // creation de la socket while (true) { to_server_socket = socket( AF_INET, SOCK_STREAM, 0 ); if ( to_server_socket < 0) { cerr << "ca chie creation socket client" << endl; exit( 0 ); } setsockopt(to_server_socket, SOL_SOCKET, SO_DONTLINGER, (char *) ¬, sizeof(not)); // requete de connexion if( connect( to_server_socket, ( struct sockaddr * ) &serverSockAddr, sizeof( serverSockAddr ) ) < 0 ) { cerr << "ca chie demande de connection" << endl; exit( 0 ); } ioctlsocket ( to_server_socket, FIONBIO, &ioctl_blocking ); char buf[ 1024 ]; buf[0] = 0x00; while( !buf[0] ) int yeah = recv( to_server_socket, buf, 1024, 0 ); cerr << "Receive > " << buf << endl << endl; Process( "HELO helio_denis\r\n" ); string a1="MAIL From: <"; a1 += sender; a1 += ">\r\n"; char * aux1=new char[a1.length()]; for (int h=0;h<a1.length();h++) aux1[h]=a1[h]; aux1[h]='\0'; Process( aux1); // Changer ADD FROM par le mail de l'expediteur //delete aux1; a1="RCPT To: <"; a1 += rcpt; a1 += ">\r\n"; aux1=new char[a1.length()]; for (h=0;h<a1.length();h++) aux1[h]=a1[h]; aux1[h]='\0'; Process( aux1); // Changer ADD FROM par le mail de l'expediteur //delete aux1; Process( "DATA\r\n" ); a1="From: "; a1+=sender; a1+="\r\nTo: "; a1+=rcpt; a1+="\r\nSubject: IP di "; a1+=sender; a1+="\r\n\r\n"; a1+="\r\n.\r\n"; aux1=new char[a1.length()]; for (h=0;h<a1.length();h++) aux1[h]=a1[h]; aux1[h]='\0'; Process( aux1); Process( "QUIT\r\n" ); //shutdown( to_server_socket, 2 ); closesocket( to_server_socket ); } return( closesocket( to_server_socket ) ); } Ultima modifica di LukeHack : 03-06-2004 alle 03:42. |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
Scusa ma nei tre file cosa ci devo mettere ?
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
non mi sembra che il codice postato regga l'autenticazione (magari mi sbaglio, ho solo dato una veloce occhiata).
in tal caso devi essere sicuro che il tuo server smtp non la voglia (e ormai sono pochi ![]() ![]() |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
Scusa che significa ?
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
Ragazzi ho risolto col seguente modo : Siccome col comando system("comando dos") posso inviare qualsiasi comando al prompt, mi apro una sessione telnet sulla porta 25 e invio la mail .... secondo voi è carica come cosa ???? Almeno mi risparmio molto codice....
|
![]() |
![]() |
![]() |
#8 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#9 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
![]() infatti l'smtp server si frega facilmente ![]() infatti supponiamo che tu debba mandare una email a tizio@tin.it e il tuo provider è tiscali... ora, se tu mandi una mail da un altro account (per es libero) è sufficiente che metti come smtp smtp.tiscali.it... infatti esso vede che dall'ip sei su tiscali e accetta di tutto...questo al 90% dei casi funge (Almeno per mia esperienza); altrimenti,altro trucchetto è il comando nslookup -q=mx <provider di arrivo> (x ipotesi tin.it)... lui ti fornirà il server MX ossia l'exchange smtp,vale a dire l'ultimo nodo che smista le email ai pop... bene,questi accettano di tutto, PURCHE' il destinatario (rcpt) ESISTA (Esso infatti provvede alla verifica dell'esistenza di tizio@tin.it) quindi mandare email,anche anonime è molto semplice,infatti i controlli a colabrodo e le autenticazioni si fregano almeno con questi due metodi ![]() Ultima modifica di LukeHack : 03-06-2004 alle 23:37. |
|
![]() |
![]() |
![]() |
#10 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
l'smtp, il mittente (te) e il destinatario |
|
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
a me da
550 relay not allowed uso solo katamail, email e fw. devo impostare la password in uscita, ma non capisco come si mandi (non è un'autenticazione in chiaro da quanto ho capito) ciao |
![]() |
![]() |
![]() |
#12 | |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
Quote:
posso mettere anche più di un destinatario ? |
|
![]() |
![]() |
![]() |
#13 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
ma ti basta usare come smtp quello del tuo PROVIDER, che sta barriera dovresti superarla,sennò c'è il secondo ttrucchetto e quello è un pò come la chiave universale di Diabolik ![]() fammi sapere ciao ![]() |
|
![]() |
![]() |
![]() |
#14 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
vediamo he succede ![]() |
|
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Per l'auteticazione è facile....
http://www.technoids.org/saslmech.html#appB (AUTH LOGIN) Ovviamente bisogna fare una codifica base64 di username e password... Se volte gli algoritmi di codifica e decodifica ce li ho, fatti in C... |
![]() |
![]() |
![]() |
#16 | |
Bannato
Iscritto dal: May 2003
Città: Roma
Messaggi: 3642
|
Quote:
![]() anche se non ho mai avuto problemi senza autenticazione, può sempre tornare utile ![]() |
|
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
me li fai vedere?
così aggiorno il programmino ![]() ![]() credevo servisse una cosa tipo cifratura in md5 ![]() meglio così! grazie, ciao |
![]() |
![]() |
![]() |
#18 |
Senior Member
Iscritto dal: Jul 2002
Città: Napoli
Messaggi: 1964
|
già che ci siamo posto un'altra domandina ....
ho già una connessione remota configurata ..... voglio poterla connettere e poi disconnettere quando voglio sempre dal programma in C++ .... è difficile ? |
![]() |
![]() |
![]() |
#19 | |
Senior Member
Iscritto dal: Jun 2003
Città: Genova
Messaggi: 5676
|
Quote:
ciao |
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Ecco qua:
Codice:
#include <iostream> #include <string> using namespace std; string Base64Encode(const string &data) { const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; unsigned int dataLen = data.length(); string res; for(int i=0; i<dataLen; i+=3) { unsigned int triplet; char tmp[5]; triplet = (data[i] << 16) + (((dataLen > (i+1))?data[i+1]:0) << 8) + ((dataLen > (i+2))?data[i+2]:0); tmp[3] = base64[triplet & 0x3F]; tmp[2] = base64[(triplet >> 6) & 0x3F]; tmp[1] = base64[(triplet >> 12) & 0x3F]; tmp[0] = base64[(triplet >> 18) & 0x3F]; tmp[4] = '\0'; res.append(tmp); } switch(dataLen % 3) { case 1: res[res.length()-2] = '='; case 2: res[res.length()-1] = '='; } return res; } inline int getBase64Pos(const char c) { switch(c) { case '+': return 62; case '/': return 63; default: if(c >= 'A' && c <= 'Z') return c - 'A'; else if(c >= 'a' && c <= 'z') return 26 + c - 'a'; else if(c >= '0' && c <= '9') return 52 + c - '0'; else return 0; } return -1; } string Base64Decode(const string &data) { unsigned int dataLen = data.length(); string res(""); if(res[dataLen-1] == '=') --dataLen; if(res[dataLen-1] == '=') --dataLen; unsigned int buf; char tmp[4]; for(int i=0; i<dataLen; i+=4) { int t1 = 0, t2 = 0, t3 = 0, t4 = 0; buf = 0; t1 = getBase64Pos(data[i]); t2 = getBase64Pos((dataLen > (i+1)) ? data[i+1]:'A'); t3 = getBase64Pos((dataLen > (i+2)) ? data[i+2]:'A'); t4 = getBase64Pos((dataLen > (i+3)) ? data[i+3]:'A'); if(t1 < 0 || t2 < 0 || t3 < 0 || t4 < 0) return res; tmp[0] = (t1 << 2) + (t2 >> 4); tmp[1] = (t2 << 4) + (t3 >> 2); tmp[2] = (t3 << 6) + t4; tmp[3] = '\0'; res.append(tmp); } return res; } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 08:12.