Skip to content

How to configure SMTP server for sending emails

Elizabeth129 edited this page Apr 15, 2021 · 3 revisions

There are two ways to configure our server:

  1. If you want to use Standard Port

    • Use this method to connect to the SMTP server: client.ConnectAsync(hostName, port, SecureSocketOptions.StartTls); where port = 587
    • before connection write such code: client.CheckCertificateRevocation = false;
      For testing, you can use a fake SMTP service - https://ethereal.email/
      Here you can "Create Ethereal Account" and get your email address and password.
      Then set user secrets:
      • Go to the IdentityServer directory
      • Run this code: dotnet user-secrets set "EmailConfiguration:From" "some text"
      • Run this code: dotnet user-secrets set "EmailConfiguration:SmtpServer" "smtp.ethereal.email"
      • Run this code: dotnet user-secrets set "EmailConfiguration:Username" "your email"
      • Run this code: dotnet user-secrets set "EmailConfiguration:Password" "password by email"
  2. If you want to use an SSL port

    • Use this method to connect to the SMTP server: client.ConnectAsync(hostName, port, SecureSocketOptions.SslOnConnect); where port = 465
    • before connection write such code: client.CheckCertificateRevocation = false;
      For testing, you can use, for example, Gmail.
      You need to set user secrets:
      • Go to the IdentityServer directory
      • Run this code: dotnet user-secrets set "EmailConfiguration:From" "some text"
      • Run this code: dotnet user-secrets set "EmailConfiguration:SmtpServer" "smtp.gmail.com"
      • Run this code: dotnet user-secrets set "EmailConfiguration:Username" "your email"
      • Run this code: dotnet user-secrets set "EmailConfiguration:Password" "password by email"
        Also, you need to allow your Gmail account to send emails using external (less- secure applications). To enable this, go to https://myaccount.google.com/security and turn ON Less Secure App Access.
        Gmail allows you to send 2,000 emails per day using the Gmail SMTP Server for FREE.
        Set your email address and password to authenticate:
        client.AuthenticateAsync(UserName, Password);

For send an email write such code:
var message = new Message(new string[] { "recipient mail" }, "Some header", "This is the content from our email.");
_emailSender.SendEmailAsync(message);