65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (12 votes)

Dec 19, 2011

CPOL
viewsIcon

31614

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:

smtp.Dispose();

or use a using:

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.

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