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