I have update the SMTP Console Tester with a POP3 option.
Here's the code (hacked together and not great at all but here it is)
It uses the OpenPop library which is embedded in the exe using ilmerge.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using OpenPop.Pop3;
namespace SMTPTester
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("########################################");
Console.WriteLine("# Welcome to Gary's world class smtp #");
Console.WriteLine("# tester. Select your option. #");
Console.WriteLine("# 1. Unathenticated SMTP #");
Console.WriteLine("# 2. Authenticated SMTP #");
Console.WriteLine("# 3. Authenticated SMTP with TLS (SSL) #");
Console.WriteLine("# 4. Test POP Mailbox #");
Console.WriteLine("# 5. Exit program #");
Console.WriteLine("########################################");
Console.Write("#/> ");
var selectedOption = Console.ReadLine();
if (selectedOption == "5")
{
break;
}
if (selectedOption != "4")
{
Console.WriteLine("Enter the SMTP server");
Console.Write("#/> ");
var smtpServer = Console.ReadLine();
Console.WriteLine("Enter the SMTP port (default 25)");
Console.Write("#/> ");
var smtpPort = 25;
Int32.TryParse(Console.ReadLine(), out smtpPort);
Console.WriteLine("Enter the FROM address");
Console.Write("#/> ");
var mailFrom = Console.ReadLine();
Console.WriteLine("Enter the TO address");
Console.Write("#/> ");
var mailTo = Console.ReadLine();
Console.WriteLine("Enter the message subject");
Console.Write("#/> ");
var subject = Console.ReadLine();
Console.WriteLine("Enter the message body");
Console.Write("#/> ");
var body = Console.ReadLine();
switch (selectedOption)
{
case "1":
Console.WriteLine("Attempting to send mail");
var message = SendEmail(smtpServer, smtpPort, mailFrom, mailTo, subject, body);
Console.WriteLine(message);
break;
case "2":
Console.WriteLine("Please enter your username");
Console.Write("#/> ");
var username = Console.ReadLine();
Console.WriteLine("Please enter your password");
Console.Write("#/> ");
var password = Console.ReadLine();
Console.WriteLine("Attempting to send mail");
var authMessage = SendEmail(smtpServer, smtpPort, mailFrom, mailTo, subject, body, true, username, password);
Console.WriteLine(authMessage);
break;
case "3":
Console.WriteLine("Please enter your username");
Console.Write("#/> ");
var authUsername = Console.ReadLine();
Console.WriteLine("Please enter your password");
Console.Write("#/> ");
var authPassword = Console.ReadLine();
Console.WriteLine("Attempting to send mail");
var authSecureMessage = SendEmail(smtpServer, smtpPort, mailFrom, mailTo, subject, body, true, authUsername, authPassword, true);
Console.WriteLine(authSecureMessage);
break;
default:
Console.WriteLine("Incorrect option!");
break;
}
}
if (selectedOption == "4")
{
Console.WriteLine("Enter POP Server");
Console.Write("#/> ");
var popServer = Console.ReadLine();
Console.WriteLine("Enter POP Port");
Console.Write("#/> ");
var port = Int32.Parse(Console.ReadLine());
Console.WriteLine("Is SSL? (y/n)");
Console.Write("#/> ");
var isSSL = Console.ReadLine();
Console.WriteLine("Enter Username");
Console.Write("#/> ");
var username = Console.ReadLine();
Console.WriteLine("Enter Password");
Console.Write("#/> ");
var password = Console.ReadLine();
var client = new Pop3Client();
bool useSsl = false;
if (isSSL.ToLower() == "y") useSsl = true;
client.Connect(popServer, port, useSsl);
if (client.Connected)
{
bool authenticated = false;
if (client.ApopSupported)
{
try
{
client.Authenticate(username, password, AuthenticationMethod.Apop);
authenticated = true;
}
catch (Exception ex)
{
if(client.Connected)
try
{
client.Disconnect();
}
catch (Exception e) { }
}
}
if (!authenticated)
{
try
{
client.Connect(popServer, port, useSsl);
client.Authenticate(username, password, AuthenticationMethod.UsernameAndPassword);
authenticated = true;
}
catch (Exception ex)
{
if (client.Connected)
try
{
client.Disconnect();
}
catch (Exception e) { }
}
}
if (!authenticated)
{
try
{
client.Connect(popServer, port, useSsl);
client.Authenticate(username, password);
authenticated = true;
}
catch(Exception ex)
{
if (client.Connected)
try
{
client.Disconnect();
}
catch (Exception e) { }
Console.WriteLine(ex.Message);
}
}
if (authenticated)
{
var messageCount = client.GetMessageCount();
Console.WriteLine(String.Format("There are {0} messages in the mailbox.", messageCount));
client.Disconnect();
}
else
{
Console.WriteLine("Unable to authenticate. No really!");
}
}
else
{
Console.WriteLine("Could not connect");
}
}
Console.WriteLine("Press a key to continue...");
Console.ReadLine();
Console.Clear();
}
}
private static string SendEmail(string server, int port, string from, string to, string subject, string body, bool authenticate = false, string user = null, string password = null, bool secure = false)
{
var smtpClient = new SmtpClient(server, port);
var message = new MailMessage(from, to, subject, body);
if (authenticate)
{
var credentials = new NetworkCredential(user, password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
if (secure)
{
smtpClient.EnableSsl = true;
}
}
try
{
smtpClient.Send(message);
return "Message sent successfully";
}
catch (SmtpException ex)
{
return String.Format("There was an error. {0}", ex.Message);
}
}
}
}
Download The Below
SMTPTester.zip (25.90 kb)