ASP. NET Core Send mail

Source: Internet
Author: User


We know that the current. NET Core does not support the SMTP protocol, when I use the Send mail function, we need some third-party components to achieve the purpose, today we introduce two open source mail sending components, which are mailkit and Fluentemail , I'll introduce them separately.


in ASP. NET Core, you can use Mailkit to send mail, which supports cross-platform and supports protocols such as IMAP, POP3, SMTP, and so on.

You can install it using the following method:

Install-package Mailkit

Here's a simple example of sending a message:

var message = new MimeMessage (); message. From.add (New Mailboxaddress ("Joey Tribbiani", "")); To.add (New Mailboxaddress ("Mrs chanandler Bong", "")); Subject = "Where to play in Sunday?" "; message. Body = new Textpart ("plain") {Text = "I want to go to the Forbidden City to play, How to"};using (var client = new SmtpClient ()) {    //for demo-purposes, AC Cept all SSL certificates (in case the server supports STARTTLS)    client. Servercertificatevalidationcallback = (s,c,h,e) = true;    Client. Connect ("", 587, false);    Note:since we don ' t has an OAuth2 token, disable    //the XOAUTH2 authentication mechanism.    Client. Authenticationmechanisms.remove ("XOAUTH2");    Note:only needed If the SMTP server requires authentication    client. Authenticate ("Joey", "Password");    Client. Send (message);    Client. Disconnect (TRUE);}

If you want to send the Body content is HTML, you can use the following:

var bodybuilder = new bodybuilder (); bodybuilder.htmlbody = @ "<b>this is bold and this is <I>ITALIC</I>&L T;/b> "; message. Body = Bodybuilder.tomessagebody ();

Fluent Email

Fluent Email This is also an open source project, using it, you can use the Razor template to send mail, and can integrate some third-party mail sender such as Mailgun , but this package only under. NET 4.6 support SMTP. You can use the following command to install it:

Install-package Fluentemail.razor

You can use the most basic way to send mail, very simple as follows:

Note:. NET 4.6 Supports Email.defaultsender = new Smtpsender (); var email = email    . From ("")    . to ("", "Bob")    . Subject ("Where to play in Sunday?") ")    . Body ("I want to go to the Forbidden City to play, how?" "); await email. SendAsync ();

Alternatively, you can use the Razor template to send:

Note:. NET 4.6 Supports Email.defaultsender = new Smtpsender ();//Using Razor Templating Packageemail.defaultrenderer = new Razo Rrenderer (); var template = "Dear @Model. Name, you are totally @Model. compliment."; var email = email    . From ("")    . to ("")    . Subject ("Woo NuGet")    . Usingtemplate (template, new {Name = "Luke", compliment = "Awesome"});

Email.DefaultRendereris to tell Fulentemail which renderer to use (you can also implement one yourself), and then provide a template template with the Razor syntax, and then use it to render the rendering UsingTemplate .

cshtml Templates on disk

Add your mail Razor template file is larger, the words are not very elegant, so you can put the template file on disk, and then use the following way to load:

Note:. NET 4.6 Supports Email.defaultsender = new Smtpsender (); Email.defaultrenderer = new Razorrenderer (); var email = email    . From ("")    . to ("", "Bob")    . Subject ("Where to play in Sunday?") ")    . Usingtemplatefromfile ($ "{directory.getcurrentdirectory}/emailtemplage.cshtml", new {Name = "Luke"})

Send mail using Mailgun

There may be some people who are not very clear about Mailgun ,mailgun is a foreign mail service company, such as the famous Github Mail service hosted on it, the free Maingun account can send 10000 messages per month , for many small and medium-sized websites enough.

When using Mailgun to send mail, you first need to register an account, then you can use the Rest API provided by Mailgun to manage the messages sent or received. Using Fluentemail integrated Mailgun only needs to add the following packages:

Install-package Fluentemail.mailgun

After registering Mailgun will assign you an API Key and a two-level domain name, in the program type, you need the following configuration:

Supports both. NET Core and. NET Frameworkvar sender = new Mailgunsender (    " ",//Mailgun level two domain name    " KEY-8D32C046D7F14ADA8D5BA8253E3E30DF "/ /Mailgun API Key); Email.defaultsender = Sender;var email = email    . From ("")    . to ("", "Bob")    . Subject ("Where to play in Sunday?") ")    . Body ("I want to go to the Forbidden City to play, how?" "); await email. SendAsync ();


We can see from the above example that Mailkit and fluentemail have advantages and disadvantages. The advantage of Mailkit is that it supports more protocols and is cross-platform, but the disadvantage is that it does not provide support for razor, and it needs to be integrated if Mailgun is used. The advantage of Flentemail is that it provides support for the Razor template and encapsulates the Mailgun, with the disadvantage that the SMTP protocol does not yet provide support for. NET Core.

In summary, if you use Mailgun to send mail, then Fluentemail is you should choose, if you want to use the SMTP protocol link to your mail server to send mail, then you should use Mailkit.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.