How can send Email via Console Application ?

Posted by  Nargish Rajpoot
 998  View(s)
Rate this:

Write a small program for knowledge. 

  1. Re: How can send Email via Console Application ?

    Step (1). Open your visual and select a new project(empty) window base.

    Step(2). Add a class file(.cs)

    How can send Email via Console Application ?

    Step (3). Write these code on that file (.cs)

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Configuration;

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

                using (MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["FromEmail"], to))
                    mm.Subject = subject;
                    mm.Body = body;
                    mm.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = ConfigurationManager.AppSettings["Host"];
                    smtp.EnableSsl = true;
                    NetworkCredential NetworkCred = new NetworkCredential(ConfigurationManager.AppSettings["
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                    Console.WriteLine("Sending Email......");
                    Console.WriteLine("Email Sent.");

    Note :- username is  a valid EmailID and password - real password

    How can send Email via Console Application ?

    Step (4). Add a properties folder ---> Add a class file as AssemblyInfo.cs and write these code on that.

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    // General Information about an assembly is controlled through the following
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("Send_Email_Console_CS")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("Send_Email_Console_CS")]
    [assembly: AssemblyCopyright("Copyright © 2017")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]

    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components. If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]

    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("64df0df6-5333-4529-8006-e9d3e83ef491")]

    // Version information for an assembly consists of the following four values:
    // Major Version
    // Minor Version
    // Build Number
    // Revision
    // You can specify all the values or you can default the Build and Revision Numbers
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("")]
    [assembly: AssemblyFileVersion("")]

    How can send Email via Console Application ?

    Output :- 

    How can send Email via Console Application ?