|
|
using Myshipping.Core;
|
|
|
using Furion.DependencyInjection;
|
|
|
using Furion.DynamicApiController;
|
|
|
using Mapster;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using SqlSugar;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Furion.FriendlyException;
|
|
|
using Myshipping.Core.Entity;
|
|
|
using MailKit.Net.Imap;
|
|
|
using MailKit.Net.Pop3;
|
|
|
using MailKit.Security;
|
|
|
using MailKit;
|
|
|
using System;
|
|
|
using MailKit.Net.Smtp;
|
|
|
|
|
|
namespace Myshipping.Core.Service
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 用户邮箱账号服务
|
|
|
/// </summary>
|
|
|
[ApiDescriptionSettings(Name = "DjyUserMailAccount", Order = 1)]
|
|
|
public class DjyUserMailAccountService : IDjyUserMailAccountService, IDynamicApiController, ITransient
|
|
|
{
|
|
|
private readonly SqlSugarRepository<DjyUserMailAccount> _rep;
|
|
|
private readonly ILogger<DjyUserMailAccount> _logger;
|
|
|
|
|
|
public DjyUserMailAccountService(SqlSugarRepository<DjyUserMailAccount> rep, ILogger<DjyUserMailAccount> logger)
|
|
|
{
|
|
|
_rep = rep;
|
|
|
_logger = logger;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 分页查询用户邮箱账号
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpGet("/DjyUserMailAccount/page")]
|
|
|
public async Task<dynamic> Page([FromQuery] QueryDjyUserMailAccountInput input)
|
|
|
{
|
|
|
var entities = await _rep.AsQueryable()
|
|
|
.Where(m => m.CreatedUserId == UserManager.UserId)
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.MailAccount), u => u.MailAccount.Contains(input.MailAccount.Trim()))
|
|
|
.ToPagedListAsync(input.PageNo, input.PageSize);
|
|
|
return entities.XnPagedResult();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 增加用户邮箱账号
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpPost("/DjyUserMailAccount/add")]
|
|
|
public async Task<long> Add(AddDjyUserMailAccountInput input)
|
|
|
{
|
|
|
var cc = _rep.AsQueryable().Filter(null, true).Count(x => x.MailAccount == input.MailAccount);
|
|
|
if (cc > 0)
|
|
|
{
|
|
|
throw Oops.Oh(ErrorCode.EMAIL001);
|
|
|
}
|
|
|
|
|
|
var entity = input.Adapt<DjyUserMailAccount>();
|
|
|
await _rep.InsertAsync(entity);
|
|
|
return entity.Id;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 更新用户邮箱账号
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpPost("/DjyUserMailAccount/edit")]
|
|
|
public async Task<long> Update(UpdateDjyUserMailAccountInput input)
|
|
|
{
|
|
|
var cc = _rep.AsQueryable().Filter(null, true).Count(x => x.MailAccount == input.MailAccount && x.Id != input.Id);
|
|
|
if (cc > 0)
|
|
|
{
|
|
|
throw Oops.Oh(ErrorCode.EMAIL001);
|
|
|
}
|
|
|
|
|
|
var entity = input.Adapt<DjyUserMailAccount>();
|
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
|
return entity.Id;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除用户邮箱账号
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpPost("/DjyUserMailAccount/delete")]
|
|
|
public async Task Delete(GetDjyUserMailAccountInput input)
|
|
|
{
|
|
|
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
|
|
|
await _rep.DeleteAsync(entity);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取用户邮箱账号
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpGet("/DjyUserMailAccount/detail")]
|
|
|
public async Task<DjyUserMailAccount> Get([FromQuery] GetDjyUserMailAccountInput input)
|
|
|
{
|
|
|
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
|
|
|
}
|
|
|
|
|
|
///// <summary>
|
|
|
///// 获取用户邮箱账号列表
|
|
|
///// </summary>
|
|
|
///// <param name="input"></param>
|
|
|
///// <returns></returns>
|
|
|
//[HttpGet("/DjyUserMailAccount/list")]
|
|
|
//public async Task<dynamic> List([FromQuery] QueryDjyUserMailAccountInput input)
|
|
|
//{
|
|
|
// return await _rep.ToListAsync();
|
|
|
//}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 校验邮箱
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpGet("/DjyUserMailAccount/check")]
|
|
|
public async Task<dynamic> Check(long id)
|
|
|
{
|
|
|
var ma = await _rep.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id);
|
|
|
if (ma == null)
|
|
|
{
|
|
|
throw Oops.Oh("邮箱账号未找到");
|
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(ma.MailAccount)
|
|
|
|| string.IsNullOrEmpty(ma.Password)
|
|
|
|| string.IsNullOrEmpty(ma.ReceiveServer)
|
|
|
|| string.IsNullOrEmpty(ma.SmtpServer)
|
|
|
|| !ma.ReceivePort.HasValue
|
|
|
|| !ma.SmtpPort.HasValue)
|
|
|
{
|
|
|
throw Oops.Oh("请完整填写邮箱的账号、密码、收发件服务器及端口等信息");
|
|
|
}
|
|
|
|
|
|
var recOK = false;
|
|
|
var sendOK = false;
|
|
|
|
|
|
//接收
|
|
|
if (ma.UseImap == true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
using (var imapClient = new ImapClient())
|
|
|
{
|
|
|
imapClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
|
|
_logger.LogInformation($"正在验证IMAP收件服务器:{ma.ReceiveServer}:{ma.ReceivePort},SSL:{ma.ReceiveSSL}");
|
|
|
imapClient.Connect(ma.ReceiveServer, ma.ReceivePort.Value, ma.ReceiveSSL == true ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None);
|
|
|
_logger.LogInformation($"正在登录邮件服务器,账号:{ma.MailAccount}");
|
|
|
imapClient.Authenticate(ma.MailAccount, ma.Password);
|
|
|
|
|
|
var inbox = imapClient.Inbox;
|
|
|
inbox.Open(FolderAccess.ReadOnly);
|
|
|
_logger.LogInformation($"获取IMAP Folder成功,账号:{ma.MailAccount}");
|
|
|
inbox.Close();
|
|
|
|
|
|
imapClient.Disconnect(true);
|
|
|
recOK = true;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogInformation($"验证 {ma.MailAccount} IMAP收件服务器失败:{ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
using (var popClient = new Pop3Client())
|
|
|
{
|
|
|
popClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
|
|
_logger.LogInformation($"正在验证POP3收件服务器:{ma.ReceiveServer}:{ma.ReceivePort},SSL:{ma.ReceiveSSL}");
|
|
|
popClient.Connect(ma.ReceiveServer, ma.ReceivePort.Value, ma.ReceiveSSL == true ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None);
|
|
|
_logger.LogInformation($"正在登录邮件服务器,账号:{ma.MailAccount}");
|
|
|
popClient.Authenticate(ma.MailAccount, ma.Password);
|
|
|
|
|
|
var uids = popClient.GetMessageUids();
|
|
|
_logger.LogInformation($"获取POP3 UID列表成功,账号:{ma.MailAccount}");
|
|
|
|
|
|
popClient.Disconnect(true);
|
|
|
recOK = true;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogInformation($"验证 {ma.MailAccount} POP3收件服务器失败:{ex.Message}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//发送
|
|
|
try
|
|
|
{
|
|
|
using (var client = new SmtpClient())
|
|
|
{
|
|
|
_logger.LogInformation($"准备校验发送邮件{ma.MailAccount} {ma.SmtpServer} {ma.SmtpPort} {ma.SmtpSSL}");
|
|
|
await client.ConnectAsync(ma.SmtpServer, ma.SmtpPort.Value, ma.SmtpSSL.HasValue && ma.SmtpSSL.Value ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None);
|
|
|
await client.AuthenticateAsync(ma.MailAccount, ma.Password);
|
|
|
|
|
|
//var message = new MimeMessage();
|
|
|
//message.From.Add(MailboxAddress.Parse(ma.MailAccount));
|
|
|
//message.To.Add(MailboxAddress.Parse(ma.MailAccount));
|
|
|
//message.Subject = "大简云邮件发送校验";
|
|
|
//message.Sender = MailboxAddress.Parse(ma.MailAccount);
|
|
|
//var html = new TextPart("html")
|
|
|
//{
|
|
|
// Text = "大简云邮件发送校验"
|
|
|
//};
|
|
|
//message.MessageId = Guid.NewGuid().ToString();
|
|
|
//await client.SendAsync(message);
|
|
|
await client.DisconnectAsync(true);
|
|
|
|
|
|
sendOK = true;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogInformation($"验证 {ma.MailAccount} POP3收件服务器失败:{ex.Message}");
|
|
|
}
|
|
|
|
|
|
return new { recOK, sendOK };
|
|
|
}
|
|
|
}
|
|
|
}
|