阿杜
  • Updated on 1/10/2024 10:57:22 PM

Abp Framework Mail Delivery: Smooth Switching from SmtpClient to MailKit

1ay6lt-konuj.jpg

Abp Framework comes equipped with two built-in email senders: SmtpEmailSender based on SmtpClient, and MailKitSmtpEmailSender based on MailKit. By default, projects created using the application startup template use SmtpClient's SmtpEmailSender.

MailKit is an open-source, cross-platform email client library with robust functionality and improved compatibility. Therefore, when encountering issues that cannot be resolved using SmtpClient to send emails, switching to MailKit is a viable solution.

In a recent project, I encountered an error while using SmtpClient's SmtpEmailSender to send emails to users. The exception message was: System.Net.Mail.SmtpException: “Syntax error, command unrecognized. The server response was: ”

Through testing with the Abp official demo, I found that by using the same email configuration information, switching to MailKit's MailKitSmtpEmailSender successfully sent emails.

I conducted tests covering Gmail, 163 Mail, and QQ Mail. Sending emails with SmtpClient's SmtpEmailSender failed, while switching to MailKit's MailKitSmtpEmailSender succeeded.

Switching to MailKit - Step by Step

Switching to MailKit is straightforward. Simply install the Volo.Abp.MailKit package in your project (usually in the Domain project) and add the necessary dependencies:

No other code modifications are required; this module will automatically override SmtpClient's SmtpEmailSender.

Email Configuration

Whether it's Gmail, 163 Mail, or QQ Mail, all support the mechanism of application-specific passwords, enhancing the security of your email's main password.

Taking Gmail as an example, here's how to enable the application-specific password:

Log in to your Google account, navigate to the security page, and enter the two-step verification page.

At the bottom of the two-step verification page, find the application-specific password section.

Generate an application-specific password here and copy it for safekeeping.

In the Abp system's backend settings page, fill in your Gmail email information:

Now, your Abp program can successfully send emails to users.

Conclusion

Abp Framework employs a rich plugin mechanism, offering multiple sets of plugins for the same functionality based on the strength of the open-source community. Developers can also implement their own plugins according to their needs, making it extremely versatile and user-friendly.

Through this article, it is hoped that you will have a smoother understanding and resolution of potential email sending issues in Abp Framework, transitioning seamlessly from SmtpClient to MailKit.

Clicky