Hallo erstmal das hier ist mein erster Post, ich bin Schüler und für die Schule muss ich ein Programm in C# schreiben welches E-Mail abrufen kann.
Ich habe erstmal den Code von Microsoft angeguckt:
using System;
using System.Text;
namespace imapclient
{
class Program
{
static System.IO.StreamWriter sw = null;
static System.Net.Sockets.TcpClient tcpc = null;
static System.Net.Security.SslStream ssl = null;
static string username, password;
static string path;
static int bytes = -1;
static byte[] buffer;
static StringBuilder sb = new StringBuilder();
static byte[] dummy;
static void Main(string[] args)
{
try
{
path = Environment.CurrentDirectory + "\\emailresponse.txt";
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
sw = new System.IO.StreamWriter(System.IO.File.Create(path));
// there should be no gap between the imap command and the \r\n
// ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server
// cannot check for \r\n because in case of larger response from server ex:read email message
// there are lot of lines so \r \n appears at the end of each line
//ssl.timeout sets the underlying tcp connections timeout if the read or write
//time out exceeds then the undelying connection is closed
tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
ssl = new System.Net.Security.SslStream(tcpc.GetStream());
ssl.AuthenticateAsClient("imap.gmail.com");
receiveResponse("");
Console.WriteLine("username : ");
username = Console.ReadLine();
Console.WriteLine("password : ");
password = Console.ReadLine();
receiveResponse("$ LOGIN " + username + " " + password + " \r\n");
Console.Clear();
receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");
receiveResponse("$ SELECT INBOX\r\n");
receiveResponse("$ STATUS INBOX (MESSAGES)\r\n");
Console.WriteLine("enter the email number to fetch :");
int number = int.Parse(Console.ReadLine());
receiveResponse("$ FETCH " + number + " body[header]\r\n");
receiveResponse("$ FETCH " + number + " body[text]\r\n");
receiveResponse("$ LOGOUT\r\n");
}
catch (Exception ex)
{
Console.WriteLine("error: " + ex.Message);
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
if (ssl != null)
{
ssl.Close();
ssl.Dispose();
}
if (tcpc != null)
{
tcpc.Close();
}
}
Console.ReadKey();
}
static void receiveResponse(string command )
{
try
{
if (command != "")
{
if (tcpc.Connected)
{
dummy = Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0, dummy.Length);
}
else
{
throw new ApplicationException("TCP CONNECTION DISCONNECTED");
}
}
ssl.Flush();
buffer = new byte[2048];
bytes = ssl.Read(buffer, 0, 2048);
sb.Append(Encoding.ASCII.GetString(buffer));
Console.WriteLine(sb.ToString());
sw.WriteLine(sb.ToString());
sb = new StringBuilder();
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
}
Erhalte jedoch folgende Fehlermeldung> Fehlermeldung:
$ BAD Unkown command o26mb203706986wra
in der Konsole
Benutzername und Passwort sind richtig, IMAP Abruf ist auch aktiviert
Wichtig wäre zu wissen was davon alles funtktioniert und wo diese Meldung kommt.
Dieser komische String sieht mir auch nicht nach einem funktionierenden Befehl aus. Wo kommt der her? Das solltest du selbst herausfinden (können).
Bezweifle zwar stark, dass du das einfach so in einem Projekt verwenden darfst aber schau dir mal ImapX an. Wie der Name schon sagt gehts nur um das Ima-Protokoll und nicht Pop3. Allerdings kannst du dir dort im Code schon sehr viele Denkanstöße holen.
Beste Grüße
emuuu
2+2=5( (für extrem große Werte von 2)
emuuu, raten bringt nichts. Natürlich darfst Du das.
Oder glaubst Du Google bietet APIs an und sagt dann: ätsch, darfst Du aber nicht verwenden!
Hier scheint es aber um eine einfache Schulaufgabe zu gehen, weshalb sich es wohl um das Thema "E-Mail" handelt und nicht um das Thema Google API.
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code