1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
| using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
public class PopCheck : Form
{
private TextBox hostname;
private TextBox username;
private TextBox password;
private TextBox status;
private ListBox messages;
private TcpClient mailclient;
private NetworkStream ns;
private StreamReader sr;
private StreamWriter sw;
public PopCheck()
{
Text = "popcheck - A POP3 e-mail checker";
Size = new Size(400, 380);
Label label1 = new Label();
label1.Parent = this;
label1.Text = "Hostname:";
label1.AutoSize = true;
label1.Location = new Point(10, 33);
hostname = new TextBox();
hostname.Parent = this;
hostname.Size = new Size(200, 2 * Font.Height);
hostname.Location = new Point(75, 30);
Label label2 = new Label();
label2.Parent = this;
label2.Text = "User name:";
label2.AutoSize = true;
label2.Location = new Point(10, 53);
username = new TextBox();
username.Parent = this;
username.Size = new Size(200, 2 * Font.Height);
username.Location = new Point(75, 50);
Label label3 = new Label();
label3.Parent = this;
label3.Text = "Password:";
label3.AutoSize = true;
label3.Location = new Point(10, 73);
password = new TextBox();
password.Parent = this;
password.PasswordChar = '*';
password.Size = new Size(200, 2 * Font.Height);
password.Location = new Point(75, 70);
Label label4 = new Label();
label4.Parent = this;
label4.Text = "Status:";
label4.AutoSize = true;
label4.Location = new Point(10, 325);
status = new TextBox();
status.Parent = this;
status.Text = "Not connected";
status.Size = new Size(200, 2 * Font.Height);
status.Location = new Point(50, 322);
messages = new ListBox();
messages.Parent = this;
messages.Location = new Point(10, 108);
messages.Size = new Size(360, 16 * Font.Height);
messages.DoubleClick += new EventHandler(getmessagesDoubleClick);
Button login = new Button();
login.Parent = this;
login.Text = "Login";
login.Location = new Point(295, 32);
login.Size = new Size(5 * Font.Height, 2 * Font.Height);
login.Click += new EventHandler(ButtonloginOnClick);
Button close = new Button();
close.Parent = this;
close.Text = "Close";
close.Location = new Point(295, 62);
close.Size = new Size(5 * Font.Height, 2 * Font.Height);
close.Click += new EventHandler(ButtoncloseOnClick);
}
void ButtonloginOnClick(object obj, EventArgs ea)
{
status.Text = "Checking for messages...";
Thread startlogin = new Thread(new ThreadStart(loginandretr));
startlogin.IsBackground = true;
startlogin.Start();
}
void ButtoncloseOnClick(object obj, EventArgs ea)
{
if (ns != null)
{
sw.Close();
sr.Close();
ns.Close();
mailclient.Close();
}
Close();
}
void loginandretr()
{
string response;
string from = null;
string subject = null;
int totmessages;
try
{
mailclient = new TcpClient(hostname.Text, 110);
} catch (SocketException)
{
status.Text = "Unable to connect to server";
return;
}
ns = mailclient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
response = sr.ReadLine(); //Get opening POP3 banner
sw.WriteLine("User " + username.Text); //Send username
sw.Flush();
response = sr.ReadLine();
if (response.Substring(0,3) == "-ERR")
{
status.Text = "Unable to log into server";
return;
}
sw.WriteLine("Pass " + password.Text); //Send password
sw.Flush();
try
{
response = sr.ReadLine();
} catch (IOException)
{
status.Text = "Unable to log into server";
return;
}
if (response.Substring(0,3) == "-ER")
{
status.Text = "Unable to log into server";
return;
}
sw.WriteLine("stat"); //Send stat command to get number of messages
sw.Flush();
response = sr.ReadLine();
string[] nummess = response.Split(' ');
totmessages = Convert.ToInt16(nummess[1]);
if (totmessages > 0)
{
status.Text = "you have " + totmessages + " messages";
}
else
{
status.Text = "You have no messages";
}
for (int i = 1; i <= totmessages; i++)
{
sw.WriteLine("top " + i + " 0"); //read header of each message
sw.Flush();
response = sr.ReadLine();
while (true)
{
response = sr.ReadLine();
if (response == ".")
break;
if (response.Length > 4)
{
if (response.Substring(0, 5) == "From:")
from = response;
if (response.Substring(0, 8) == "Subject:")
subject = response;
}
}
//messages.Items.Add(i + " " + from + " " + subject);
}
}
void getmessagesDoubleClick(object obj, EventArgs ea)
{
string text = (string)messages.SelectedItem;
string[] textarray = text.Split(' ');
ShowMessage sm = new ShowMessage(ns, textarray[0]);
sm.ShowDialog();
}
public static void Main()
{
Application.Run(new PopCheck());
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// PopCheck
//
this.ClientSize = new System.Drawing.Size(284, 264);
this.Name = "PopCheck";
this.Load += new System.EventHandler(this.PopCheck_Load);
this.ResumeLayout(false);
}
private void PopCheck_Load(object sender, EventArgs e)
{
}
}
class ShowMessage : Form
{
public ShowMessage(NetworkStream ns, string messnumber)
{
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string response;
Text = "Message " + messnumber;
Size = new Size(400, 380);
ShowInTaskbar = false;
TextBox display = new TextBox();
display.Parent = this;
display.Multiline = true;
display.Dock = DockStyle.Fill;
display.ScrollBars = ScrollBars.Both;
sw.WriteLine("retr " + messnumber); //Retrieve entire message
sw.Flush();
response = sr.ReadLine();
while (true)
{
response = sr.ReadLine();
if (response == ".")
break;
display.Text += response + "\r\n";
}
}
} |