using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Share; using MailKit.Security; using MimeKit; namespace IRaCIS.Application.Services { public interface IMailVerificationService { Task SendMail(Guid userId, string userName, string emailAddress, int verificationCode); Task AnolymousSendEmail(string researchProgramNo ,string emailAddress, int verificationCode); Task SendMailEditEmail(Guid userId, string userName, string emailAddress, int verificationCode); Task AnolymousSendEmailForResetAccount( string emailAddress, int verificationCode); } public class MailVerificationService : IMailVerificationService { private readonly IRepository _verificationCodeRepository; private readonly IRepository _systemBasicDatarepository; public MailVerificationService(IRepository verificationCodeRepository, IRepository systemBasicDatarepository) { _verificationCodeRepository = verificationCodeRepository; _systemBasicDatarepository = systemBasicDatarepository; } public async Task SendMailEditEmail(Guid userId, string userName, string emailAddress, int verificationCode) { var messageToSend = new MimeMessage(); //发件地址 messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com")); //收件地址 messageToSend.To.Add(new MailboxAddress(userName, emailAddress)); //主题 messageToSend.Subject = "重置邮箱"; messageToSend.Body = new TextPart("plain") { Text = $@" {userName},您好: 感谢您使用展影云平台。 您正在进行邮箱重置操作,验证码是: {verificationCode},请在3分钟内输入该验证码,进行后续操作。如非本人操作,请忽略该邮件 此邮件属系统自动发出,无需回复。 上海展影医疗科技有限公司" }; using (var smtp = new MailKit.Net.Smtp.SmtpClient()) { smtp.MessageSent += (sender, args) => { // args.Response var code = verificationCode.ToString(); _ = _verificationCodeRepository.AddAsync(new VerificationCode() { CodeType = 0, HasSend = true, Code = code, UserId = userId, ExpirationTime = DateTime.Now.AddMinutes(3) }).Result; _ = _verificationCodeRepository.SaveChangesAsync().Result; }; smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls); await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"); await smtp.SendAsync(messageToSend); await smtp.DisconnectAsync(true); } } public async Task AnolymousSendEmailForResetAccount( string emailAddress, int verificationCode) { var messageToSend = new MimeMessage(); //发件地址 messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com")); //收件地址 messageToSend.To.Add(new MailboxAddress(String.Empty, emailAddress)); //主题 messageToSend.Subject = "重置密码"; messageToSend.Body = new TextPart("plain") { Text = $@" 您好: 感谢您使用展影云平台。 您正在进行邮箱重置密码操作,验证码是: {verificationCode},请在3分钟内输入该验证码,进行后续操作。如非本人操作,请忽略该邮件 此邮件属系统自动发出,无需回复。 上海展影医疗科技有限公司" }; using (var smtp = new MailKit.Net.Smtp.SmtpClient()) { smtp.MessageSent += (sender, args) => { // args.Response var code = verificationCode.ToString(); _ = _verificationCodeRepository.AddAsync(new VerificationCode() { CodeType = Core.Domain.Share.VerifyType.Email, HasSend = true, Code = code, UserId = Guid.Empty,//此时不知道用户 EmailOrPhone = emailAddress, ExpirationTime = DateTime.Now.AddMinutes(3) }).Result; _ = _verificationCodeRepository.SaveChangesAsync().Result; }; smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls); await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"); await smtp.SendAsync(messageToSend); await smtp.DisconnectAsync(true); } } public async Task AnolymousSendEmail(string researchProgramNo,string emailAddress, int verificationCode) { var messageToSend = new MimeMessage(); //发件地址 messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com")); //收件地址 messageToSend.To.Add(new MailboxAddress(String.Empty, emailAddress)); //主题 messageToSend.Subject = $"[{researchProgramNo}]中心调研"; messageToSend.Body = new TextPart("plain") { Text = $@" 感谢您使用展影云平台。 您正在参与展影医疗IRC项目中心调研工作,验证码是:: {verificationCode},请在3分钟内输入该验证码,进行后续操作。如非本人操作,请忽略该邮件。 此邮件属系统自动发出,无需回复。 上海展影医疗科技有限公司" }; using (var smtp = new MailKit.Net.Smtp.SmtpClient()) { smtp.MessageSent += (sender, args) => { // args.Response var code = verificationCode.ToString(); _ = _verificationCodeRepository.AddAsync(new VerificationCode() { CodeType = VerifyType.Email, HasSend = true, Code = code, UserId = Guid.Empty,//此时不知道用户 EmailOrPhone = emailAddress, ExpirationTime = DateTime.Now.AddMinutes(3) }).Result; _ = _verificationCodeRepository.SaveChangesAsync().Result; }; smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls); await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"); await smtp.SendAsync(messageToSend); await smtp.DisconnectAsync(true); } } public async Task SendMail(Guid userId, string userName, string emailAddress, int verificationCode) { var messageToSend = new MimeMessage(); //发件地址 messageToSend.From.Add(new MailboxAddress("GRR", "iracis_grr@163.com")); //收件地址 messageToSend.To.Add(new MailboxAddress(userName, emailAddress)); //主题 messageToSend.Subject = "Reset PassWord (Verification Code)"; messageToSend.Body = new TextPart("plain") { Text = $@"Hey {userName},you are resetting your password via email. The verification code is: {verificationCode}, which is valid within 3 minutes. If it is not your own operation, please ignore it! -- GRR" }; using (var smtp = new MailKit.Net.Smtp.SmtpClient()) { smtp.MessageSent += (sender, args) => { // args.Response var code = verificationCode.ToString(); _ = _verificationCodeRepository.AddAsync(new VerificationCode() { CodeType = 0, HasSend = true, Code = code, UserId = userId, ExpirationTime = DateTime.Now.AddMinutes(3) }).Result; _ = _verificationCodeRepository.SaveChangesAsync().Result; }; smtp.ServerCertificateValidationCallback = (s, c, h, e) => true; await smtp.ConnectAsync("smtp.163.com", 25, SecureSocketOptions.StartTls); await smtp.AuthenticateAsync("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"); await smtp.SendAsync(messageToSend); await smtp.DisconnectAsync(true); } } } }