项目

一般

简介

ImapMail.c

IMAP发送邮件测试 - 卢 军辉, 2020-01-21 11:06

 
1
#include <stdio.h>
2
#include <string.h>
3
#include <curl/curl.h>
4

    
5

    
6
#define FROM        "<sebder@qq.org>"
7
#define TO                "<addressee@qq.net>"
8
#define CC                "<info@qq.org>"
9

    
10
static const char *payload_text[] = {
11
        "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
12
        "To: " TO "\r\n",
13
        "Frpm: " FROM "(qq User)\r\n",
14
        "Cc: " CC "(Another qq User)\r\n",
15
        "Message-ID: ",
16
        "<dcd7cb36_11db-487a-9f3a-e652a9458efd@rfcpedant.qq.org>\r\n>",
17
        "Subject: IMAP qq message\r\n",
18
        "\r\n",
19
        "The body of the message starts here.\r\n",
20
        "\r\n",
21
        "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
22
        "Check RFC5322.\r\n",
23
        NULL,
24
};
25

    
26
struct upload_status{
27
        int lines_read;
28
};
29

    
30
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
31
{
32
        struct upload_status *upload_ctx = (struct upload_status *)userp;
33
        const char *data;
34
        
35
        if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1))
36
        {
37
                return 0;
38
        }        
39
        
40
        data = payload_text[upload_ctx->lines_read];
41
        
42
        if(data)
43
        {
44
                size_t len = strlen(data);
45
                memcpy(ptr, data, len);
46
                upload_ctx->lines_read++;
47
                
48
                return len;        
49
        }
50
        
51
        return 0;
52
}
53

    
54
int main(void)
55
{
56
        CURL *curl;
57
        CURLcode res = CURLE_OK;
58
        
59
        curl = curl_easy_init();
60
        if(curl)
61
        {
62
                const char **p;
63
                long infilesize;
64
                struct upload_status upload_ctx;
65
                upload_ctx.lines_read = 0;
66
                
67
    /*?????û????????? */
68
                curl_easy_setopt(curl, CURLOPT_USERNAME, "1172678422@qq.com");
69
                curl_easy_setopt(curl, CURLOPT_PASSWORD, "pmdwsrcvhtwuidbj");
70
                
71
    /*?⽫????һ??????Ϣ100????ע?⣬??Ӧ??ִ??
72
     * EXAMINE?????ȡҪ????????һ????Ϣ??UID??һ??
73
     *ѡ????ȷ?????ڷ??????д?????Ϣ??*/
74
                curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.qq.com/100");
75
                
76
    /*??????????£?????ʹ?ûص???????ָ?????ݡ???
77
     *????ֻʹ??CURLOPT_READDATAѡ????ָ??ָ???FILEָ??
78
     *?Ķ???*/
79
                curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
80
                curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
81
                curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
82
                
83
                infilesize = 0;
84
                for(p = payload_text; *p; ++p)
85
                {
86
                        infilesize += (long)strlen(*p);        
87
                }                
88
                curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
89
                
90
    /*ִ?и??Ӳ???*/ 
91
                res = curl_easy_perform(curl);
92
                
93
    /*??????*/ 
94
                if(res != CURLE_OK)
95
                {
96
                        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));        
97
                }
98
                
99
    /*????????*/
100
                curl_easy_cleanup(curl);
101
        }
102
        
103
        return (int)res;
104
}