This article is a mirror article of machine translation, please click here to jump to the original article.

View: 40028|Reply: 1

[.NET Core] Send messages with ASP.NET Core

[Copy link]
Posted on 5/3/2017 3:32:37 PM | | |
preface

We know that the current .NET Core does not support the SMTP protocol, when I use the sending function of the email, I need to use some third-party components to achieve the purpose, today I will introduce you to two open source email sending components, they are MailKit and FluentEmail, I will introduce them respectively.

MailKit

In ASP.NET Core, you can use MailKit to send messages, it supports cross-platform, IMAP, POP3, SMTP and other protocols.

You can install it using the following methods:


Here's a simple example of sending an email:



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




Fluent Email is also an open source project, you can use Razor templates to send emails, and you can integrate with some third-party email senders such as Mailgun, but this package only supports SMTP under .NET 4.6. You can install it using the following command:


You can send an email using the most basic methods, as simple as this:



Alternatively, you can use a Razor template to send:


Email.DefaultRenderer tells FulentEmail which renderer to use (you can also implement your own), then provides a template template with the content of the template string of Razor syntax, and then uses UsingTemplate to render the render.

CSHTML template on disk

If the Razor template file is large and not elegant to use strings, then you can put the template file on disk and load it as follows:




Send messages with Mailgun

Some people may not know much about Mailgun, Mailgun is a foreign mail service company, such as the famous Github email service is hosted on it, and the free Maingun account can send 10,000 emails per month, which is enough for many small and medium-sized websites.

When using Mailgun to send emails, you first need to register an account, and then you can use the Rest API provided by Mailgun to manage the emails you send or receive. Mailgun with FluentEmail integration only needs to add packages like this:


After registering Mailgun, you will be assigned an API Key and a second-level domain name, and you need to configure it as follows:




summary

From the above examples, we can see that MailKit and FluentEmail have their own advantages and disadvantages. The advantage of MailKit is that it supports many protocols and is cross-platform, but the disadvantage is that it does not provide support for Razor, and if you use Mailgun, you need to integrate it yourself. The advantage of FlentEmail is that it provides support for Razor templates and encapsulates Mailgun, but the disadvantage is that the SMTP protocol does not yet provide support for .NET Core.

In summary, if you use Mailgun to send emails, then FluentEmail is the choice you should choose, and if you want to use the SMTP protocol to link to your mail server to send emails, then you should use MailKit.





Previous:.net/c# Google Protocol Buffers tutorial
Next:Why is .net/c# Task slower than Thread?
Posted on 11/4/2021 1:57:02 PM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com