Reputation: 8652
I was using below c# code in Visual studio 2022 to read mails from exchange server folder testmail
and getting error as ERR Logon failure: unknown user name or bad password
at line below. telnet is working and even same account i added in microsoft outlook 365,so password also correct.Do i need to replace basic authentication with Oauth? if so how i can change below code since i already have tenant key
,app key
and clientSecret
string statCommand = $"STAT\r\n";
SendCommand(sslStream, statCommand);
Full Code is as below
string pop3Server = "pop3.test.corp.ae"; // Replace with your POP3 server
int pop3Port = 995; // SSL port
string username = "[email protected]";
string password = "1234ere";
// Path to the client certificate
string certificatePath = @"D:\test\test.crt";
string certificatePassword = "";
// System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
// Pop3Client pop3Client = new Pop3Client();
//pop3Client.Connect(pop3Server, pop3Port, true);
//pop3Client.Authenticate(username, password);
// Load the client certificate
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath);//certificatePassword);
// Connect to the POP3 server
using (TcpClient client = new TcpClient(pop3Server, pop3Port))
using (SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null))
{
// Authenticate using SSL with the client certificate
sslStream.AuthenticateAsClient(pop3Server, new X509CertificateCollection { clientCertificate }, System.Security.Authentication.SslProtocols.Tls12, false);
// Send POP3 commands to authenticate and retrieve emails
string loginCommand = $"USER {username}\r\n";
SendCommand(sslStream, loginCommand);
string passCommand = $"PASS {password}\r\n";
SendCommand(sslStream, passCommand);
}
static void SendCommand(SslStream sslStream, string command)
{
byte[] buffer = Encoding.ASCII.GetBytes(command);
sslStream.Write(buffer, 0, buffer.Length);
sslStream.Flush();
// Read server response (if needed)
byte[] responseBuffer = new byte[2048];
int bytesRead = sslStream.Read(responseBuffer, 0, responseBuffer.Length);
string response = Encoding.ASCII.GetString(responseBuffer, 0, bytesRead);
Console.WriteLine(response);
}
EDIT There was String Interpolation issue i have to replace @ with $... So now for
string loginCommand = $"USER {username}\r\n";
i am getting response as +OK The Microsoft Exchange POP3 service is ready
and for string passCommand = $"PASS {password}\r\n";
i am getting response as +OK
but for string statCommand = $"STAT\r\n";
I am getting response as -ERR Logon failure: unknown user name or bad password
Upvotes: 0
Views: 62