Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.IO;
namespace POP3_Client
{
/// <summary>
/// Class POP3Client, (c) 2006 by Yanick Dickbauer
/// HTL Leonding, Austria
/// </summary>
class POP3Client
{
/// <param name="serverDNS">The Mailserver DNS adress</param>
/// <param name="port">The Port Number - Usally 110</param>
/// <param name="user">Your User Account</param>
/// <param name="passw">Your Password</param>
public POP3Client(string serverDNS, int port, string user, string passw)
{
this.serverDNS = serverDNS;
this.port = port;
this.user = user;
this.passw = passw;
}
/// <summary>
/// Connecting to the Server
/// If there are Emails on the Server, save to an ArrayList and delete it
/// </summary>
public void Receive()
{
//Connect and fetch an Stream
this.tcpClient = new TcpClient(this.serverDNS, this.port);
Stream stream = tcpClient.GetStream();
//Login at the Server
this.GetAnswer(stream);
WriteStringToStream("USER " + this.user + "\n", stream);
GetAnswer(stream);
WriteStringToStream("PASS " + this.passw + "\n", stream);
GetAnswer(stream);
}
private void WriteStringToStream(string s, Stream stream)
{
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
stream.Write(bytes, 0, bytes.Length);
}
private string GetAnswer(Stream stream)
{
//Read till the Stream is Empty
BinaryReader br = new BinaryReader(stream);
return br.ReadString();
}
#region private Field Members
private string serverDNS, user, passw;
private int port;
private TcpClient tcpClient; //my TcpClient who connects to the server
private int anzReceivedMails;
/// <summary>
/// An Array List that saves all my Received Emails
/// </summary>
private ArrayList receivedMails;
#endregion
}
}
static void Main(string[] args)
{
POP3Client client = new POP3Client("pop.eunet.at", 110, "yanick@dickbauer.at", "xxx");
client.Receive();
}
using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Collections;
using System.IO;
namespace POP3_Client
{
/// <summary>
/// Class POP3Client, (c) 2006 by Yanick Dickbauer
/// HTL Leonding, Austria
/// </summary>
class POP3Client
{
#region constructors
/// <param name="serverDNS">The Mailserver DNS adress</param>
/// <param name="port">The Port Number - Usally 110</param>
/// <param name="user">Your User Account</param>
/// <param name="passw">Your Password</param>
public POP3Client(string serverDNS, int port, string user, string passw)
{
this.serverDNS = serverDNS;
this.port = port;
this.user = user;
this.passw = passw;
}
#endregion
#region public Methods
/// <summary>
/// Connecting to the Server
/// If there are Emails on the Server, save to an ArrayList and delete it
/// </summary>
public void Receive()
{
//Connect and fetch an Stream
this.tcpClient = new TcpClient(this.serverDNS, this.port);
stream = tcpClient.GetStream(); //absorbing the greeting message
//Login at the Server
this.login();
//check how many emails are there
this.anzReceivedMails = checkAnzEmailsOnServer();
//save my Emails to this.receivedMails
this.receivedMails = new string[anzReceivedMails];
for (int i = 1; i <= anzReceivedMails; i++)
{
this.receivedMails[i-1] = this.getMail(i);
}
}
#endregion
#region private Methods
private string getMail(int number)
{
this.WriteStringToStream("RETR "+number+"\n");
string mail = this.GetAnswer();
return mail;
}
private int checkAnzEmailsOnServer()
{
int anz = 0;
this.WriteStringToStream("LIST\n");
string answer = this.GetAnswer();
int numberOfLines = answer.Split('\n').Length; //very unsafe!!
//ignore first line (+OK ...)
if (numberOfLines <= 1)
return 0;
for (int i = 1; i < numberOfLines; i++)
{
//split with ' '
try
{
//Look whether the actual line (and first column) is convertable to an integer
int a = Convert.ToInt32(answer.Split('\n')[i].Split(' ')[0]);
anz++;
}catch{}
}
return anz;
}
/// <summary>
/// Login at the Server
/// Throws an Exception if the login wasn't successfully!
/// </summary>
private void login()
{
string sUSER = "USER " + this.user + "\n";
string sPASS = "PASS " + this.passw + "\n";
this.GetAnswer(); //pick up the greeting stream
WriteStringToStream(sUSER); //login
this.GetAnswer();
WriteStringToStream(sPASS); //deliver the password
string answer = this.GetAnswer();
if (!answer.Contains("+OK")) //testing whether the login was successfully
throw new Exception("Username or Password is incorrect\nServer's Answer:\n"
+ answer); //throw an exception if not
}
private void WriteStringToStream(string s)
{
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
stream.Write(bytes, 0, bytes.Length);
}
private string GetAnswer()
{
//this solution is not very cleanly, but i don't know another
//TODO: Look for a better Solution *g*
int max = 50000;
byte[] buffer = new byte[max];
stream.Flush();
stream.Read(buffer, 0, max);
string s = Encoding.ASCII.GetString(buffer);
//remove all \0 chars
s = s.Trim('\0');
return s;
}
#endregion
#region private Field Members
private string serverDNS, user, passw;
private int port;
private TcpClient tcpClient; //my TcpClient who connects to the server
private Stream stream; //my Stream for communice with the server
private int anzReceivedMails;
/// <summary>
/// An Array that saves all my Received Emails
/// </summary>
private string[] receivedMails;
#endregion
#region Properties
public int CountMails
{
get
{
return this.anzReceivedMails;
}
}
public string[] Mails
{
get
{
return this.receivedMails;
}
}
#endregion
}
}
Testprogramm:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using POP3_Client;
namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
POP3Client client = new POP3Client("pop.eunet.at", 110, "yanick@dickbauer.at", "xxx");
client.Receive();
MessageBox.Show("Number of Mails: "+ client.CountMails.ToString());
for ( int i = 0; i < client.Mails.Length; i++)
{
MessageBox.Show(client.Mails[i],"Mail No. "+((int)(i+1)).ToString());
}
}
}
}