Error recieving mail using pop3 in c# wpf to private mail server

650 views Asked by At

Hey guys we have a private email server at work and I need to write a email client to be used by all the developers. I have got it to send email without any problem but I cant seem to get it to retrieve email. I get a error saying the remote certificate is invalid according to the validation procedure. We use a proxy in the email server but they set up a outside address on my blackberry it works 100% I can send and receive email but in this application it wont... I searched google and got that I need to disable the ssl validation certificate witch I did but I still get the error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Mail;
using System.Configuration;
using System.Net;
using System.Net.Security;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Policy;

namespace VeriChat
{
    public partial class Mail : UserControl
    {

        string Email = "";
        string Password = "";

        public Mail()
        {
            InitializeComponent();
        }

        private void lblTime_Loaded(object sender, RoutedEventArgs e)
        {
            VeriChatMain v = new VeriChatMain();
            Email = v.Email;
            Password = v.Password;

            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();
            Retrieve();

        }

        private void timer_Tick(object sender, EventArgs e)
        {
            lblTime.Content = DateTime.Now.ToString();
        }

        private void btnNewEmail_Click(object sender, RoutedEventArgs e)
        {
            SendEmail se = new SendEmail();
            se.ShowDialog();
        }

        public void Retrieve()
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;
            };

            var client = new Pop3Client();
            client.Connect("example.com", 995, true);
            client.Authenticate("username", "password");

            var count = client.GetMessageCount();
            Message message = client.GetMessage(count);
            StreamWriter sw = new StreamWriter(@"downloads.txt", true);
            sw.WriteLine(message.Headers.Subject);
            sw.Close();
        }
    }
}
1

There are 1 answers

0
PashaPash On

You should use Connect overload that accepts RemoteCertificateValidationCallback parameter:

client.Connect("example.com", 995, true, 60000, 60000, 
    new RemoteCertificateValidationCallback(ValidateServerCertificate));

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{         
    return true;  // force the validation of any certificate
}