Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!

I can receive mail from pop3+ssl server in window application with SslStream in .Net 2.0.
But in .Net CF, it doesn't support SslStream. So, which object i should to use to connect pop3+ssl server.
I want to write it by myself. I know i have to use TCPClient or Socket, NetworkStream, StreamReader, and X509Certificates but i don't know how to use X509Certificates to authenticate with server.
If have any example about pop3+ssl, pls share for me.

Thanks and Best Regards,

LuongHVM :)
Posted
Updated 4-May-10 15:07pm
v3

Hi all!

After research, i have solved it. See it for more details :)
Implementing a Secure Socket

My code after read it :)
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace SslTest
{
    public partial class Form1 : Form
    {
        
        private const ushort SO_SECURE = 0x2001;
        private const ushort SO_SEC_SSL = 0x2004;
        private const int _SO_SSL_VALIDATE_CERT_HOOK = 0x08;
        private const int SO_SSL_FAMILY = 0x00730000;
        private const long _SO_SSL = ((2L << 27) | SO_SSL_FAMILY);
        private const uint IOC_IN = 0x80000000;
        private const long SO_SSL_SET_VALIDATE_CERT_HOOK = (IOC_IN | _SO_SSL | _SO_SSL_VALIDATE_CERT_HOOK);
        public delegate int SSLVALIDATECERTFUNC(uint dwType, IntPtr pvArg, uint dwChainLen, IntPtr pCertChain, uint dwFlags);
        private IntPtr hookFunc;
        public Form1()
        {
            InitializeComponent();
        }
        private void btnReceive_Click(object sender, EventArgs e)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            
            socket.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)SO_SECURE, SO_SEC_SSL);
            hookFunc = Marshal.GetFunctionPointerForDelegate(new SSLVALIDATECERTFUNC(ValidateCert));

            byte[] inBuffer = new byte[8];
            byte[] hookFuncBytes = BitConverter.GetBytes(hookFunc.ToInt32());
            Array.Copy(hookFuncBytes, inBuffer, hookFuncBytes.Length);
            unchecked
            {
                socket.IOControl((int)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, null);
            }
            
            IPHostEntry hostEntry = Dns.GetHostEntry("xxx.xxx.xxx.xxx");
            IPEndPoint ip = new IPEndPoint(hostEntry.AddressList[0], 995);
            socket.Connect(ip);
            NetworkStream netStream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(netStream);
            MessageBox.Show(reader.ReadLine());
            netStream.Close();
            reader.Close();
            
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
        }
        private int ValidateCert(uint dwType, IntPtr pvArg, uint dwChainLen, IntPtr pCertChain, uint dwFlags)
        {
            return 0;
        }
    }
}


Hope to help someone! ;)

Best Regards,


LuongHVM
 
Share this answer
 
v3
Hi, your solution helped me a lot. But i can not integrate it with my code. Can you share how to get email with this?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900