Skip to content
IGeekFan 开发者文档IGeekFan 开发者文档
CMS
.NET指南
FreeKit
Docker
关于
博客
github icon
    • FreeKit
      • 基础包
        • aspnetcore identity freesql 的实现
          • Email 邮件
            • IGeekFan.FreeKit.Email
            • Extras 扩展包
              • Modularity 模块化
                • 本地化

                Email 邮件

                calendar icon2022年6月2日timer icon大约 1 分钟word icon约 360 字

                此页内容
                • IGeekFan.FreeKit.Email

                # Email 邮件

                # IGeekFan.FreeKit.Email

                该包是一个独立的开发包,内部由 MailKit 实现发送邮件。

                1. 安装包
                dotnet add package IGeekFan.FreeKit.Email
                
                1
                • ConfigureServices 方法
                    Services.Configure<MailKitOptions>(Configuration.GetSection("MailKitOptions"));
                
                1
                • appsettings.json
                  "MailKitOptions": {
                    "Host": "smtp.163.com",
                    "Port": "25",
                    "EnableSsl": true,
                    "UserName": "igeekfan@163.com",
                    "Password": "",
                    "Domain": ""
                  },
                
                1
                2
                3
                4
                5
                6
                7
                8
                • 业务逻辑
                    public interface IAccountService
                    {
                        Task SendEmailCode(RegisterDto registerDto);
                    }
                    public class AccountService : IAccountService
                    {
                        private readonly IEmailSender _emailSender;
                        private readonly MailKitOptions _mailKitOptions;
                
                        public AccountService(
                            IEmailSender emailSender,
                            IOptions<MailKitOptions> options)
                        {
                            _emailSender = emailSender;
                            _mailKitOptions = options.Value;
                        }
                        public async Task SendEmailCode(RegisterDto registerDto)
                        {
                            var message = new MimeMessage();
                            message.From.Add(new MailboxAddress(_mailKitOptions.UserName, _mailKitOptions.UserName));
                            message.To.Add(new MailboxAddress(registerDto.Nickname, registerDto.Email));
                            message.Subject = $"VVLOG-你的验证码是";
                
                            string uuid = Guid.NewGuid().ToString();
                            await RedisHelper.SetAsync("SendEmailCode-" + registerDto.Email, uuid, 30 * 60);
                
                            int rand6Value = new Random().Next(100000, 999999);
                
                            message.Body = new TextPart("html")
                            {
                                Text = $@"{registerDto.Nickname},您好!</br>你此次验证码如下,请在 30 分钟内输入验证码进行下一步操作。</br>如非你本人操作,请忽略此邮件。</br>{rand6Value}"
                            };
                
                
                            await _emailSender.SendAsync(message);
                        }
                    }
                
                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
                11
                12
                13
                14
                15
                16
                17
                18
                19
                20
                21
                22
                23
                24
                25
                26
                27
                28
                29
                30
                31
                32
                33
                34
                35
                36
                37

                简单的实体,在发送之前应验证必填项、密码强度和邮件格式等

                    public class RegisterDto
                    {
                        /// <summary>
                        /// 昵称
                        /// </summary>
                        public string Nickname { get; set; }
                        /// <summary>
                        /// 密码
                        /// </summary>
                        public string Password { get; set; }
                        /// <summary>
                        /// 邮件
                        /// </summary>
                        public string Email { get; set; }
                   }
                
                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
                11
                12
                13
                14
                15

                IEmailSender还不能直接使用。

                有二种方式

                1.使用 ASPNETCore 默认的 DI。

                    services.AddTransient<IEmailSender, EmailSender>();
                
                1

                2.由于继承了接口ITransientDependency,可通过IGeekFan.FreeKit.Extras依赖包中,默认的注册机制,通过 Autofac 配置继承此接口的接口将注入到依赖注入的集合中。

                • 增加依赖包
                dotnet add package IGeekFan.FreeKit.Extras
                
                1
                • 配置依赖
                builder.Host
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureContainer<ContainerBuilder>((webBuilder, containerBuilder) =>
                    {
                        containerBuilder.RegisterModule(new FreeKitModule(typeof(FreeKitModule), typeof(Program),typeof(MailKitOptions)))
                    });
                
                1
                2
                3
                4
                5
                6
                edit icon在 GitHub 上编辑此页open in new window
                上次编辑于: 2022/6/2 18:18:50
                贡献者: igeekfan
                上一页
                aspnetcore identity freesql 的实现
                下一页
                Extras 扩展包
                MIT Licensed | Copyright © 2021-present luoyunchong
                苏ICP备16046457号-1

                该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                详情