Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Send Authenticated Email – Console Application

Posted on March 19, 2015October 26, 2015 By David Kittell

Depending on your needs I would put the email settings in an app.config file rather than in the exe but for testing this will do what it needs to.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#region Send Authenticated Email - Using List
using System.Net.Mail;
#endregion Send Authenticated Email - Using List

namespace ConsoleApplication1
{
    class Program
    {
        #region Email Variables
        public static string sEmailSMTPServer = "<mail server IP or hostname>";
        public static string sEmailSMTPUser = "<email username>";
        public static string sEmailSMTPPassword = "<email password>";
        public static int nEmailSMTPPort = 465; // Verify correct port

        public static string sEmailFromAddress = "<email address>";
        public static string sEmailFromName = "<Name>";
        public static string sEmailToAddress = "<email address>";
        public static string sEmailToName = "<Name>";
        public static string sEmailSubject = "Test";
        public static string sEmailBody = "This is a test";
        #endregion Email Variables

        static void Main(string[] args)
        {

            MailMessage mailObj = new MailMessage();

            #region Set the message sender
            mailObj.From = new MailAddress(sEmailFromAddress, sEmailFromName);
            #endregion Set the message sender

            #region Set the message recipient(s)
            mailObj.To.Add(new MailAddress(sEmailToAddress, sEmailToName));
            //mailObj.CC.Add(new MailAddress(userName, "<Name>"));
            //mailObj.Bcc.Add(new MailAddress(userName, "<Name>"));
            #endregion Set the message recipient(s)

            #region Set the message subject
            mailObj.Subject = sEmailSubject;
            #endregion Set the message subject

            #region Set the message priority
            mailObj.Priority = MailPriority.High;
            // The email has high priority.

            // mailObj.Priority = MailPriority.Low;
            // The email has low priority.

            // mailObj.Priority = MailPriority.Normal;
            // The email has normal priority.
            #endregion Set the message priority

            #region Set the message body type
            mailObj.IsBodyHtml = true; // true or false
            #endregion Set the message body type

            #region Set the message server configuration
            SmtpClient SMTPServer = new SmtpClient(sEmailSMTPServer);
            SMTPServer.Credentials = new System.Net.NetworkCredential(sEmailSMTPUser, sEmailSMTPPassword);
            SMTPServer.UseDefaultCredentials = false;
            SMTPServer.Port = nEmailSMTPPort; // Make sure this is the correct email port
            #endregion Set the message server configuration

            try
            {
                SMTPServer.Send(mailObj);
                Console.Write("Message Sent");
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }
    }
}
Originally Posted on March 19, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code CSharp

Post navigation

Previous post
Next post

Related Posts

Clone A Menu Into Multiple Languages

Posted on October 23, 2013October 26, 2015

BEGIN TRANSACTION CREATE TABLE #NewMenu ( [mnu_id] [bigint] NOT NULL ,[mnu_name] [nvarchar](255) NULL ,[mnu_description] [nvarchar](255) NULL ,[folder_id] [bigint] NOT NULL ,[recursive] [int] NOT NULL ,[user_id] [bigint] NULL ,[date_created] [datetime] NOT NULL ,[last_edit_date] [datetime] NULL ,[last_edit_lname] [nvarchar](50) NOT NULL ,[last_edit_fname] [nvarchar](50) NOT NULL ,[mnu_type] [int] NOT NULL ,[mnu_link] [nvarchar](255) NULL ,[template_link]…

Read More

MySQL Server Information

Posted on September 11, 2015

SHOW VARIABLES; SHOW STATUS; select version(); SHOW STATUS provides server status information like Connections, Opened_tables, Bytes_received, Bytes_sent, etc. SHOW VARIABLES shows the values of MySQL system variables like time_zone, version, max_connections, etc. References: http://stackoverflow.com/questions/3939803/how-to-get-mysql-server-info-using-command-line All information on this site is shared with the intention to help. Before any source code…

Read More

JavaScript – Time Conversion – Millisecond (Execution/Run Time) To Human Readable

Posted on December 26, 2018December 26, 2018

Sometimes when you are looking at execution time you want to make it easier to report the amount of time. The script below will help. And a full example is below All information on this site is shared with the intention to help. Before any source code or program is…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes