| | |
read e-mail attachment with POP3
![]() |
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
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 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
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
0
#3 23 Days Ago
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:
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:
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:
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:
PS: sorry for the late response.

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:
c Syntax (Toggle Plain Text)
#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:
c Syntax (Toggle Plain Text)
/* 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:
c Syntax (Toggle Plain Text)
#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:
c Syntax (Toggle Plain Text)
memset(sendbuf,0,sizeof(sendbuf)); sprintf(sendbuf,"QUIT\n"); write(sockfd,sendbuf,strlen(sendbuf)); close(sockfd);
PS: sorry for the late response.
![]() |
Similar Threads
- Email attachment and add text inside the mail (Shell Scripting)
- Handy e-mail client (Windows Software)
- help in mail attachment (PHP)
- e-mail won't attach word.doc (Windows NT / 2000 / XP)
- Automate Web Browser (VB.NET)
- Monitoring number of smtp bytes sent through python e-mail socket (Python)
- email attachment (Visual Basic 4 / 5 / 6)
- Reading Mail (Java)
- Pop3 Mail Watcher (Part 1) (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Print table of a number entered by user
- Next Thread: switch case problem with characters
| Thread Tools | Search this Thread |
#include adobe api array asterisks binarysearch calculate char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab win32api windows.h





