79e870d9e717268e55cdb27aedeb3d946b52489a
[sdk] / samples / net / smtp / smtp.ec
1 import "ecere"
2
3 define app = ((MyApp)__thisModule);
4
5 class SMTPSocket : Socket
6 {
7    bool replied;
8    uint OnReceive(const byte * buffer, uint count)
9    {
10       int c;
11       for(c = 0; c<count; c++)
12       {
13          //putch(buffer[c]);
14          putc(buffer[c]);
15       }
16       replied = true;
17       return count;
18    }
19    void OnDisconnect(int code)
20    {
21       replied = true;      
22    }
23    void WaitReply()
24    {
25       while(!replied)
26       {
27          app.WaitNetworkEvent();
28          app.ProcessNetworkEvents();
29       }
30       replied = false;
31    }
32 };
33
34 void Send(char * host, char * to, char * from, File file)
35 {
36    SMTPSocket socket { };
37    incref socket;
38
39    if(socket.Connect(host, 25))
40    {
41       socket.WaitReply();
42       socket.SendString("HELO localhost\n");
43       socket.WaitReply();
44       socket.Sendf("MAIL from: %s\n", from); 
45       socket.WaitReply();
46       socket.Sendf("RCPT To: %s\n", to); 
47       socket.WaitReply();
48       socket.SendString("DATA\n"); 
49       socket.WaitReply();
50       socket.SendString("Subject: Email test\n");
51       socket.SendString("Content-Type: text/html; charset=Windows-1252\n");
52      
53       file.Seek(0, start);
54       while(!file.Eof())
55       {
56          char buffer[4096];
57          uint read = file.Read(buffer, 1, sizeof(buffer));
58          socket.Send(buffer, read);
59       }
60       socket.SendString("\r\n");
61       socket.SendString(".\r\n");
62       socket.WaitReply();
63       socket.SendString("QUIT\r\n");
64       socket.Disconnect(0);
65    }
66    delete socket;
67 }
68
69 class MyApp : GuiApplication
70 {
71    void Main()
72    {
73       File f = FileOpen("http://www.ecere.com/", read);
74       if(f)
75          Send("mail.mailserver.com", "destaddress@destdomain.com", "srcaddress@srcdomain.com", f);
76       delete f;
77       getch();
78    }   
79 }