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
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.