read e-mail attachment with POP3

Reply

Join Date: Mar 2008
Posts: 17
Reputation: soultrav is an unknown quantity at this point 
Solved Threads: 0
soultrav soultrav is offline Offline
Newbie Poster

read e-mail attachment with POP3

 
0
  #1
May 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: Slamowitz is an unknown quantity at this point 
Solved Threads: 0
Slamowitz Slamowitz is offline Offline
Newbie Poster
 
0
  #2
28 Days Ago
I'm looking for a way to read emails, is there a way that you can share your code with me?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: soultrav is an unknown quantity at this point 
Solved Threads: 0
soultrav soultrav is offline Offline
Newbie Poster
 
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:
  1. #define MAX_CH 255
  2. #define POP3_PORT 110
  3.  
  4. int sockfd;
  5. struct sockaddr_in servaddr;
  6. struct hostent* h;
  7. char domain[MAX_CH];
  8. /*buffers for sending/receiving messages*/
  9. char sendbuf[MAXLEN];
  10. char recvbuf[MAXLEN];
  11.  
  12. /* user input's the domain name*/
  13. printf("POP3 server: ");
  14. scanf("%s", domain);
  15.  
  16. /*create socket */
  17. if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  18. {
  19. printf("Error creating socket.\n");
  20. exit(EXIT_FAILURE);
  21. }
  22.  
  23. /* for the socket to "understand" the domain name
  24.   it must be converted using the gethostbyname()
  25.   function */
  26. h = gethostbyname(domain);
  27.  
  28. /* fill servaddr with zeroes, and then initialize it */
  29. memset((char *) &servaddr, 0, sizeof(servaddr));
  30. servaddr.sin_family = AF_INET;
  31. memcpy((char *)&servaddr.sin_addr.s_addr,
  32. (char *)h->h_addr,
  33. h->h_length);
  34. servaddr.sin_port = htons(POP3_PORT);
  35.  
  36. /* connect to the server */
  37. if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
  38. {
  39. printf("Server could not be reached.\n");
  40. exit(EXIT_FAILURE);
  41. }
  42.  
  43. /* read the response from the server
  44.   and verify it's ok */
  45. read(sockfd,recvbuf,sizeof(recvbuf));
  46. if(!strstr(recvbuf,"+OK"))
  47. 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:
  1. /* read the username from the user
  2.   and send the command to the server */
  3. scanf("%s", username);
  4. sprintf(sendbuf,"USER %s\n", username);
  5. write(sockfd,sendbuf,strlen(sendbuf));
  6.  
  7. /*verify that the server responded OK*/
  8. memset(recvbuf,0,sizeof(recvbuf));
  9. read(sockfd,recvbuf,sizeof(recvbuf));
  10. if(!strstr(recvbuf,"+OK"))
  11. {
  12. printf("Incorrect user name.\n");
  13. exit(EXIT_FAILURE);
  14. }

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:
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #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:
  1. memset(sendbuf,0,sizeof(sendbuf));
  2. sprintf(sendbuf,"QUIT\n");
  3. write(sockfd,sendbuf,strlen(sendbuf));
  4. close(sockfd);


PS: sorry for the late response.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster
 
-1
  #4
23 Days Ago
On Windows, you don't need sockets; just use COM (10 lines of code)
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC