1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include <Socket.h>
|
---|
4 | #include <SecureSocket.h>
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | status_t Open(const char *address, int port, bool use_ssl)
|
---|
9 | {
|
---|
10 | if (port <= 0)
|
---|
11 | port = use_ssl ? 465 : 25;
|
---|
12 |
|
---|
13 | cout << "***" << endl;
|
---|
14 | cout << "Connecting to server: " << address << " port: " << port << " ssl: " << use_ssl << endl;
|
---|
15 |
|
---|
16 | BNetworkAddress addr(address);
|
---|
17 | if (addr.InitCheck() != B_OK) {
|
---|
18 | BString str;
|
---|
19 | str.SetToFormat("Invalid network address for SMTP server: %s",
|
---|
20 | strerror(addr.InitCheck()));
|
---|
21 | cout << str << endl;
|
---|
22 | return addr.InitCheck();
|
---|
23 | }
|
---|
24 |
|
---|
25 | if (addr.Port() == 0)
|
---|
26 | addr.SetPort(port);
|
---|
27 |
|
---|
28 | BSocket *fSocket = nullptr;
|
---|
29 | if (use_ssl)
|
---|
30 | fSocket = new(std::nothrow) BSecureSocket;
|
---|
31 | else
|
---|
32 | fSocket = new(std::nothrow) BSocket;
|
---|
33 |
|
---|
34 | if (!fSocket)
|
---|
35 | return B_NO_MEMORY;
|
---|
36 |
|
---|
37 | if (fSocket->Connect(addr) != B_OK) {
|
---|
38 | BString error;
|
---|
39 | error << "Could not connect to SMTP server "
|
---|
40 | << address;
|
---|
41 | error << ":" << addr.Port();
|
---|
42 | cout << error << endl;
|
---|
43 | delete fSocket;
|
---|
44 | return B_ERROR;
|
---|
45 | }
|
---|
46 |
|
---|
47 | // BString line;
|
---|
48 | // ReceiveResponse(line);
|
---|
49 |
|
---|
50 | // char localhost[255];
|
---|
51 | // gethostname(localhost, 255);
|
---|
52 |
|
---|
53 | // if (localhost[0] == 0)
|
---|
54 | // strcpy(localhost, "namethisbebox");
|
---|
55 |
|
---|
56 | // char *cmd = new char[::strlen(localhost)+8];
|
---|
57 | // if (!esmtp)
|
---|
58 | // ::sprintf(cmd,"HELO %s" CRLF, localhost);
|
---|
59 | // else
|
---|
60 | // ::sprintf(cmd,"EHLO %s" CRLF, localhost);
|
---|
61 |
|
---|
62 | // if (SendCommand(cmd) != B_OK) {
|
---|
63 | // delete[] cmd;
|
---|
64 | // return B_ERROR;
|
---|
65 | // }
|
---|
66 |
|
---|
67 | // delete[] cmd;
|
---|
68 |
|
---|
69 | // Check auth type
|
---|
70 | // if (esmtp) {
|
---|
71 | // const char *res = fLog.String();
|
---|
72 | // char *p;
|
---|
73 | // if ((p = ::strstr(res, "250-AUTH")) != NULL
|
---|
74 | // || (p = ::strstr(res, "250 AUTH")) != NULL) {
|
---|
75 | // if(::strstr(p, "LOGIN"))
|
---|
76 | // fAuthType |= LOGIN;
|
---|
77 | // if(::strstr(p, "PLAIN"))
|
---|
78 | // fAuthType |= PLAIN;
|
---|
79 | // if(::strstr(p, "CRAM-MD5"))
|
---|
80 | // fAuthType |= CRAM_MD5;
|
---|
81 | // if(::strstr(p, "DIGEST-MD5")) {
|
---|
82 | // fAuthType |= DIGEST_MD5;
|
---|
83 | // fServerName = address;
|
---|
84 | // }
|
---|
85 | // }
|
---|
86 | // }
|
---|
87 | cout << endl;
|
---|
88 | return B_OK;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | int
|
---|
93 | main(int argc, char **argv)
|
---|
94 | {
|
---|
95 | BString name = "smtp.mail.me.com";
|
---|
96 |
|
---|
97 | status_t status = Open(name.String(), 0, true);
|
---|
98 | cout << "Status: " << status << endl;
|
---|
99 | status = Open(name.String(), 587, true);
|
---|
100 | cout << "Status: " << status << endl;
|
---|
101 | status = Open(name.String(), 465, true);
|
---|
102 | cout << "Status: " << status << endl;
|
---|
103 | status = Open(name.String(), 0, true);
|
---|
104 | cout << "Status: " << status << endl;
|
---|
105 |
|
---|
106 | status = Open(name.String(), 0, false);
|
---|
107 | cout << "Status: " << status << endl;
|
---|
108 | status = Open(name.String(), 587, false);
|
---|
109 | cout << "Status: " << status << endl;
|
---|
110 | status = Open(name.String(), 465, false);
|
---|
111 | cout << "Status: " << status << endl;
|
---|
112 | status = Open(name.String(), 0, false);
|
---|
113 | cout << "Status: " << status << endl;
|
---|
114 | }
|
---|