// Mail.cs
namespace Tools
{
/// <summary>
/// Stellt eine E-Mail dar.
/// </summary>
public class Mail
{
#region Vars
private string mSenderMail;
private string mReceiverMail;
private string mTitle;
private string mContent;
#endregion
#region Properties
/// <summary>
/// E-Mail Adresse des Versenders
/// </summary>
public string SenderMail
{
get { return mSenderMail; }
set { mSenderMail = value; }
}
/// <summary>
/// E-Mail Adresse des Empfängers
/// </summary>
public string ReceiverMail
{
get { return mReceiverMail; }
set { mReceiverMail = value; }
}
/// <summary>
/// Betreff
/// </summary>
public string Title
{
get { return mTitle; }
set { mTitle = value; }
}
/// <summary>
/// Inhalt
/// </summary>
public string Content
{
get { return mContent; }
set { mContent = value; }
}
#endregion
}
}
// MailProvider.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace Tools
{
/// <summary>
/// Stellt einen MailProvider zur Verfügung,
/// der E-Mails abrufen kann.
/// </summary>
public class MailProvider
{
#region Vars
private NetworkStream mPop3Stream;
private StreamReader mStreamListener;
private byte[] mCommandBuffer = new byte[1024];
ASCIIEncoding mAscEncoding = new ASCIIEncoding();
private string mPopServer;
private int mPort;
private string mUser;
private string mPassword;
#endregion
#region Properties
/// <summary>
/// POP-3 Server
/// </summary>
public string PopServer
{
get { return mPopServer; }
set { mPopServer = value; }
}
/// <summary>
/// Port des Servers
/// </summary>
public int Port
{
get { return mPort; }
set { mPort = value; }
}
/// <summary>
/// Benutzername
/// </summary>
public string User
{
get { return mUser; }
set { mUser = value; }
}
/// <summary>
/// Passwort
/// </summary>
public string Password
{
set { mPassword = value; }
}
#endregion
#region Ctor
/// <summary>
/// Initialisiert einen neuen MailProvider,
/// der E-Mail empfangen kann.
/// </summary>
public MailProvider(string popServer, int port, string user, string pw)
{
// Zugangsinformationen zuweisen
mPopServer = popServer;
mPort = port;
mUser = user;
mPassword = pw;
// Verbindung zum Server herstellen
establishConnection();
}
#endregion
#region Private Methods
/// <summary>
/// Stellt die Verbindung zum POP-Server her.
/// </summary>
private void establishConnection()
{
TcpClient server = new TcpClient();
// Verbindung herstellen
server.Connect(this.PopServer, this.Port);
// Streams einleiten
mPop3Stream = server.GetStream();
mStreamListener = new StreamReader(mPop3Stream);
// Wenn erfolgreich verbunden
if (server.Connected)
{
mStreamListener.ReadLine();
// Am Server anmelden
mCommandBuffer = mAscEncoding.GetBytes(String.Format("USER {0}\r\n", this.User));
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
mStreamListener.ReadLine();
mCommandBuffer = mAscEncoding.GetBytes(String.Format("PASS {0}\r\n", mPassword));
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
mStreamListener.ReadLine();
}
}
/// <summary>
/// Parst anhand des übergebenen Strings ein Mail-Objekt.
/// </summary>
private Mail parseMail(string input)
{
Mail retVal = null;
// E-Mail in 2 Hälften zerlegen (Header und Body)
string[] emailSplit = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);
string header = String.Empty;
string body = String.Empty;
if (emailSplit.Length >= 2)
{
// Header
header = emailSplit[0];
// Body
for (int i = 1; i < emailSplit.Length - 1; i++) body = String.Concat(body, "\r\n\r\n" ,emailSplit[i]);
}
// Header-Informationen filtern
retVal = getHeaderInformations(header);
// Content zuweisen
retVal.Content = body;
// Mail-Objekt zurückgeben
return retVal;
}
/// <summary>
/// Gibt die Headerinformationen zurück.
/// </summary>
private Mail getHeaderInformations(string header)
{
Mail retVal = new Mail();
// Jede einzelne Zeile parsen
foreach (string line in header.Split(new string[] { "\r\n" }, StringSplitOptions.None))
{
// Aufteilen in Schlüssel und Wert
string[] lineSplit = line.Split(new string[] { ": " }, StringSplitOptions.None);
string key = String.Empty;
string value = String.Empty;
if (lineSplit.Length >= 2)
{
// Schlüssel
key = lineSplit[0];
// Wert
for (int i = 1; i < lineSplit.Length; i++) value = String.Concat(value, lineSplit[i]);
}
// Je nach Schlüssel den dazugehörigen Wert zuweisen
switch (key)
{
// Absender
case "From":
retVal.SenderMail = value;
break;
// Empfänger
case "To":
retVal.ReceiverMail = value;
break;
// Betreff
case "Subject":
retVal.Title = value;
break;
}
}
return retVal;
}
#endregion
#region Public Methods
/// <summary>
/// Gibt alle E-Mails zurück.
/// </summary>
public List<Mail> GetMails()
{
List<Mail> retVal = new List<Mail>();
List<string> mailList = new List<string>();
// Liste mit allen E-Mails anfordern
mCommandBuffer = mAscEncoding.GetBytes("LIST\r\n");
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
mStreamListener.ReadLine();
// Solange wie nicht "." ausgelesen wird, sind noch E-Mails
// auf dem Server vorhanden
string currMsg = mStreamListener.ReadLine();
while (currMsg != ".")
{
mailList.Add(currMsg);
currMsg = mStreamListener.ReadLine();
}
// Die E-Mail Liste durchgehen
foreach (string listVal in mailList)
{
Mail mail = null;
// Mail-Index filtern
int mailNr = -1;
try
{
mailNr = Convert.ToInt32(listVal.Split(' ')[0]);
}
catch { }
// Wenn der Mail-Index gefunden wurde, kompletten Mail Inhalt abrufen
if (mailNr != -1)
{
mCommandBuffer = mAscEncoding.GetBytes(String.Format("RETR {0}\r\n",
mailNr));
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
StringBuilder mailContent = new StringBuilder();
// Solange nicht "." augelesen wird, ist die Email noch nicht zuende
string tmpLine = mStreamListener.ReadLine();
while (tmpLine != ".")
{
mailContent.AppendLine(tmpLine);
tmpLine = mStreamListener.ReadLine();
}
// E-Mail parsen
mail = parseMail(mailContent.ToString());
}
if (mail != null)
retVal.Add(mail);
}
return retVal;
}
#endregion
}
}