ozcankrks.devbankai
TR
All posts

Sending Email with SMTP in C# (Gmail, Outlook)

Write the SMTP class once and use it in every project for verification codes, notifications or contact emails.

In this post we explain how to use SMTP mail, which lets us send verification codes, notification emails or contact messages from our projects. You can write the SMTP mail code once as a class and include it in your other projects. That's why I'll put a ready-made SMTP DLL at the end of the post, which you can download and add to your project directly.

First, to write the SMTP mail class, we need to add the System.Net.Mail library to our project.

C#
using System.Net.Mail;

After adding the library, we need to create our SMTP object so we can connect to the SMTP server. You can connect with your own mail server's details, or with Gmail or Outlook details.

C#
SmtpClient smtp = new SmtpClient();

After entering the sender and recipient settings through the properties of the SMTP object, we set the message, subject and sender with the mail object.

C#
smtp.Port = 587;smtp.Host = "smtp.gmail.com";smtp.EnableSsl = true;smtp.Credentials = new System.Net.NetworkCredential

("isim@gmail.com","parola");

Next, we create our mail object. 
 

C#
MailMessage mail = new MailMessage();mail.From = new MailAddress("eposta@gmail.com", "Ekranda Görünecek İsim");mail.To.Add("alici1@mail.com");mail.To.Add("alici2@mail.com");mail.CC.Add("alici3@mail.com");mail.CC.Add("alici4@mail.com");mail.Subject = "E-Posta Konusu";mail.IsBodyHtml = true;mail.Body = "E-Posta İçeriği";

Then we send the mail object with the Send method of our SMTP object.

smtp.Send(mail);

The complete code

C#
SmtpClient smtp = new SmtpClient();        smtp.Port = 587;        smtp.Host = "smtp.gmail.com";        smpt.EnableSsl = true;        smpt.Credentials = new NetworkCredential("eposta@gmail.com", "gmail_sifre");        MailMessage mail = new MailMessage();        mail.From = new MailAddress("eposta@gmail.com", "Ekranda Görünecek İsim");        mail.To.Add("alici1@mail.com");        mail.To.Add("alici2@mail.com");        mail.CC.Add("alici3@mail.com");        mail.CC.Add("alici4@mail.com");        mail.Subject = "E-Posta Konusu"; mail.IsBodyHtml = true; mail.Body = "E-Posta İçeriği";        smtp.Send(mail);

If you get an authentication error while sending through Gmail, sign in to that Gmail account using the link below and turn on the Less secure apps option. 

Click to change the less secure apps setting

You can download the project by clicking here. The DLL will be added soon.

Comments 0

No comments yet. Be the first.

Leave a comment

Never published.

© 2026 Özcan Karakuş · This site is built with Next.js and runs on my own server.