Click here to Skip to main content
15,889,808 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
19 Dec 2011CPOL 30.4K   10   7
I'm not sure that GC would matter very much in such a simple app.But, it's either a call to Dispose on your SmtpClient after you're done using it:smtp.Dispose();or use a using:using (SmtpClient smtp = new SmtpClient{ Host = "smtp.gmail.com", Port = 587, Credentials = new...

I'm not sure that GC would matter very much in such a simple app.


But, it's either a call to Dispose on your SmtpClient after you're done using it:


C#
smtp.Dispose();

or use a using:


C#
using (SmtpClient smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    Credentials = new NetworkCredential("user@gmail.com", "password"),
    EnableSsl = true
})
{
    smtp.Send(mail);
}

The using will take care of calling Dispose for you.


Also, don't forget that there's a convenience method on the SmtpClient, if you don't need anything fancy on the message like HTML formatting or attachments.


C#
smtp.Send(from, to, subject, body);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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

 
GeneralI just wanted to say "Thanks"... Pin
Annyclerk28-Dec-11 1:01
Annyclerk28-Dec-11 1:01 
GeneralRe: My Pleasure Pin
N8tiv29-Dec-11 0:40
N8tiv29-Dec-11 0:40 
GeneralThat's where I did a lot of my practice was that MSDN. A per... Pin
N8tiv26-Dec-11 21:31
N8tiv26-Dec-11 21:31 
GeneralLooks like example from MSDN :) Pin
NeoPunk20-Dec-11 8:55
NeoPunk20-Dec-11 8:55 
GeneralI'm really new to programming in general. I guess I should h... Pin
N8tiv19-Dec-11 14:30
N8tiv19-Dec-11 14:30 
I'm really new to programming in general.
I guess I should have mentioned, "I needed something a bit more difficult then the traditional, 'Hello World Application' to practice & to look at"…
Something I could look at line by line & say to myself, "okay, I think I can understand that"…
I'm not trying to reinvent the wheel. I just needed a less confusing,, cleaner wheel to learn from.
I hope that made sense.
I will most likely build on this & I'm not sure what I'll start with… Maybe the ability to add an attachment, then later on incorporate HTML & an address book.
I'm a slow baby steps learner. Smile | :)
GeneralNice and clean. I like it. +5 Pin
fjdiewornncalwe19-Dec-11 10:29
professionalfjdiewornncalwe19-Dec-11 10:29 
GeneralRe: Thank You Pin
N8tiv29-Dec-11 0:40
N8tiv29-Dec-11 0:40 

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.