How to use .NET Core Console app?

Console apps have been around since .NET’s inception i.e. 2002. Over the years, it has had a bit of a windows dressing with additional APIs, integration with .NET Core and the like.

Let’s look at a simple console app written in .NET Core that simply sends an email using the Net.Mail API.

using System;
using System.Net.Mail;

namespace SMTP
{
    class Program
    {
        static void Main(string[] args)
        {
            SendTestEmail();
            Console.WriteLine("The email was sent successfully!");
            Console.ReadLine();
        }

        private static void SendTestEmail()
        {
            MailMessage mail = new MailMessage("Obi@eOberoi.com", "ooberoi@hotmail.com");
            SmtpClient client = new SmtpClient
            {
                Port = 587,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = true,
                Host = "smtp.google.com",
                Credentials = new System.Net.NetworkCredential("Obi@gmail.com", "MyPassword")
            };
            
            mail.Subject = "Testing Console App!";
            mail.Body = "How is it going Obi!";
            client.Send(mail);
        }
        
    }

Links to similar blog posts (coming soon!):

  • Publishing .NET Core app.
  • Creating an .EXE in a Console App
  • Consuming Console App’s DLL externally (using Batch program)
  • Sending Email blasts using SendGrid
  • Using Windows Task Scheduler

About Obi Oberoi

Obi Oberoi is a Microsoft MVP (Visual Studio & Development Technologies), developer and a life-long student of continuous learning. Obi enjoys to code, read, and hang out with techies and geeks!
This entry was posted in .NET. Bookmark the permalink.