Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (64 sloc) 1.73 kb
daf87a2 @dinhviethoa work on sample, added pop sample
authored
1 #include <libetpan/libetpan.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4
5 static void check_error(int r, char * msg)
6 {
7 if (r == MAILPOP3_NO_ERROR)
8 return;
9
10 fprintf(stderr, "%s\n", msg);
11 exit(EXIT_FAILURE);
12 }
13
14 int main(int argc, char ** argv)
15 {
16 mailpop3 * pop3;
17 int r;
18 carray * list;
19 unsigned int i;
20
21 if (argc < 3) {
22 fprintf(stderr, "syntax: pop-sample [gmail-email-address] [gmail-password]\n");
23 exit(EXIT_FAILURE);
24 }
25
26 mkdir("download", 0700);
27
28 pop3 = mailpop3_new(0, NULL);
29 r = mailpop3_ssl_connect(pop3, "pop.gmail.com", 995);
30 check_error(r, "connect failed");
31
32 r = mailpop3_user(pop3, argv[1]);
33 check_error(r, "user failed");
34
35 r = mailpop3_pass(pop3, argv[2]);
36 check_error(r, "pass failed");
37
38 r = mailpop3_list(pop3, &list);
39 check_error(r, "list failed");
40
41 for(i = 0 ; i < carray_count(list) ; i ++) {
42 struct mailpop3_msg_info * info;
43 char * msg_content;
44 size_t msg_size;
45 FILE * f;
46 char filename[512];
47 struct stat stat_info;
48
49 info = carray_get(list, i);
50
51 if (info->msg_uidl == NULL) {
52 continue;
53 }
54
55 snprintf(filename, sizeof(filename), "download/%s.eml", info->msg_uidl);
56 r = stat(filename, &stat_info);
57 if (r == 0) {
58 printf("already fetched %u %s\n", info->msg_index, info->msg_uidl);
59 continue;
60 }
61
62 r = mailpop3_retr(pop3, info->msg_index, &msg_content, &msg_size);
63 check_error(r, "get failed");
64
65 f = fopen(filename, "w");
66 fwrite(msg_content, 1, msg_size, f);
67 fclose(f);
68 mailpop3_retr_free(msg_content);
69
70 if (info->msg_uidl != NULL) {
71 printf("fetched %u %s\n", info->msg_index, info->msg_uidl);
72 }
73 else {
74 printf("fetched %u\n", info->msg_index);
75 }
76 }
77
78 mailpop3_quit(pop3);
79 mailpop3_free(pop3);
80
81 exit(EXIT_SUCCESS);
82 }
Something went wrong with that request. Please try again.