How can send Email via Console Application ?

Rahul Rajpoot

Total Post:20

Points:284

Posted by  Rahul Rajpoot
C#
 940  View(s)
Ratings:
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["
    Username"],
                                                                          ConfigurationManager.AppSettings["
    Password"]);
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                    Console.WriteLine("Sending Email......");
                    smtp.Send(mm);
                    Console.WriteLine("Email Sent.");
                    System.Threading.Thread.Sleep(3000);
                    Environment.Exit(0);
                }
            }
        }
    }

    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("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    How can send Email via Console Application ?

    Output :- 

    How can send Email via Console Application ?

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.