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)
19 Dec 2011 1  
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 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