[ back to toc ]

Network Programming with C

Date: 2001/12/24 10:00

Q:
I would ask you how I can find source code in C in the web about writing a
programm which can open and read POP3 e-mail accounts.

I prefer C than MFC C++ and use the Microsoft Visual C compiler but also
cygwin. Thank you in advance.

A:
I do not know any ready example. However I assume that you are familiar
with socket programming. If not, read a socket programming tutorial.

In case you are familiar wit socket programming you can easily convert a
Perl script that I have here at my hands to C. This small script handles a
POP3 mail box.

I hope it helps. Also the RFC is not too difficult to read.

Regards,
Peter

#!/usr/bin/perl
# LEGAL WARNING:
#
# This software is provided on an "AS IS", basis,
# without warranty of any kind, including without
# limitation the warranties of merchantability, fitness for
# a particular purpose and non-infringement. The entire
# risk as to the quality and performance of the Software is
# borne by you. Should the Software prove defective, you
# and not the author assume the entire cost of any service
# and repair.
#

#
# This is a minimal Pop3 connection handling script.
#

$Eol = "\r\n";

sub ClosePopConnection{
print POPSH "QUIT$Eol";
shutdown(POPSH, 2);
close POPSH;
}

sub DelPopMail{
my $num = shift;
if( ! defined $num ){ $num = 1 }
print POPSH "DELE $num$Eol";
$PopServerMessage = <POPSH>;
if( $PopServerMessage =~ /^\+OK / ){
return 1;
}else{
return 0;
}
}

sub GetPopMail {
my $num = shift;
if( ! defined $num ){ $num = 1 }
print POPSH "RETR $num$Eol";
$PopServerMessage = <POPSH>;

if( ! $PopServerMessage =~ /^\+/ ){ return 0; }
if( $PopServerMessage =~ /^\+OK (\d+) /){
$buflen = $1;
}

$PopMailHeader = '';
$PopMailBody = '';
do{
$_ = <POPSH>;
$PopMailHeader .= $_;
} until /^\s*$/;

for(;;){
$_ = <POPSH>;
if( /^\.\s*$/ ){last}
$PopMailBody .= $_;
}

return 1;
}

sub OpenPopConnection {
my ($UserName,$Password,$PopServer,$PopServerPort) = @_;

my ($AF_INET) = 2;
my ($SOCK_STREAM) = 1;

if($PopServer =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
$PopServerAddress = pack('C4', $1, $2, $3, $4);
}else{
$PopServerAddress = gethostbyname ($host);
}

$PopServerName = gethostbyaddr ($PopServerAddress, $AF_INET);
if( ! defined($PopServerPort) ){ $PopServerPort = 110; }

socket(POPSH, $AF_INET, SOCK_STREAM, getprotobyname('tcp') || 6);
connect(POPSH, pack('Sna4x8', $AF_INET, $PopServerPort,
$PopServerAddress));

select((select(POPSH) , $| = 1)[0]);

my $PopServerMessage = <POPSH>;
if( ! $PopServerMessage ){ return 0; }

print POPSH "USER $UserName$Eol";
$PopServerMessage = <POPSH>;
if( ! $PopServerMessage =~ /^\+/ ){ return 0; }

print POPSH "PASS $Password$Eol";
$PopServerMessage = <POPSH>;
if( ! $PopServerMessage =~ /^\+/ ){ return 0; }

if( $PopServerMessage =~ /^\+OK \S+ has (\d+) /i ){
$PopMailCount = $1;
}else{
$PopMailCount = 0;
}

print POPSH "STAT$Eol";
$PopServerMessage = <POPSH>;
if( ! $PopServerMessage =~ /^\+OK/ ){ return 0 }
if( $PopServerMessage =~ /^\+OK (\d+) (\d+)/ ){
$PopMailCount = $1;
$PopMailSize = $2;
}
return 1;
}

1;

#print 'Username:'; $UserName = <>; chomp $UserName;
#print 'Password:'; $Password = <>; chomp $Password;
#$i= &OpenPopConnection($UserName, $Password, $PopServer);
#print "$i\nCount: $PopMailCount\nSize: $PopMailSize\nMessage:
$PopServerMessage";
#if( $PopMailCount > 0 ){
# $i = &GetPopMail;
# print "$i\n";
# print "Header:\n$PopMailHeader\nBody:\n$PopMailBody\n******";
# &DelPopMail;
# }
#&ClosePopConnection;

[ back to toc ]