How to send emails in Dotnet

How to send emails in .NET and .NET Core with the standard System.Net.Mail namespace using Gmail

Kunal Saini
Kunal Saini
August 23, 2021
1 min read

Send Transactional emails With Confidence

Our dedicated support team works around the clock because transactional emails never stop.

Free Sign UpSet up in 2 minutes . No Credit Card Required

Introduction

.NET and .NET Core come with built-in support for sending emails through the System.Net.Mail namespace.

Prerequisites

  • Visual Studio installed on your system
  • A basic knowledge of .Net and C#

Send an Email

We will be using Google's SMTP servers for this example, you will need to enable Less secure app access on your profile's Security page: https://myaccount.google.com/lesssecureapps

var smtpClient = new SmtpClient("smtp.gmail.com")
{
  Port = 587,
  Credentials = new NetworkCredential("[email protected]", "yourpassword"),
  EnableSsl = true,
};

smtpClient.Send("[email protected]", "[email protected]", "Sending Email with .Net", "Hello World!");

There's an overloaded Send-method that accepts a MailMessage object to help build the email message:

MailMessage message = new MailMessage("[email protected]", "[email protected]", "Sending Email with .Net", "Hello World!");
NetworkCredential netCred = new NetworkCredential("[email protected]","yourpassword");
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = netCred;
smtpClient.Send(message);

Using Mailazy

Mailazy supports both SMTP and Email API. With SMTP, you can use either the SmtpClient class or the MailKit package. All you need to do is to replace the SMTP server, port, and credentials with those obtained from Mailazy.

using System.Net;
using System.Net.Mail;

class Example
{
  static  void  Main(string[] args)
  {
    var mail = new MailMessage();

    //set the addresses
    mail.From = new MailAddress("[email protected]", "sender");
    mail.To.Add("[email protected]");

    //set the content
    mail.Subject = "Sending with Mailazy";

    //set the plain text part
    var plainView = AlternateView.CreateAlternateViewFromString("Hello World!", System.Text.Encoding.UTF8,
    "text/plain");
    //then set the html part
    var htmlView = AlternateView.CreateAlternateViewFromString("<b>Hello World!</b>",
    System.Text.Encoding.UTF8, "text/html");
    mail.AlternateViews.Add(plainView);
    mail.AlternateViews.Add(htmlView);

    //send the message
    var smtp = new SmtpClient("smtp.mailazy.com", 587);
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("<access_key>", "<access_secret>");
    smtp.EnableSsl = true;
    smtp.Send(mail);
  }
}


Mailazy Docs

Integrate with Transactional email service in minutes

click here

Most Popular Tags

EngineeringSMTPShort ReadAPIsBest PracticesEmail SecurityEmailEmail TemplatesGolangMarketing Email



What is Mailazy?

Mailazy is a Transactional Email Platform specially built for developers which satisfies the requirement for use cases like Reset Password Emails, OTP Emails, Welcome Emails, and so on. Mailazy has built to serve their customers a high value transactional email service. Sending transactional emails shouldn’t be hard with the right transaction email service. With an email service provider or simple transactional email service like Mailazy, you can easily send and track your transactional emails. Our goal is to help you achieve high delivery rates and keep your transactional emails out of the spam folder. We are always there for whenever ypu need us.

Lets get started!

Kunal Saini

Kunal Saini

Kunal is a Lead Software Development Engineer at Mailazy. He graduated in Computer Science and considers himself a learner of life. He loves to play cricket, football and computer games.

View Profile