Le protocole SMTP: client smtp en visual c++
une version du client smtp pour visual c++
( cet exemple a été réalisé par Denis DUCASSE à partir du programme 'envoie de fichier')
il faut changer 3
choses dans le programme: le nom du serveur SMTP, l'adresse mail de
l'expediteur et celle du destinataire. Il faut aussi ajouter la librairie
ws2_32.lib dans Project -> Settings -> Link -> Object/library modules pour
une question de link a la compilation.
#include <winsock.h>
#include <ostream.h>
SOCKET to_server_socket = 0;
char server_name[] = "SERVEUR"; // Changer SERVEUR par le nom du serveur
int port = 25;
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;
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 ) );
// converti l'adresse ip 9.100.1.1 en entier long
hostAddr = inet_addr( server_name );
if( ( long ) hostAddr != ( long ) -1 )
bcopy( &hostAddr, &serverSockAddr.sin_addr, sizeof( hostAddr ) );
else // si on a donne un nom
{
serverHostEnt = gethostbyname( server_name );
if ( serverHostEnt == NULL )
{
cerr << "ca chie gethost" << endl;
exit( 0 );
}
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
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" );
Process( "MAIL From: <ENVOYEUR>\r\n" );
Process( "RCPT To: <DESTINATAIRE>\r\n" );
Process( "DATA\r\n" );
Process( "Subject: Yeah !!!\r\n\r\nEt voici un petit mail !\r\n.\r\n" );
Process( "QUIT\r\n" );
/* fermeture de la connection */
shutdown( to_server_socket, 2 );
return( closesocket( to_server_socket ) );
}
|