using System;
using System.Collections.Generic;
using System.Text;
using a = System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Collections;
using System.Net.Sockets;
using System.Diagnostics;
OpenPop.Pop3.Pop3Client objPOP3;
public class Pop3Exception : System.ApplicationException
{
public Pop3Exception(string str)
: base(str)
{
}
}
public class Pop3Message
{
public long number;
public long bytes;
public bool retrieved;
public string message;
}
public class Pop3 : System.Net.Sockets.TcpClient
{
ArrayList retval;
Pop3 obj;
public void Connect(string server, string username, string password)
{
string message;
string response;
Connect(server, 110);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
message = "USER " + username + "\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
message = "PASS " + password + "\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
}
public void Disconnect()
{
string message;
string response;
message = "QUIT\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
}
public ArrayList returnList()
{
return retval;
}
public Pop3 returnPOP3()
{
return obj;
}
public ArrayList List()
{
string message;
string response;
retval = new ArrayList();
message = "LIST\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
while (true)
{
response = Response();
if (response == ".\r\n")
{
return retval;
}
else
{
Pop3Message msg = new Pop3Message();
char[] seps = { ' ' };
string[] values = response.Split(seps);
msg.number = Int32.Parse(values[0]);
msg.bytes = Int32.Parse(values[1]);
msg.retrieved = false;
retval.Add(msg);
continue;
}
}
}
public Pop3Message Retrieve(Pop3Message rhs)
{
string message;
string response;
Pop3Message msg = new Pop3Message();
msg.bytes = rhs.bytes;
msg.number = rhs.number;
message = "RETR " + rhs.number + "\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
msg.retrieved = true;
while (true)
{
response = Response();
if (response == ".\r\n")
{
break;
}
else
{
msg.message += response;
}
}
return msg;
}
public void Delete(Pop3Message rhs)
{
string message;
string response;
message = "DELE " + rhs.number + "\r\n";
Write(message);
response = Response();
if (response.Substring(0, 3) != "+OK")
{
throw new Pop3Exception(response);
}
}
private void Write(string message)
{
System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding();
byte[] WriteBuffer = new byte[1024];
WriteBuffer = en.GetBytes(message);
NetworkStream stream = GetStream();
stream.Write(WriteBuffer, 0, WriteBuffer.Length);
Debug.WriteLine("WRITE:" + message);
}
private string Response()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] serverbuff = new Byte[1024];
NetworkStream stream = GetStream();
int count = 0;
while (true)
{
byte[] buff = new Byte[2];
int bytes = stream.Read(buff, 0, 1);
if (bytes == 1)
{
serverbuff[count] = buff[0];
count++;
if (buff[0] == '\n')
{
break;
}
}
else
{
break;
};
};
string retval = enc.GetString(serverbuff, 0, count);
Debug.WriteLine("READ:" + retval);
return retval;
}
}
// <--Clase Pop3Exception
// Llamada a la descarga de correo -->
public int POP3connect(string server, string username, string password)
{
try
{
objPOP3 = new Pop3();
objPOP3.Connect(server, username, password);
ArrayList list = objPOP3.List();
return objPOP3.List().Count;
}
catch (Pop3Exception E)
{
return 0;
}
}
public string POP3read(int n, ref string De, ref string Para, ref string Asunto, bool delete)
{
string Body="";
try
{
ArrayList list = objPOP3.returnList();
int i = 0;
foreach (Pop3Message msg in list)
{
if (i == n)
{
Pop3Message msg2 = objPOP3.Retrieve(msg);
String Mail = msg2.message;
//De
De = Mail.Substring(Mail.IndexOf("From: ", 1));
De = De.Substring(0, De.IndexOf("\r\n")).Replace("From: ", "");
//Para
Para = Mail.Substring(Mail.IndexOf("To: ", 1));
Para = Para.Substring(0, Para.IndexOf("\r\n")).Replace("To: ", "");
//Asunto
Asunto = Mail.Substring(Mail.IndexOf("Subject: ", 1));
Asunto = Asunto.Substring(0, Asunto.IndexOf("\r\n")).Replace("Subject: ", "");
//Body
Body = Mail.Substring(Mail.IndexOf("Content-Type: text/plain;", 1));
Body = Body.Substring(Body.IndexOf("\r\n\r\n") + 4);
Body = Body.Substring(0, Body.IndexOf("Content-Type: text/html;"));
if (delete ==true)
{
objPOP3.Delete(msg2);
}
break;
}
i = i + 1;
}
}
catch (Pop3Exception E)
{
}
return Body;
}
public void POP3disconnect()
{
try
{
objPOP3.Disconnect();
}
catch (Pop3Exception E)
{
}
}
// <-- Llamada a la descarga de correo
Y donde esta la declaracion de la variable objPOP3???
ResponderEliminar