In this article I will explain with an example, how to send email using Console Application in C# and VB.Net.
This article will illustrate how to send email in
Console Application using
GMAIL SMTP server, C# and VB.Net.
Mail Server Settings in App.Config file
The following Mail Server settings need to be saved in the App.Config file.
Note: It is necessary to use the sender’s email address credentials while defining the
Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to
Gmail Username specified in credentials.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="sender@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="sender@gmail.com"
password="SenderGmailPassword"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo Email address.
SmptClient Class Properties
Following are the properties of the SmtpClient class.
Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password).
Port – Port Number of the SMTP server (Gmail: 587)
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Net.Configuration
Sending SMTP email using Console Application
When the Program is executed, the Recipient email address (To), Subject and Body are read as input from the User, while the Sender email address (from) value is fetched from the
SMTP section of the
App.Config file.
Then, all these values are set into an object of the MailMessage class.
An object of
SmtpClient class is created and the settings of the SMTP server such as
Host,
EnableSsl,
Username,
Password,
DefaultCredentials and
Port are fetched from the
SMTP section of the
App.Config file.
Finally, the email is sent using the Send function of the SmtpClient class object and success message is displayed.
C#
namespace Send_Email_Console_CS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter To Address:");
string to = Console.ReadLine().Trim();
Console.WriteLine("Enter Subject:");
string subject = Console.ReadLine().Trim();
Console.WriteLine("Enter Body:");
string body = Console.ReadLine().Trim();
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage(smtpSection.From, to))
{
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = smtpSection.Network.Port;
Console.WriteLine("Sending Email......");
smtp.Send(mm);
Console.WriteLine("Email Sent.");
System.Threading.Thread.Sleep(3000);
Environment.Exit(0);
}
}
}
}
}
VB.Net
Module Module1
Sub Main()
Console.WriteLine("Enter To Address:")
Dim to As String = Console.ReadLine().Trim()
Console.WriteLine("Enter Subject:")
Dim subject As String = Console.ReadLine().Trim()
Console.WriteLine("Enter Body:")
Dim body As String = Console.ReadLine().Trim()
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"),SmtpSection)
Using mm As MailMessage = New MailMessage(smtpSection.From, to)
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = False
Using smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = smtpSection.Network.Port
Console.WriteLine("Sending Email......")
smtp.Send(mm)
Console.WriteLine("Email Sent.")
System.Threading.Thread.Sleep(3000)
Environment.[Exit](0)
End Using
End Using
End Sub
End Module
Screenshots
Received Email
Downloads