In this article I will explain with an example, about the System.Net.Mail.SmtpClient class which is now obsolete and what is the other way to send email in .Net 4.7 or higher and .Net Core.
 
 
SmtpClient is obsolete
According to Microsoft, the SmtpClient class is obsolete, doesn't support many modern protocols.
For more details please refer MSDN.
SmtpClient is obsolete. Other options to send email
 
 
Alternative way to send email in .Net 4.7 or higher and .Net Core
MailKit library is the other option to send email in .Net 4.7 or higher and .Net Core.
MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices.
 
 
Installing MailKit package
You will need to install the MailKit package, for details on installation please refer Install MailKit from Nuget in Visual Studio.
 
 
HTML Markup
The HTML Markup consists of:
TextBox – For capturing the values of Recipient Email address, Subject, Body, Gmail account email address, Gmail account password.
FileUpload – For Attaching files to the email.
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">To:</td>
        <td><asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Subject:</td>
        <td><asp:TextBox ID="txtSubject" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td valign="top">Body:</td>
        <td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>File Attachment:</td>
        <td><asp:FileUpload ID="fuAttachment" runat="server" /></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Gmail Email:</td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>Gmail Password:</td>
        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td></td>
        <td><asp:Button Text="Send" OnClick="SendEmail" runat="server" /></td>
    </tr>
</table>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using MimeKit;
using MailKit.Net.Smtp;
 
VB.Net
Imports System.IO
Imports MimeKit
Imports MailKit.Net.Smtp
 
 
Sending email using MailKit
MimeMessage Class Properties
Following are the required properties of the MimeMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies. (If any)
BCC – Blind Carbon Copies. (If any)
Subject – Subject of the Email.
Body – Body of the Email.
Attachments – Attachments. (If any)
ReplyTo – ReplyTo Email address.
 
SmtpClient Class Methods
Following are the methods of the SMTP class.
Connect – The connection to the SMTP Server is established using the domain and the port number.
Authenticate – The username and password of the SMTP Server is authenticated.
Send – The MimeMessage object is passed to it and the email is sent
Disconnect – Disconnects the connection with SMTP Server.
 
When the SendEmail Button is clicked, the Recipient email address (to), the Sender email address (from), Subject and Body values are fetched from their respective fields and are passed as parameter to SendEmail function.
Inside the SendEmail function all these parameters are set into an object of the MimeMessage class.
If the ASP.Net Multiple FileUpload control has attachment then the attachment is added to the Attachments List of the MimeMessage Object.
Finally, an object of the SmtpClient class is created and the methods of the Mail Server such as Connect, Authenticate, Send and Disconnect are executed.
C#
protected void SendEmail(object sender, EventArgs e)
{
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string password = txtPassword.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    HttpPostedFile postedFile = fuAttachment.PostedFile;
    this.SendEmail(to, from, password, subject, body, postedFile);
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
}
 
private void SendEmail(string to, string from, string password, string subject, string body, HttpPostedFile postedFile)
{
    using (MimeMessage mm = new MimeMessage())
    {
        mm.From.Add(new MailboxAddress("Sender", from));
        mm.To.Add(new MailboxAddress("Recepient", to));
        mm.Subject = subject;
        mm.Cc.Add(new MailboxAddress("Cc", "cc@aspsnippets.com"));
        mm.Bcc.Add(new MailboxAddress("Bcc", "bcc@aspsnippets.com"));
        BodyBuilder builder = new BodyBuilder();
        builder.TextBody = body;
        if (postedFile.ContentLength > 0)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            builder.Attachments.Add(fileName, postedFile.InputStream);
        }
        mm.Body = builder.ToMessageBody();
        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Connect("smtp.gmail.com", 587);
            smtp.Authenticate(from, password);
            smtp.Send(mm);
            smtp.Disconnect(true);
        }
    }
}
 
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
    Dim [to] As String = txtTo.Text
    Dim from As String = txtEmail.Text
    Dim password As String = txtPassword.Text
    Dim subject As String = txtSubject.Text
    Dim body As String = txtBody.Text
    Dim postedFile As HttpPostedFile = fuAttachment.PostedFile
    Me.SendEmail([to], from, password, subject, body, postedFile)
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Sub
 
Private Sub SendEmail(ByVal [to] As String, ByVal from As String, ByVal password As String, ByVal subject As String, ByVal body As String, ByVal postedFile As HttpPostedFile)
    Using mm As MimeMessage = New MimeMessage()
        mm.From.Add(New MailboxAddress("Sender", from))
        mm.[To].Add(New MailboxAddress("Recepient", [to]))
        mm.Subject = subject
        mm.Cc.Add(New MailboxAddress("Cc", "cc@aspsnippets.com"))
        mm.Bcc.Add(New MailboxAddress("Bcc", "bcc@aspsnippets.com"))
        Dim builder As BodyBuilder = New BodyBuilder()
        builder.TextBody = body
        If postedFile.ContentLength > 0 Then
            Dim fileName As String = Path.GetFileName(postedFile.FileName)
            builder.Attachments.Add(fileName, postedFile.InputStream)
        End If
        mm.Body = builder.ToMessageBody()
        Using smtp As SmtpClient = New SmtpClient()
            smtp.Connect("smtp.gmail.com", 587)
            smtp.Authenticate(from, password)
            smtp.Send(mm)
            smtp.Disconnect(True)
        End Using
    End Using
End Sub
 
 
Possible Errors
The following error occurs when you try to send email using Gmail credentials in your application.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
 
Solution
 
 
Screenshots
Email Form
SmtpClient is obsolete. Other options to send email
 
Received Email
SmtpClient is obsolete. Other options to send email
 
 
Downloads