FriendLinker

Location:HOME > Socializing > content

Socializing

How to Send Emails from Your Website Using C# and .NET

November 07, 2025Socializing4529
How to Send Emails from Your Website Using C# and .NET Effective email

How to Send Emails from Your Website Using C# and .NET

Effective email communication is vital for any website. With C# and .NET, you can implement a robust email sending system within your application. This article provides detailed steps and code snippets to help you send emails from your website. For a complete tutorial, please visit the official tutorial.

The Code for Sending Emails in C#

The first method for sending emails in C# involves using the MailMessage and SmtpClient classes:

MailMessage Msg  new MailMessage();SmtpClient smtp  new SmtpClient();  new NetworkCredential("", "");smtp.Port  587;smtp.EnableSsl  true;("receiver@");  new MailAddress("sender@");  "Test Subject";  "This is the body text.";(Msg);

Note: Make sure to replace the placeholder email addresses and properties with your actual ones.

A Simple C# Method to Send Emails

Here is a simple method in C# to send emails:

public void SendMail(string RecieverEmail, string body, string Subject){    var message  new MailMessage    {        From  new MailAddress("sender@"), // replace with valid value         To  { new MailAddress(RecieverEmail) },        Subject  Subject,        Body  body,        IsBodyHtml  true    };    using var smtp  new SmtpClient    {        Credentials  new NetworkCredential        {            UserName  "yourusername", // replace with your Gmail username             Password  "yourpassword" // replace with your Gmail password        },        Port  25,        EnableSsl  false    };    (message);}

To use this method, you need to provide the receiver's email, the email body, and the subject. Remember to replace the placeholder credentials with your actual credentials.

Using Gmail SMTP Server for Email Sending

If you prefer using Gmail's SMTP server for sending emails, here is a sample code:

var fromAddress  "your-email@";var toAddress  "receiver@";const string fromPassword  "yourpassword";string subject  "Test mail";var smtp  new SmtpClient{    Host  "",    Port  587,    EnableSsl  true,    DeliveryMethod  ,    UseDefaultCredentials  false,    Credentials  new NetworkCredential(fromAddress, fromPassword)};using (smtp){    (fromAddress, toAddress, subject, "This is the body text.");}

To authenticate with Gmail, you need to generate an App Password in your Google Account settings. This is necessary since using your regular Gmail password may not work due to security settings.

Conclusion

By following these steps and utilizing the provided code snippets, you can easily implement email sending functionality in your C# and .NET website. Ensure you adapt the codes to fit your specific requirements and security settings.

For a more detailed guide, refer to the tutorial provided at this link. This concise guide should help you get started with sending emails from your C# and .NET applications.