I cannot authenticate with the email server

356 Views Asked by At

I am trying to get a code that I receive when I send a recovery email but I cannot seem to connect with the email server. This is what I have tried

using NUnit.Framework;
using PSelenium.Extensions;
using PSelenium.pageObjects;
using PSelenium.utilities;
using System;
using MailKit.Net.Imap;
using MailKit;
using MailKit.Search;
using System.Linq;

namespace PSelenium.tests
{
public class RecoveryEmailTest : Base
{

[SetUp]

public void LogginginTest()
{
    driver.Url = "https://myURL/";

    LoginPage loginPage = new LoginPage(getDriver());

    loginPage.LoggingIn("[email protected]", "MyPSw");
}


[Test]
public void recoveryEmailTest()
{
    driver.Url = "https://myURL/";

    RecoveryEmailPage recoveryEmailPage = new RecoveryEmailPage(getDriver());

    recoveryEmailPage.setRecoveryEmail("[email protected]");

    using (var client = new ImapClient())
    {
        client.Connect("imap.gmail.com", 993, true);

        client.Authenticate("[email protected]", "mypsw");

        var inbox = client.Inbox;
        inbox.Open(FolderAccess.ReadOnly);
        var query = SearchQuery.SubjectContains("Verify recovery email");
        var messages = inbox.Search(query);

        string code = null;
        if (messages.Count > 0)
        {
            var message = inbox.GetMessage(messages[0]);
            var body = message.Body;
            code = body.ToString().Trim();
        }

        if (!string.IsNullOrEmpty(code))
        {
            var codeInput = recoveryEmailPage.codeReceived;
            codeInput.SendKeys(code);
        }

        client.Disconnect(true);
    }

    recoveryEmailPage.saveChanges.Click();

    GeneralExtensions generalExtensions = new GeneralExtensions();
    generalExtensions.checkBackendResponse();
        }
    }
}

The error I get: Message:  `MailKit.Security.AuthenticationException : Invalid credentials (Failure)

Stack Trace: 
ImapClient.AuthenticateAsync(Encoding encoding, ICredentials credentials, Boolean doAsync,          CancellationToken cancellationToken)
ImapClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken       cancellationToken)
MailService.Authenticate(Encoding encoding, String userName, String password,       CancellationToken cancellationToken)
MailService.Authenticate(String userName, String password, CancellationToken  

I tried using different ports; I have turned off 2 factor authentication, I have enabled IMAP in my gmail account, I tried turning off firewalls and antivirus and I still get this error.`

1

There are 1 best solutions below

0
ericschambach On

This is what I have working for me:

using var client = new ImapClient
{
    ServerCertificateValidationCallback = (s, c, h, e) => true
};

await client.ConnectAsync("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync("[email protected]", "mypassword");
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);