Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple SMTP E-Mail Sender in C#… Console application

0.00/5 (No votes)
27 Feb 2012 1  
Simple SMTP E-Mail Sender in C#… Console application
I've been all over the Internet, as a beginner looking for examples for a simple e-mail sender program.
I was looking for a simple structure to follow. Something I could look at & say to myself, "okay, I think I can understand that".

Some examples I found were either outdated for today's .NET Network or there were over 100 lines of code & the article said, "Simple"…

My way of giving back is to post this article. I've read some articles here, "The Code Project & MSDN". Both places were very helpful regarding information and help.

For the old pros, I've been told I need to add some sort of garbage collection, which I will gladly do when I learn how.
For now, it's working… And it is, "Simple".
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Mail To");
        MailAddress to = new MailAddress(Console.ReadLine());

        Console.WriteLine("Mail From");
        MailAddress from = new MailAddress(Console.ReadLine());

        MailMessage mail = new MailMessage(from, to);

        Console.WriteLine("Subject");
        mail.Subject = Console.ReadLine();

        Console.WriteLine("Your Message");
        mail.Body = Console.ReadLine();

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;

        smtp.Credentials = new NetworkCredential(
            "username@domain.com", "password");
        smtp.EnableSsl = true;
        Console.WriteLine("Sending email...");
        smtp.Send(mail);
    }
}


As far as I know, you can use this with any SMTP server… More specifically the free ones, Gmail; Yahoo mail; AOL…
I have not tested it on a private server…

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here