Skip to content
This repository
branch: master
Hoà V. DINH August 02, 2011
file 83 lines (64 sloc) 1.73 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <libetpan/libetpan.h>
#include <stdlib.h>
#include <sys/stat.h>

static void check_error(int r, char * msg)
{
if (r == MAILPOP3_NO_ERROR)
return;

fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}

int main(int argc, char ** argv)
{
mailpop3 * pop3;
int r;
carray * list;
unsigned int i;

if (argc < 3) {
fprintf(stderr, "syntax: pop-sample [gmail-email-address] [gmail-password]\n");
exit(EXIT_FAILURE);
}

mkdir("download", 0700);

pop3 = mailpop3_new(0, NULL);
r = mailpop3_ssl_connect(pop3, "pop.gmail.com", 995);
check_error(r, "connect failed");

r = mailpop3_user(pop3, argv[1]);
check_error(r, "user failed");

r = mailpop3_pass(pop3, argv[2]);
check_error(r, "pass failed");

r = mailpop3_list(pop3, &list);
check_error(r, "list failed");

for(i = 0 ; i < carray_count(list) ; i ++) {
struct mailpop3_msg_info * info;
char * msg_content;
size_t msg_size;
FILE * f;
char filename[512];
struct stat stat_info;

info = carray_get(list, i);

if (info->msg_uidl == NULL) {
continue;
}

snprintf(filename, sizeof(filename), "download/%s.eml", info->msg_uidl);
r = stat(filename, &stat_info);
if (r == 0) {
printf("already fetched %u %s\n", info->msg_index, info->msg_uidl);
continue;
}

r = mailpop3_retr(pop3, info->msg_index, &msg_content, &msg_size);
check_error(r, "get failed");

f = fopen(filename, "w");
fwrite(msg_content, 1, msg_size, f);
fclose(f);
mailpop3_retr_free(msg_content);

if (info->msg_uidl != NULL) {
printf("fetched %u %s\n", info->msg_index, info->msg_uidl);
}
else {
printf("fetched %u\n", info->msg_index);
}
}

mailpop3_quit(pop3);
mailpop3_free(pop3);

exit(EXIT_SUCCESS);
}
Something went wrong with that request. Please try again.