在 C# wpf 中使用 pop3 接收到私人邮件服务器的邮件时出错



嘿伙计们,我们有一个私人电子邮件服务器在工作,我需要编写一个供所有开发人员使用的电子邮件客户端。我已经让它可以毫无问题地发送电子邮件,但我似乎无法让它检索电子邮件。我收到一条错误消息,指出根据验证过程,远程证书无效。我们在电子邮件服务器中使用代理,但他们在我的黑莓手机上设置了一个外部地址,它可以 100% 工作,我可以发送和接收电子邮件,但在此应用程序中它不会......我搜索了谷歌,得到我需要禁用我所做的 ssl 验证证书女巫,但我仍然收到错误。

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();
        }
    }
}

您应该使用接受 RemoteCertificateValidationCallback 参数的连接重载:

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
}

相关内容

  • 没有找到相关文章

最新更新