How does the. NET send the message?

Source: Internet
Author: User
Tags mailmessage net send smtpclient ssl connection
Document Directory

    • Brief introduction

    • Iemailsender

      • Ismtpemailsender

      • Nullemailsender

    • Configuration

    • Integrated Mailkit

      • Installation

      • Integration

      • Usage

      • Custom

Brief introduction

Sending mail is a very common task that almost every application needs. The ABP provides a basic framework for simply sending messages and separating them from the configuration of the Mail service.

Iemailsender

It is a service that you can simply use to send a message without needing to know its details, as follows:

public class taskmanager:idomainservice{private readonly Iemailsender _emailsender;    Public TaskManager (Iemailsender emailsender)    {        _emailsender = Emailsender;    } public void Assign (task task, person person)    {//assign task to the Persontask. AssignedTo = person;//send a notification email        _emailsender.send (            To:person. EmailAddress,            Subject: "You had a new task!",            Body: $ "A new task is assigned for you: <b>{task. Title}</b> ",            isbodyhtml:true);}}

We simply inject Iemailsender and use the Send method, which has several overloaded versions, which also have overloads that can accept MailMessage objects (. NET core is not available because. net Core does not contain SmtpClient and MailMessage).

Ismtpemailsender

There is also a ismtpemailsender, which expands the Iemailsender and adds the Buildclient method to create a smtpclient that can then be used directly with smtpclient (. NET core is not available, Because. NET core does not contain SmtpClient and MailMessage). The use of ismtpemailsender is sufficient in most cases.

Nullemailsender

Nullemailsender is an implementation of Iemailsender's null object design pattern, which can be used in unit testing and property dependency injection.

Configuration

Mail sending uses the settings management system to read the configuration of the mail sent, and all of the settings are defined in the form of constants in the Abp.Net.Mail.EmailSettingNames class. The following is its value and description:

    • Abp.Net.Mail. defaultfromaddress: The address of the default mail sender (as in the example above).

    • Abp.Net.Mail. defaultfromdisplayname: The default message sender displays the name (as in the example above).

    • Abp.Net.Mail. smtp.host: The IP or domain name of the SMTP server (default is 127.0.0.1).

    • Abp.Net.Mail. smtp.port: The port of the SMTP server (default is 25).

    • Abp.Net.Mail. smtp.username: The user name that needs to be provided when the SMTP server requires authentication.

    • Abp.Net.Mail. smtp.password: The password that is required when an SMTP server requires authentication.

    • Abp.Net.Mail. smtp.domain: The domain name that needs to be provided when the SMTP server requires authentication.

    • Abp.Net.Mail. Smtp.enablessl: Indicates that an SMTP server is (true) No (false) need to use an SSL connection (default = False).

    • Abp.Net.Mail. smtp.usedefaultcredentials: True to use the default credentials instead of the supplied user and password (default is true).

Integrated Mailkit

Because. NET core does not support standard System.Net.Mail.SmtpClient, we need third-party vendors to send mail, fortunately, Mailkit is a good substitute for the default SmtpClient, and Microsoft also recommends it.

The Abp.mailkit package is elegantly integrated into the ABP's mail delivery system, so you can still use the Iemailsender as in the previous way through Mailkit.

Installation

First, install the Abp.mailkit package to your project:

Install-package Abp.mailkit

Integration

Add Abpmailkitmodule dependencies to your module:

[DependsOn (typeof (Abpmailkitmodule))]public class myprojectmodule:abpmodule{//...}

Usage

You can use Iemailsender as described earlier, because the Abp.mailkit package registers the implementation of Mailkit for it. The configuration defined above is also used.

Custom

When creating Mailkit SmtpClient, you may have additional configuration or your own customizations, at which point you can replace the registration of the Imailkitsmtpbuilder interface with your own implementation, However, it is easier to inherit defaultmailkitsmtpbuilder. For example, you want to provide a credential for all SSL connections, in which case you can override the Configureclient method as follows:

public class Mymailkitsmtpbuilder:defaultmailkitsmtpbuilder{public Mymailkitsmtpbuilder ( Ismtpemailsenderconfiguration smtpemailsenderconfiguration)         : Base (smtpemailsenderconfiguration)    {    } protected override void Configureclient (SmtpClient client)    {client. Servercertificatevalidationcallback = (sender, certificate, chain, errors) = true;        Base. Configureclient (client);}    }

Then replace the implementation of the Imailkitstmpbuilder interface with your implementation in the Preinitialize method of your module:

[DependsOn (typeof (Abpmailkitmodule))]public class myprojectmodule:abpmodule{public override void Preinitialize ()    {        configuration.replaceservice<imailkitsmtpbuilder, mymailkitsmtpbuilder> ();} //...}

(Remember to add "using Abp.Configuration.Startup;" Declaration, because the Replaceservice extension method is defined in this namespace).

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: info-contact@alibabacloud.com 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.