This article is mainly for you in detail the ASP. E-mail method, to tell you how to send e-mail in ASP., with a certain reference value, interested in small partners can refer to
Objective
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 describe them separately below.
Mailkit
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", "joey@friends.com")); To.add (New Mailboxaddress ("Mrs chanandler Bong", "chandler@friends.com")); 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, accept All SSL certificates (in case the server supports STARTTLS) client. Servercertificatevalidationcallback = (s,c,h,e) = true; Client. Connect ("smtp.friends.com", 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 ("foo@email.com"). to ("Bar@email.com", "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 ("bob@hotmail.com"). to ("somedude@gmail.com"). Subject ("Woo NuGet"). Usingtemplate (template, new {Name = "Luke", compliment = "Awesome"});
Email.defaultrenderer is to tell Fulentemail which renderer to use (you can also implement one yourself), and then provide a template template that contains Razor syntax, Then use usingtemplate to render the rendering.
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 ("foo@email.com"). to ("Bar@email.com", "Bob"). Subject ("Where to play in Sunday?") ") . Usingtemplatefromfile ($ "{directory.getcurrentdirectory}/emailtemplage.cshtml", new {Name = "Luke"})
Send mail using Mailgun
There may be some people on the mailgun is not clear, Mailgun is a foreign mail service company, such as the famous Github Mail service hosted on it, the free Maingun account can send 10000 emails a 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 ("sandboxcf5f41bbf2f84f15a386c60e253b5fe8.mailgun.org",/ /mailgun Level Two domain name "KEY-8D32C046D7F14ADA8D5BA8253E3E30DF"//Mailgun API key); Email.defaultsender = Sender;var email = email. From ("foo@email.com"). to ("Bar@email.com", "Bob"). Subject ("Where to play in Sunday?") ") . Body ("I want to go to the Forbidden City to play, how?" "); await email. SendAsync ();
Summarize
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.
"Recommended"
1. asp free Video Tutorial
2. asp Tutorials
3. Eon the ASP Basic video tutorial