Tag Archives: SmtpClient
Send email using C#
Posted on May 19, 2013 by CooLMinE No comments
Sending emails using C# is fairly easy. You require the host or the IP of an SMTP server that the email will be send through and a username/password if that server requires credentials.
For this example I will be providing a way to send emails using the Gmail SMTP server.
// This might not be required if the SMTP server you are sending the email through does not require credentials.
NetworkCredential credentials = new NetworkCredential("your username", "your password");
// The email address that will show where the email came from.
MailAddress fromAddress = new MailAddress("[email protected]");
// The email address that will receive the email.
MailAddress toAddress = new MailAddress("[email protected]");
using (MailMessage mail = new MailMessage(fromAddress, toAddress))
// If port 587 fails you might want to try 465 instead. The port might differ if you choose to use another SMTP server.
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Credentials = credentials;
client.Timeout = 10000; // Timeout set to 10 seconds. The default is 100,000 (100 seconds).
mail.ReplyTo = fromAddress;
mail.Subject = "The subject"; // The subject of the email.
mail.Body = "The content of the email"; // The content of the email.
client.Send(mail); // Send the email.
Posted in C#.
Tagged C#, csharp, email, MailAddress, MailMessage, NetworkCredential, SmtpClient, snippet, winforms