C# — Imap via Telnet

So, connecting to Telnet via C#, most especially when the point of the whole thing is to connect to an IMAP server, is kind of a pain in the butt.  Many things I read in forums suggested giving up and paying for a library.  But finally, got this working.

using System.Net.Sockets;
using System.IO;
using System;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace ImapMain
{
class Program
{
static void Main(String[] args)
{
TcpClient oClient = new TcpClient();
try{
NetworkStream ns = TelnetOps.GetImapServer(ref oClient);
Console.WriteLine(TelnetOps.read(ns));
TelnetOps.write(ns, @”A2 LIST “””” “”*”””);
TelnetOps.write(ns, @”A3 SELECT “”INBOX”””);
TelnetOps.write(ns, @”A4 FETCH 1 ALL”);
//TelnetOps.write(ns, @”A4 FETCH 1 BODY[Header.FIELDS (Subject)]”);
//TelnetOps.write(ns, @”A4 FETCH 1 BODY[Text]”);
TelnetOps.write(ns, @”A4 FETCH 1 BODY[]”);
ProcessEmail(TelnetOps.read(ns));
TelnetOps.write(ns, Environment.NewLine);
TelnetOps.write(ns, “A5 LOGOUT”);
ns.Close();
oClient.Close();
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(Environment.NewLine + ex.Message);
if (oClient.Connected)
oClient.Close();
}
}

public static void ProcessEmail(String rawEmail)
{
String EmailBody = GetBody(rawEmail);
String SubjectOfEmail = GetSubject(rawEmail);
}

private static String GetSubject(String rawEmail)
{
int StartOfHeader = rawEmail.IndexOf(“Subject:”) + “Subject:”.Length;
String subjectOfEmail = rawEmail.Substring(StartOfHeader);
return subjectOfEmail.Substring(0, subjectOfEmail.IndexOf(“\r”)).Trim();
}

private static String GetBody(String rawEmail)
{
int StartOfBody = rawEmail.IndexOf(“Content-Type: text/plain;”);
int EndOfBody = rawEmail.IndexOf(“Content-Type: text/html;”);
String bodyOfEmail = rawEmail.Substring(StartOfBody, EndOfBody – StartOfBody);
StartOfBody = bodyOfEmail.IndexOf(“\n”);
EndOfBody = bodyOfEmail.IndexOf(“–“);
return bodyOfEmail.Substring(StartOfBody, EndOfBody – StartOfBody);
}
}
}

namespace ImapMain
{
public static class TelnetOps
{
public static NetworkStream GetImapServer(ref TcpClient oClient)
{
try
{
AppSettingsReader apps = new AppSettingsReader();
String TelnetServer = apps.GetValue(“TelnetServer”, typeof(System.String)).ToString();
String TelnetUsername = apps.GetValue(“TelnetUsername”, typeof(System.String)).ToString();
String TelnetPassword = apps.GetValue(“TelnetPassword”, typeof(System.String)).ToString();
oClient.Connect(TelnetServer, 143);
NetworkStream ns = oClient.GetStream();
TelnetOps.write(ns, String.Format(“A1 LOGIN {0} {1}”, TelnetUsername, TelnetPassword));
return ns;
}
catch (Exception exc)
{
throw exc;
}
}

public static string read(NetworkStream ns)
{
StringBuilder sb = new StringBuilder();
if (ns.CanRead)
{
byte[] readBuffer = new byte[1024];

int someBytes = 0;

do
{
someBytes = ns.Read(readBuffer, 0, readBuffer.Length);
sb.AppendFormat(“{0}”, Encoding.ASCII.GetString(readBuffer, 0, someBytes));
}
while (ns.DataAvailable);

}

return sb.ToString();
}

public static void write(NetworkStream ns, string message)
{
byte[] msg = Encoding.ASCII.GetBytes(message + Environment.NewLine);
ns.Write(msg, 0, msg.Length);
Console.WriteLine(Environment.NewLine + read(ns));
}
}
}

Advertisement
This entry was posted in .NET, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s