public bool Authenticate(SmtpLogin login) { bool fAuthenticated = false; Send("EHLO " + System.Net.Dns.GetHostName(), true); Send("EHLO " + System.Net.Dns.GetHostName(), true); if (IsResponseCode("250")) { Send("AUTH LOGIN", true); if (IsResponseCode("334")) { string username = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(login.Username)); string password = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(login.Password)); Send(username, true); Send(password, true); if (IsResponseCode("235")) { // TODO send a test mail through these credentials to verify it's correctness. This can be a false-positive aswell. fAuthenticated = true; } } Send("close", true); } return(fAuthenticated); }
static void Main(string[] args) { SmtpHost host = new SmtpHost() { Host = "smtp.somesite.com", Port = 25 }; SmtpLogin credentials = new SmtpLogin() { Username = "******", Password = "******" }; using (SmtpSocket auth = new SmtpSocket(host)) { auth.Connect(); if (auth.Authenticate(credentials)) { Console.WriteLine("Logged in"); } else { Console.WriteLine("Failed"); } } Console.WriteLine(" == Program Executed == "); Console.ReadLine(); }