using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net.Sockets;
using System.Diagnostics;
namespace NetOffice_3
{
public partial class Form12 : Form
{
public Form12()
{
InitializeComponent();
}
// emails abholen -----------------------------------------------------------------------------
private void F12B1_Click(object sender, EventArgs e)
{
try
{
string str1 = F12TSCmb2.Text;
string str2 = F12TSCmb3.Text;
string str3 = F12TSTB1.Text;
POPServer svr = new POPServer();
svr.Verbinden(str1, str2, str3);
ArrayList list = svr.List();
foreach (ServerMessage msg in list)
{
ServerMessage smg2 = svr.Abholen(msg);
F12RTFBox1.AppendText("Nr." + smg2.number.ToString() + " ");
F12RTFBox1.AppendText(smg2.message.ToString() + "End\n\n");
svr.Entfernen(msg);
}
svr.Trennen();
}
catch (ServerException ex)
{
MessageBox.Show("Ein Fehler ist aufgetreten ..." + "Details: " + ex.ToString(), "NetOffice", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
catch (Exception ex)
{
MessageBox.Show("Ein Fehler ist aufgetreten ..." + "Details: " + ex.ToString(), "NetOffice", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
MessageBox.Show("Keine weiteren Nachrichten vorhanden ...", "NetOffice", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// Exceptionklasse definieren -----------------------------------------------------
public class ServerException : System.ApplicationException
{
public ServerException(string str)
: base(str)
{
}
}
//---------------------------------------------------------------------------------
public class ServerMessage
{
public long number;
public long bytes;
public bool retrieved;
public string message;
}
//---------------------------------------------------------------------------------
public class POPServer : System.Net.Sockets.TcpClient
{
//---------------------------------------------------------------------------------
public void Verbinden(string server, string username, string password)
{
string message;
string response;
Connect(server, 110);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
message = "USER " + username + "\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
message = "PASS " + password + "\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
}
// ------------------------------------------------------------------------------
public void Trennen()
{
string message;
string response;
message = "QUIT\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
}
// ---------------------------------------------------------------------------------
public ArrayList List()
{
string message;
string response;
ArrayList arl1 = new ArrayList();
message = "LIST\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
while (true)
{
response = Antwort();
if (response == ".\r\n")
{
return arl1;
}
else
{
ServerMessage smg = new ServerMessage();
char[] sps = { ' ' };
string[] values = response.Split(sps);
smg.number = Int32.Parse(values[0]);
smg.bytes = Int32.Parse(values[1]);
smg.retrieved = false;
arl1.Add(smg);
continue;
}
}
}
//Nachicht abholen -----------------------------------------------------------
public ServerMessage Abholen(ServerMessage svm)
{
string message;
string response;
ServerMessage msg = new ServerMessage();
msg.bytes = svm.bytes;
msg.number = svm.number;
message = "RETR " + svm.number + "\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
msg.retrieved = true;
while (true)
{
response = Antwort();
if (response == ".\r\n")
{
break;
}
else
{
msg.message += response;
}
}
return msg;
}
// Nachrichten löschen ------------------------------------------------------
public void Entfernen(ServerMessage svm)
{
string message;
string response;
message = "DELE " + svm.number + "\r\n";
Write(message);
response = Antwort();
if (response.Substring(0, 3) != "+OK")
{
throw new ServerException(response);
}
}
// Hilfsmethoden ------------------------------------------------------------
private void Write(string message)
{
ASCIIEncoding enc = new ASCIIEncoding();
byte[] WriteBuffer = new byte[2048];
WriteBuffer = enc.GetBytes(message);
NetworkStream stream = GetStream();
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
}
// --------------------------------------------------------------------------
private string Antwort()
{
ASCIIEncoding enc = new ASCIIEncoding();
byte[] arr1 = new Byte[2048];
NetworkStream stream = GetStream();
int count = 0;
while (true)
{
byte[] arr2 = new Byte[2];
int bytes = stream.Read(arr2, 0, 1);
if (bytes == 1)
{
arr1[count] = arr2[0];
count++;
if (arr2[0] == '\n')
{
break;
}
}
else
{
break;
}
}
string arrl1 = enc.GetString(arr1, 0, count);
return arrl1;
}
}
// Form12 End --------------------------------------------------------------------
}
}