send mail in csharp

Sending an email in C# is relatively simple thanks to the System.Net.Mail namespace. The following code snippet shows a basic example of how to send an email using SMTP:

main.cs
using System.Net;
using System.Net.Mail;

string fromEmail = "sender@example.com";
string toEmail = "recipient@example.com";
string subject = "Test email";
string body = "Hello, world!";

MailMessage message = new MailMessage(fromEmail, toEmail, subject, body);

SmtpClient client = new SmtpClient("smtp.example.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("username", "password");

client.Send(message);
440 chars
16 lines

In this example, the MailMessage class is used to create the email message with the sender, recipient, subject, and body. The SmtpClient class is used to send the email via SMTP. The SMTP server hostname and port number are specified in the SmtpClient constructor. The EnableSsl property is set to true to enable SSL encryption if required by the SMTP server. Finally, the Credentials property is set to the username and password required by the SMTP server for authentication.

Note: Replace "smtp.example.com" with the hostname of your SMTP server and "username" and "password" with your SMTP server credentials.

gistlibby LogSnag