C# is an object-oriented, component-oriented programming language. C# provides language constructs to directly support these concepts, making C# a natural language in which to create and use software components.
Like other general-purpose programming languages, C# can be used to create a number of different programs and applications: mobile apps, desktop apps, cloud-based services, websites, enterprise software and games.
// use namespace here using System.Net.Mail; using System.Net; namespace SMTPConnection { public class Program { // Main Function static void Main() { string smtpServer = "smtp.gmail.com"; int smtpPort = 587; // Call SendEmail Function SendEmail(smtpServer, smtpPort); } // Send Email Function private static void SendEmail(string smtpServer, int smtpPort) { SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort); smtpClient.EnableSsl = true; MailAddress mailAddressFrom = new MailAddress("demomailfrom@gmail.com"); MailAddress mailAddressTo = new MailAddress("demomailto@gmail.com"); MailMessage mailMessage = new MailMessage(mailAddressFrom, mailAddressTo); mailMessage.Body = "Body :- This is The Test Email"; mailMessage.Subject = "Subject :- Test Email"; NetworkCredential emailCredential = new NetworkCredential("demomailfrom@gmail.com", "demomailpassword"); smtpClient.Credentials = emailCredential; try { smtpClient.Send(mailMessage); } catch (Exception ex) { Console.WriteLine("Exception is:" + ex.ToString()); } Console.WriteLine("Email Sent"); } } }