Receiving Server Not Found Error using POP3 for downloading e-mails from Outlook

1.5k views Asked by At

Using POP3 I want to download mails from outlook but I am not able to connect with server. It is showing while connect to server. Error is:

Server not found at OpenPop.Pop3.Pop3Client.Connect(String hostname, Int32 port, Boolean useSsl, Int32 receiveTimeout, Int32 sendTimeout, RemoteCertificateValidationCallback certificateValidator) at Aceo.Activities.EmailPOP.RequestEmailPOP.Execute(CodeActivityContext context)

In parameters, I pass:

Host number="mail.xxx.com"

Use SSl=false or true

Port Number= 110 or 995

My code is:

public sealed class RequestEmailPOP:CodeActivity
{
    public InArgument<string> Host { get; set; }
    public InArgument<string> PortNumber { get; set; }
    public InArgument<string> UseSSL { get; set; }
    public InArgument<string> UserName { get; set; }
    public InArgument<string> Password { get; set; }
    public InArgument<string> LoggedInUser { get; set; }

    protected override void Execute(CodeActivityContext context)
    {

            string Host = context.GetValue(this.Host);
            int PortNumber =Convert.ToInt32(context.GetValue(this.PortNumber));
            bool UseSSL = Convert.ToBoolean(context.GetValue(this.UseSSL));
            string UserName = context.GetValue(this.UserName);
            string LoggedInUser = context.GetValue(this.LoggedInUser);
            string Password = context.GetValue(this.Password);
            using (Pop3Client client = new Pop3Client())
            {
                client.Connect(Host, PortNumber, UseSSL, 600, 600, CertificateValidationCallBack);
                client.Authenticate(UserName, Password);
                int messageCount = client.GetMessageCount();
                List<string> uids = client.GetMessageUids();
                List<Message> newMessages = new List<Message>();
                for (int i = 0; i < uids.Count; i++)
                {
                    string currentUidOnServer = uids[i];
                    if (!seenUids.Contains(currentUidOnServer))
                    {
                        Message unseenMessage = client.GetMessage(i + 1);
                        newMessages.Add(unseenMessage);
                    }
                }
           }
            return newMessage;
     } 

   private static bool CertificateValidationCallBack(
           object sender,
           System.Security.Cryptography.X509Certificates.X509Certificate certificate,
           System.Security.Cryptography.X509Certificates.X509Chain chain,
           System.Net.Security.SslPolicyErrors sslPolicyErrors)
          {
                  return true;
          }       

How do I resolve this error?

1

There are 1 answers

0
jstedfast On

You've probably figured this out by now, but it's likely that the problem is due to the extremely low timeout values you are passing in to the Connect() method (600 milliseconds). That's a really low timeout value and is probably not allowing enough time for the socket to connect to the remote host.

Now to plug my own alternative to OpenPOP.NET :-)

A lot of developers use timeouts as a solution to "network hangs" when a far better solution is to have a way to cancel the operation.

MailKit is the only mail library that allows this. Every single API that requires network activity takes an optional CancellationToken argument that gives you, the consumer of the API, the power to cancel that operation as needed.

This is an infinitely better solution than relying on arbitrary timeouts that almost never work as intended.

(Note: MailKit also allows setting timeouts, but I greatly discourage their use because they are almost always used incorrectly).

Hope that helps.