Send email, send email

Source: Internet
Author: User
Tags mailmessage smtpclient ssl connection

Send email, send email

Document directory

 

  • Introduction
  • IEmailSender
    • ISmtpEmailSender
    • NullEmailSender
  • Configuration
  • Integrate MailKit
    • Install
    • Integration
    • Usage
    • Customization

 

Introduction

Sending emails is a common task, which is required by almost every application. A basic framework is provided for sending emails and separating the mail service configurations.

 

IEmailSender

It is a service that can be simply used to send emails without understanding its details. Its usage is 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 person        task.AssignedTo = person;        //Send a notification email        _emailSender.Send(            to: person.EmailAddress,            subject: "You have 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. This method has several overloaded versions, and can also accept the reload of the MailMessage object (. net core is not available, because. net core does not contain SmtpClient and MailMessage ).

 

ISmtpEmailSender

There is also an ISmtpEmailSender, which extends IEmailSender and adds the BuildClient method to create a SmtpClient, and then you can directly use SmtpClient (. net core is not available, because. net core does not contain SmtpClient and MailMessage ). In most cases, ISmtpEmailSender is sufficient.

 

NullEmailSender

NullEmailSender is the implementation of the Null Object Design Pattern of IEmailSender. It can be used in unit testing and attribute dependency injection.

 

Configuration

The Mail sending system uses the setting management system to read the configuration of Mail sending. All the set names are defined as constants in the Abp. Net. Mail. EmailSettingNames class. The following are its values and descriptions:

  • Abp. Net. Mail.DefaultFromAddress: The default sender address (as shown in the preceding example ).
  • Abp. Net. Mail.DefaultFromDisplayName: Default name displayed by the sender (as shown in the preceding example ).
  • Abp. Net. Mail.Smtp. Host: The IP address or domain name of the SMTP server (127.0.0.1 by default ).
  • Abp. Net. Mail.Smtp. Port: SMTP server port (25 by default ).
  • Abp. Net. Mail.Smtp. UserName: The user name to be provided when the SMTP server requires authentication.
  • Abp. Net. Mail.Smtp. Password: Password required when the SMTP server requires authentication.
  • Abp. Net. Mail.Smtp. Domain: The domain name to be provided when the SMTP server requires authentication.
  • Abp. Net. Mail.Smtp. EnableSsl: Indicates whether an SMTP server is (true) or not (false) and requires an SSL connection (false by default ).
  • Abp. Net. Mail.Smtp. usedefacrecredentials: If it is True, use the default credential instead of the provided user and password (true by default ).

 

Integrate MailKit

Because. net core does not support standard System. net. mail. smtpClient, so we need a third-party vendor to send emails. Fortunately, MailKit is a good alternative to the default Smtpclient, and Microsoft also recommends using it.

The Abp. MailKit package is elegantly integrated into the ABC mail system, so you can still use IEmailSender through MailKit as in the previous method.

 

Install

First, install the ABC. MailKit package to your project:

Install-Package Abp.MailKit

 

Integration

Add the AbpMailKitModule dependency to your module:

[DependsOn(typeof(AbpMailKitModule))]public class MyProjectModule : AbpModule{    //...}

 

Usage

You can use IEmailSender as described earlier, because the Abp. MailKit package registers MailKit implementation for it. Also use the configuration defined above.

 

Customization

When creating the SmtpClient of MailKit, you may have additional configuration or customization. At this time, you can use your own implementation to replace the registration of the IMailKitSmtpBuilder interface, however, it is easier to inherit defamailmailkitsmtpbuilder. For example, if you want to provide a credential for all SSL connections, you can rewrite 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 IMailKitStmpBuilder interface with your above 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 the "using Abp. Configuration. Startup;" Declaration because the extension method of ReplaceService 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.