Hi!
I wrote a program that reads and saves e-mails, of course by using sockets for connecting to the mail server. But I don't know how to download attachments, I used telnet to open an e-mail with an attachment, and I didn't see a link or something to the attached file. I've been searching the web, but found only python/php/c# solutions. Help appreciated :)

Cheers

I'm looking for a way to read emails, is there a way that you can share your code with me?

Well, if I share my code, you could probably copy-paste it and not understand a thing of it, I'll just explain a bit, it's really simple :)

There's two things you need to read about: working with sockets in C and the POP3 protocol (described in RFC1725).

A socket is an endpoint of a bidirectional communication flow between processes - it allows processes on different machines to comunicate with each other.
Here's how you create a socket and establish the connection between you and the mail server:

#define MAX_CH 255
#define POP3_PORT 110

int sockfd;
struct sockaddr_in servaddr;
struct hostent* h;
char domain[MAX_CH];
/*buffers for sending/receiving messages*/
char sendbuf[MAXLEN]; 
char recvbuf[MAXLEN];

/* user input's the domain name*/
printf("POP3 server: ");
scanf("%s", domain);

/*create socket */
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) 
{
	printf("Error creating socket.\n");
	exit(EXIT_FAILURE);
}  

/* for the socket to "understand" the domain name
    it must be converted using the gethostbyname() 
    function */
h = gethostbyname(domain);

/* fill servaddr with zeroes, and then initialize it */
memset((char *) &servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
memcpy((char *)&servaddr.sin_addr.s_addr,
              (char *)h->h_addr, 
               h->h_length);
servaddr.sin_port = htons(POP3_PORT);
    
/* connect to the server  */
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) 
{
    printf("Server could not be reached.\n");
    exit(EXIT_FAILURE);
}

/* read the response from the server
    and verify it's ok */	
read(sockfd,recvbuf,sizeof(recvbuf));
if(!strstr(recvbuf,"+OK"))
	exit(EXIT_FAILURE);

From now on, you will comunicate with the server using the read() and write() methods. Above is an example of using read(); write() is similar to that. For example, the next step in the protocol, you will have to send the server your username:

/* read the username from the user
    and send the command to the server */
scanf("%s", username);
sprintf(sendbuf,"USER %s\n", username);
write(sockfd,sendbuf,strlen(sendbuf));

/*verify that the server responded OK*/
memset(recvbuf,0,sizeof(recvbuf));
read(sockfd,recvbuf,sizeof(recvbuf));
if(!strstr(recvbuf,"+OK")) 
{
	printf("Incorrect user name.\n");
	exit(EXIT_FAILURE);
}

If you want to find more about the methods/structures above, just google it, you'll find lots of stuff.
I don't remember exactly what libraries are used for every method/structure but here is what i used in my entire program:

#include <sys/socket.h> 
#include <netinet/in.h>
#include <arpa/inet.h>  
#include <unistd.h>     
#include <sys/types.h>  
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>

I hope you got it, from now on you just have to follow the steps of the POP3 protocol, save your e-mails in files, and at the end of the communication send a QUIT command to the server and close the socket:

memset(sendbuf,0,sizeof(sendbuf));
sprintf(sendbuf,"QUIT\n");
write(sockfd,sendbuf,strlen(sendbuf));
close(sockfd);

PS: sorry for the late response.

This article has been dead for over six months. Start a new discussion instead.