Ok.
Code:
Includes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
"pop.h":
void receive( int sock, char *buf, int len )
{
ssize_t received = recv( sock, buf, len, 0 );
int i;
for( i = 0; i < received; i++ )
putchar( buf[i] );
}
void pop( char *address, char *username, char *password )
{
char buf[100];
int s = socket( AF_INET, SOCK_STREAM, 0 ),
i;
ssize_t received;
struct hostent *ho;
struct sockaddr_in remote;
if( s < 0 )
{
printf( "%s\n", strerror( errno ) );
return;
}
if( ( ho = gethostbyname( address ) ) == NULL )
{
printf( "%s\n", strerror( errno ) );
return;
}
remote.sin_addr = *( struct in_addr* )ho->h_addr_list[0];
remote.sin_port = htons( 110 );
remote.sin_family = AF_INET;
if( connect( s, ( struct sockaddr* )&remote, sizeof( struct sockaddr ) ) < 0)
{
printf( "%s\n", strerror( errno ) );
return;
}
receive( s, buf, 100 );
snprintf( buf, 100, "USER %s\r\n", username );
send( s, buf, strlen( buf ) + 1, 0 );
receive( s, buf, 100 );
snprintf( buf, 100, "PASS %s\r\n", password );
send( s, buf, strlen( buf ) + 1, 0 );
receive( s, buf, 100 );
send( s, "QUIT", 5, 0 );
close( s );
}
Call it like pop( "mail.bleh.com", "foobar", "mypass" );