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