SMTP Console Tester with POP3 Support

by Gary Gagnon 14. February 2012 19:48

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)

Tags:

ASP.Net

SMTP Console Tester

by Gary Gagnon 16. January 2012 16:32

I recently wrote a console application to test various modes of SMTP. It's very basic and simple and I may expand it later.

It should be self-explanitory and only used as a basic testing utility.

This utility will NOT record any information entered. If you feel uncomfortable entering settings into some website or unknown app then this might work for you.

SMTPTester.zip (3.00 kb)

Here's the source code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

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. Exit program                      #");
                Console.WriteLine("########################################");
                Console.Write("#/> ");
                var selectedOption = Console.ReadLine();
                if (selectedOption == "4")
                {
                    break;
                }
                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;
                }

                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);
            }
        }
    }
}

 

All stuff is as-is, no guarentee, blah blah blah, contact me for things.

Tags: , ,

ASP.Net

TeamLab Version 3 Timer Update

by Gary Gagnon 16. August 2011 01:12

I have completed my update for the timer I created a while back for TeamLab 2.2. Here is the timer with full documentation contained in the download linked below for the latest version of TeamLab v3.1.21013.

I have documented the steps for adding this feature in the README.txt file included with the source files but to reiterate BACKUP YOUR DATA before doing this update. One of the updates is to a DB3 file (shown in the README) and I wouldn't want to be responsible for overwritting your data. I also included an SQL command for SQLite to update the database if you are adding this feature to an existing instance of TeamLab.

If you are a developer looking to impliment this feature I would recommend using your favorite text-comparison application to review the changes line by line and make sure they don't conflict with features you've implimented. I've included a file change list in the README for easy review.

If there's issues/bugs/complaints please let me know at gary@hazardbrick.com.

TeamLabV3TimerUpdate.zip (49.04 kb)

Tags: , , , ,

ASP.Net | TeamLab

ASP.Net 4.0 C# Facebook Login Integration with Application Services Template

by Gary Gagnon 6. April 2011 22:26

This is a follow up with one of my previous posts concerning Facebook in ASP.Net 4.0 and C#. I have created a template site that will be improved upon as time passes. For now I have made the source available here. If there are any improvements to be made please let me know.

All I did was create the site using the default site created by Visual Studio 2010 when you choose a Web Application project and setup a database on my local instance of Microsoft SQL Express 2008 R2. I'm sure the code is not the greatest at the moment but it seems to be working pretty well. I tried my best to follow the second diagram found here http://developers.facebook.com/docs/user_registration/flows/.

Here is the source.

FacebookASPNetUserAccounts.zip (602.36 kb)

View the sample site at http://facebook.hazardbrick.com

(Project created in Visual Studio 2010 and uses a dll from here http://json.codeplex.com/)

Once again contact me at gary@hazardbrick.com with questions, concerns, rants, interests, or any other thing.

EDIT:

Fixed issue with Facebook login window being blocked as a popup. Also fixed issue with being asked for a password when registering with Facebook.

Tags: , , , ,

ASP.Net | Facebook