Closed jvianafec closed 9 years ago
Every Toolkit method is described in the readme with a simple explanation. This is like I implemented the OTP:
class OTP:ITwoFactorCodeProvider
{
public string otpAuth = string.Empty;
private double m_mailretentionSeconds;
private string m_popServer;
private int m_popPort;
private string m_popUserName;
private string m_popPassword;
public OTP(double mailretentionSeconds, string popServer, int popPort, string popUserName, string popPassword)
{
m_mailretentionSeconds = mailretentionSeconds;
m_popServer = popServer;
m_popPort = popPort;
m_popUserName = popUserName;
m_popPassword = popPassword;
}
public async Task<string> GetTwoFactorCodeAsync()
{
await GetMail();
return await Task.FromResult<string>(otpAuth);
throw new NotImplementedException();
}
public async Task GetMail()
{
List<Mail> mailList = new List<Mail>();
Regex regex = new Regex(@"y=\([0-9]\)\([0-9]\)(\s|)\+(\s+|)[0-9]");
Match match = regex.Match("y=(4)(5)+6");
bool deleteMails = true;
while (otpAuth == string.Empty)
{
while (mailList.Count < 1)
{
MailProvider mailProv_get = new MailProvider(m_popServer, m_popPort, m_popUserName, m_popPassword);
mailList = mailProv_get.GetMails(deleteMails);
mailList.Reverse();
await Task.Delay(2000);
}
foreach (Mail mail in mailList)
{
DateTime dateMail = Convert.ToDateTime(mail.Date);
if (mail.SenderMail.Contains("ea.com") == true && DateTime.Now < dateMail.AddSeconds(m_mailretentionSeconds))
{
otpAuth = Convert.ToString(Convert.ToString(ConvertToInt(mail.Title)));
}
}
}
mailList.Clear();
MailProvider mailProv_delete = new MailProvider(m_popServer, m_popPort, m_popUserName, m_popPassword);
mailList = mailProv_delete.GetMails(deleteMails);
}
public async Task DeleteMail()
{
await Task.Delay(1);
bool deleteMails = true;
List<Mail> mailList = new List<Mail>();
MailProvider mailProv_get = new MailProvider(m_popServer, m_popPort, m_popUserName, m_popPassword);
mailList = mailProv_get.GetMails(deleteMails);
}
public static string ConvertToInt(String input)
{
Match match = Regex.Match(input, "-?[0-9]+");
if (match.Success)
{
return match.Value;
}
return match.Value;
}
}
@tringler Could you please show us your Mail.cs and MailProvider.cs ?
Thanks in advance! :)
I use this (a bit customized) snippet found on http://dotnet-snippets.de/snippet/pop3-e-mails-abrufen/1519 - Sorry for the German Resource and Commentary
using Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace Helper
{
/// <summary>
/// Stellt eine E-Mail dar.
/// </summary>
public class Mail
{
#region Vars
private string mSenderMail;
private string mReceiverMail;
private string mTitle;
private string mContent;
private string mDate;
#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; }
}
/// <summary>
/// Datum und Zeit
/// </summary>
public string Date
{
get { return mDate; }
set { mDate = value; }
}
#endregion
}
}
/// <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;
//Datum und Zeit
case "Date":
retVal.Date = value;
break;
}
}
return retVal;
}
#endregion
#region Public Methods
/// <summary>
/// Gibt alle E-Mails zurück.
/// </summary>
public List<Mail> GetMails(bool deleteMails)
{
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());
//Mail löschen
if (deleteMails == true)
{
mCommandBuffer = mAscEncoding.GetBytes(String.Format("DELE {0}\r\n", mailNr));
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
mStreamListener.ReadLine();
}
}
if (mail != null)
retVal.Add(mail);
}
mCommandBuffer = mAscEncoding.GetBytes(String.Format("QUIT\r\n"));
mPop3Stream.Write(mCommandBuffer, 0, mCommandBuffer.Length);
mStreamListener.ReadLine();
return retVal;
}
#endregion
}
This does not work for secure pop3 server, does it? At least I get an error everytime I try to check my gmx account...
Correct - This is not for secure POP3, but you won't have a problem to find a class for secure POP3
10 days with no reply, so i'm closing this one.
first I apologize, but I am beginner and would like to study the toolkit trydis, but I can not make it work. Can someone send me some simple design that works Login and already implemented ImapTwoFactorCodeProvider.cs class
my email: jvianafec@icloud.com
Many thanks to anyone who can help me somehow