Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.86/5 (30 votes)
27 Feb 2012CPOL 208.5K   29   25
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Website Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthe smtp server requires secured connection Pin
Member 1419312129-Mar-21 22:03
Member 1419312129-Mar-21 22:03 
Questionerror Pin
Member 1465561614-Nov-19 2:23
Member 1465561614-Nov-19 2:23 
GeneralMy vote of 5 Pin
Javier Carrion26-Aug-16 9:04
Javier Carrion26-Aug-16 9:04 
QuestionSystem requirements Pin
michael_rut0213-Oct-15 5:30
michael_rut0213-Oct-15 5:30 
AnswerRe: System requirements Pin
N8tiv13-Oct-15 10:47
N8tiv13-Oct-15 10:47 
Questiondispose() Pin
Gideon Cole13-Mar-15 0:59
Gideon Cole13-Mar-15 0:59 
QuestionProblem Pin
Asad Marfani18-Oct-14 10:04
Asad Marfani18-Oct-14 10:04 
Questionvery nice Pin
yechengmiao11-Mar-14 22:11
yechengmiao11-Mar-14 22:11 
GeneralVery Cool 7 Simple 7 Helpful Pin
New DotNet Developer31-Jan-14 5:47
New DotNet Developer31-Jan-14 5:47 
GeneralMy vote of 5 Pin
Аslam Iqbal30-Mar-13 6:35
professionalАslam Iqbal30-Mar-13 6:35 
QuestionFailure sending message error.. Pin
burdz9-Oct-12 23:30
burdz9-Oct-12 23:30 
AnswerRe: Failure sending message error.. Pin
N8tiv10-Oct-12 0:13
N8tiv10-Oct-12 0:13 
AnswerRe: Failure sending message error.. Pin
Аslam Iqbal30-Mar-13 6:40
professionalАslam Iqbal30-Mar-13 6:40 
AnswerRe: Failure sending message error.. Pin
N8tiv2-Apr-15 15:42
N8tiv2-Apr-15 15:42 
GeneralJust a small hint ;) Pin
Nicholas Marty5-Mar-12 5:14
professionalNicholas Marty5-Mar-12 5:14 
GeneralRe: Just a small hint ;) Pin
N8tiv10-Apr-12 22:58
N8tiv10-Apr-12 22:58 
GeneralRe: thanks :) Pin
Mokhtar Ashour29-Dec-11 4:22
Mokhtar Ashour29-Dec-11 4:22 
GeneralReason for my vote of 5 Awesome Example for beginners. Pin
Balakrishnan Dhinakaran29-Feb-12 20:26
professionalBalakrishnan Dhinakaran29-Feb-12 20:26 
GeneralReason for my vote of 4 Nice example - and "simple" Pin
brother.gabriel29-Feb-12 12:10
brother.gabriel29-Feb-12 12:10 
GeneralReason for my vote of 5 simple and powerful Pin
Mokhtar Ashour29-Dec-11 4:23
Mokhtar Ashour29-Dec-11 4:23 
Generalsimple and easy, but the question is "is it secured"? I don'... Pin
Mokhtar Ashour27-Dec-11 9:38
Mokhtar Ashour27-Dec-11 9:38 
GeneralRe: SSL - Secured Socket Layer is enabled. I'm assuming it's enc... Pin
N8tiv29-Dec-11 0:39
N8tiv29-Dec-11 0:39 
GeneralReason for my vote of 5 good one! Pin
velt_99126-Dec-11 20:48
velt_99126-Dec-11 20:48 
GeneralRe: Thank You Pin
N8tiv29-Dec-11 0:39
N8tiv29-Dec-11 0:39 
GeneralCheck out http://www.systemnetmail.com/ Pin
Axel Rietschin19-Dec-11 9:59
professionalAxel Rietschin19-Dec-11 9:59 
Check out http://www.systemnetmail.com/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.