944,113 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1794
  • C RSS
May 31st, 2009
0

read e-mail attachment with POP3

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soultrav is offline Offline
17 posts
since Mar 2008
Oct 30th, 2009
0
Re: read e-mail attachment with POP3
I'm looking for a way to read emails, is there a way that you can share your code with me?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Slamowitz is offline Offline
1 posts
since Oct 2009
Nov 4th, 2009
0
Re: read e-mail attachment with POP3
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soultrav is offline Offline
17 posts
since Mar 2008
Nov 4th, 2009
-1
Re: read e-mail attachment with POP3
On Windows, you don't need sockets; just use COM (10 lines of code)
Reputation Points: -76
Solved Threads: 14
Junior Poster
marco93 is offline Offline
132 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Print table of a number entered by user
Next Thread in C Forum Timeline: switch case problem with characters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC