connecting POP3 via http proxy using HigLabo

660 Views Asked by At

I'm using HigLabo to create an email client. Also I need to use an http proxy. But authentication fails every time. I found the base code here : .net pop3 over http proxy

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HigLabo.Net;
using HigLabo.Net.Pop3;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static Pop3Client pop;
        static Socket socket;

    static void Main(string[] args)
    {
        String proxyAddr = "112.241.212.104"; //This seemed to be working
        int proxyPort = 8585;
        byte[] buffer = new byte[25];

        pop = new Pop3Client("PopClient");
        socket = new Socket(AddressFamily.InterNetwork,
                                     SocketType.Stream,
                                     ProtocolType.Tcp);

        Console.WriteLine("Connecting to proxy...");
        socket.Connect(proxyAddr, proxyPort);
        Console.WriteLine("Connected to proxy");

        Console.WriteLine("Sending Packets...");
        socket.Send(Encoding.UTF8.GetBytes("CONNECT pop.mail.yahoo.com:995 HTTP/1.1<CR><LF>"));
        socket.Send(Encoding.UTF8.GetBytes("<CR><LF>"));
        Console.WriteLine("Packets sent");

        Console.WriteLine("Waiting for response...");
        socket.Receive(buffer);
        Console.WriteLine("Packets received");
        Console.WriteLine("Time Elapsed : " + timeElapsed + " seconds");

        Console.WriteLine("Connectong POP to socket...");
        if (pop.Connect(socket)) 
        {
            pop.Connect();
            Console.WriteLine("Connection completed");
        }
        else
        {
            Console.WriteLine("Disconnected");
            Disconnect();
            return;
        }

        pop.Ssl = true;
        pop.UserName = "EMAIL_ADDRESS";
        pop.Password = "PASSWORD";
        pop.ServerName = "pop.gmail.com";
        pop.Port = 995;

        Console.WriteLine("Authenticating...");
        if (pop.Authenticate())
        {
            Console.WriteLine("Authentication completed"); //Never comes here
            GetMail();
        }
        else
        {
            Console.WriteLine("Authentication failed"); //Always comes here
            Disconnect();
        }


    }

    private static void GetMail()
    {
        HigLabo.Mime.MailMessage msg = pop.GetMessage(1);
        Console.WriteLine(msg.BodyText);
    }

    static void Disconnect()
    {            
        Console.WriteLine("Disconnecting...");
        pop.Close();
        socket.Close();
        Console.WriteLine("Disconnected");
    }
}

}

I gave permission for the app also. What is wrong here?

Are there any other/better/easy ways to do this? May be with a different library?

1

There are 1 best solutions below

0
On BEST ANSWER

This is wrong: <CR><LF>.

What you want to use is \r\n in your strings.