简体   繁体   中英

Email sender namespace reference for a .Net Core framework ASP.Net web application

I have an email sender working as shown below for a C# Console App with .Net Framework 4.6.1 as target framework for the application. However I have tried to reuse the code in a C# ASP.Net web project with .Net Core 1.0 as target framework but the namespace System.Net.Mail is not available in .Core framework.

Which reference does one add in a in a C# ASP.Net web project with .Net Core 1.0 so that email_sender is reusable? Alternatively is there reference to use for .Net Core 1.0 to send an email?

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

private static void email_sender()
{
    string filename = @"<attachment path>";
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("mainserver.domain.com");
    mail.From = new MailAddress("from@fromdomain.com");
    mail.To.Add("receiver@domain.com");
    mail.Subject = "Email subject";
    mail.Body = "Report";
    Attachment attachment = new Attachment(filename);
    mail.Attachments.Add(attachment);
    SmtpServer.Port = 25;
    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
    SmtpServer.EnableSsl = false;
    SmtpServer.Send(mail);
}

Don't use that class. The documentation itself warns that it's obsolete and "strongly recommends" people use MailKit or MimeKit. It even emits a compilation warning about this.

The latest MailKit version on NuGet is 2.06 and targets .NET Standard 1.3, 1.6 and 2.0 which means it can be used with any .NET Core version. If you check the project's Github Repo you'll see it provides a lot of email features that aren't available with System.Net.Mail.SmtpClient, like sending MIME messages, HTML bodies, etc.

For easy migration, MailKit allows explicitly casting a System.Net.Mail.MailMessage to a MimeMessage class. As a first step you could use the MailMessage code you already have and only change the SmtpClient-related code :

        using (var client = new MailKit.Net.Smtp.SmtpClient ()) {
            client.Connect ("mainserver.domain.com", 25, false);
            // Note: only needed if the SMTP server requires authentication
            client.Authenticate ("joey", "password");

            var message=(MimeMessage)mail;

            client.Send (message);
            client.Disconnect (true);
        }

The docs show how to create more complex messages , attach images, files etc using MimeMessage directly.

The example in docs shows how to construct a message with embedded images, an HTML body and attachtments. This simplified example shows only what's needed to add an attachment :

        var message = new MimeMessage ();
        message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
        message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
        message.Subject = "How you doin?";

        var builder = new BodyBuilder ();

        // Set the plain-text version of the message text
        builder.TextBody = @"Hey Alice,... ";

        // We may also want to attach a calendar event for Monica's party...
        builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");

        // Now we just need to set the message body and we're done
        message.Body = builder.ToMessageBody ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM