using System.Net; using System.Net.Mail; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new NetworkCredential("[email protected]", "password"); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("[email protected]"); mailMessage.To.Add("[email protected]"); mailMessage.Subject = "Test Message"; mailMessage.Body = "This is a test email message."; client.Send(mailMessage); client.Dispose();
using System.Net; using System.Net.Mail; const string smtpHost = "smtp.example.com"; const int smtpPort = 587; const string smtpUsername = "username"; const string smtpPassword = "password"; SmtpClient client = new SmtpClient(smtpHost, smtpPort); client.EnableSsl = true; client.Credentials = new NetworkCredential(smtpUsername, smtpPassword); client.Authenticate += (sender, e) => { e.Authenticated = true; }; MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("[email protected]"); mailMessage.To.Add("[email protected]"); mailMessage.Subject = "Test Message"; mailMessage.Body = "This is a test email message."; client.Send(mailMessage); client.Dispose();This example connects to a custom SMTP server on port 587 using TLS encryption, and authenticates with a custom username and password. It then sets up a custom event handler to authenticate without sending any authentication commands to the server, and sends a test email message using a MailMessage object. Package library: System.Net.Mail