Tag Archives: MailMessage
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 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. // The email address that will receive the email. 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