Commit 3294a1c2 by Neo Turing

init

parent b52c479f
......@@ -8,10 +8,6 @@ namespace Kivii.EmailPools
{
internal class Configs
{
public const string TableNameEmailPool = "EML_EmailPools";
public const string TableNameSmtpConfig = "EML_SmtpConfigs";
public const string EmailPoolDbFolderPath = "/EmailPools/Files";
public const string TableNamePrefixEmailPool = "Emls";
}
}
......@@ -7,35 +7,13 @@ using System.Threading.Tasks;
namespace Kivii.EmailPools.Entities
{
/// <summary>
/// 发送状态
/// </summary>
public enum SendStatus
[Api(Description = "邮箱池")]
[Alias(Configs.TableNamePrefixEmailPool)]
public class EmailPool<T> : EmailPoolBase, IEntityInGenericTypeDb, IEntityHasRemark, IEntity, IEntityHasCreator, IHasCreator, IEntityHasUpdater, IHasUpdater
{
/// <summary>
/// 待发
/// </summary>
Sending,
/// <summary>
/// 完成
/// </summary>
Complete,
/// <summary>
/// 取消
/// </summary>
Cancel,
/// <summary>
/// 失败
/// </summary>
Fail
}
[Api(Description = "邮箱池")]
[Alias(Configs.TableNameEmailPool)]
public class EmailPool : EmailPoolBase,IEntityInAssemblyDb,
IEntityHasRemark,
IEntityHasCreator, IEntityHasUpdater
......@@ -86,10 +64,6 @@ namespace Kivii.EmailPools.Entities
[InternalSetter]
public bool IsSended { get; set; }
[ApiMember(Description = "发送状态")]
public SendStatus SendStatus { get; set; }
[ApiMember(Description = "附件地址")]
[StringLength(1000)]
public string FilePath { get; set; }
......
using Kivii.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.EmailPools.Entities
{
[Api(Description = "邮箱配置")]
[Alias(Configs.TableNameSmtpConfig)]
public class SmtpConfig: EntityWithMetadata, IEntityInAssemblyDb
{
[ApiMember(Description = "配置名称")]
[StringLength(200)]
public string Title { get; set; }
[ApiMember(Description = "内部编码")]
[Unique]
[StringLength(200)]
public string InternalCode { get; set; }
[ApiMember(Description = "默认配置")]
public bool IsDefault { get; set; }
[ApiMember(Description = "描述")]
[StringLength(500)]
public string Description { get; set; }
[ApiMember(Description = "smtp服务地址")]
[StringLength(300)]
public string SmtpUrl { get; set; }
[ApiMember(Description = "邮箱地址")]
[Unique]
[StringLength(300)]
public string Address { get; set; }
[ApiMember(Description = "邮箱友好名称")]
[StringLength(300)]
public string Name { get; set; }
[ApiMember(Description = "邮箱账号")]
[StringLength(300)]
public string UserId { get; set; }
[ApiMember(Description = "邮箱密码")]
[StringLength(300)]
public string Password { get; set; }
[ApiMember(Description = "摘要")]
[StringLength(1000)]
public string Summary { get; set; }
[ApiMember(Description = "备注")]
[StringLength(2000)]
public string Remark { get; set; }
}
}
using Kivii.EmailPools.Entities;
using Kivii.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.EmailPools
{
public static class Extensions
{
private static Dictionary<string, SmtpConfig> _smtpConfigs = new Dictionary<string, SmtpConfig>();
/// <summary>
/// 获取Smtp配置
/// </summary>
/// <param name="FromAddress"></param>
/// <returns></returns>
public static SmtpConfig GetSmtpConfig(this string FromAddress)
{
//默认key
var key = "default";
if (!FromAddress.IsNullOrEmpty())
{
try
{
//验证传入的邮箱地址格式是否正确,正确就赋值key,不正确key取默认值
var addr = new System.Net.Mail.MailAddress(FromAddress);
key = FromAddress;
}
catch { }
}
if (_smtpConfigs.ContainsKey(key))
{
return _smtpConfigs[key];
}
var conn = KiviiContext.GetOpenedDbConnection<SmtpConfig>();
var smtpConfigs = conn.Select<SmtpConfig>();
if (!smtpConfigs.IsNullOrEmpty())
{
var defaultConfig = smtpConfigs.FirstOrDefault(o => o.IsDefault);
if (defaultConfig == null) defaultConfig = smtpConfigs.FirstOrDefault();
_smtpConfigs["default"] = defaultConfig;
foreach (var item in smtpConfigs)
{
_smtpConfigs[item.Address] = item;
}
}
if (_smtpConfigs.ContainsKey(key))
{
return _smtpConfigs[key];
}
return null;
}
/// <summary>
/// 通过emialPool找到对应的附件
/// </summary>
/// <param name="pool"></param>
/// <returns></returns>
public static List<Attachment> GetAttachments(this EmailPool pool)
{
var conn=KiviiContext.GetOpenedDbConnection<EmailPool>();
var query = conn.From<EntityDbFile<EmailPool>>();
query.Where(o => o.ParentKvid == Guid.Empty&&o.DbFolderPath==Configs.EmailPoolDbFolderPath && o.OwnerKvid == pool.Kvid);
var dbFiles = conn.Select(query);
if (dbFiles.IsNullOrEmpty()) return null;
var rtns = new List<Attachment>();
foreach (var dbFile in dbFiles)
{
//获取到文件的实际存储路径
var physicalStorageFilePath = dbFile.GetPhysicalPath();
if (!File.Exists(physicalStorageFilePath)) continue;//判断此文件是否存在,不存在就跳过
var attach = new Attachment(physicalStorageFilePath);
rtns.Add(attach);
}
return rtns;
}
/// <summary>
/// 发送email
/// </summary>
/// <param name="pool"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static EmailPool Sending(this EmailPool pool)
{
var config = pool.FromAddress.GetSmtpConfig();
if (config == null) throw new Exception("未找到指定SMTP配置,请先配置SMTP服务信息");
MailAddress from = null;
MailAddress to = null;
try
{
from = new MailAddress(config.Address, config.Name);
}
catch
{
throw new Exception($"发送地址{config.Address},格式错误");
}
try
{
to = new MailAddress(pool.ToAddress, pool.ToName);
}
catch
{
throw new Exception($"收件地址{pool.ToAddress},格式错误");
}
SmtpClient smtp = new SmtpClient(config.SmtpUrl);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(config.UserId, config.Password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.From = from;
msg.To.Add(to);
msg.BodyEncoding = Encoding.UTF8;
msg.Body = pool.Body;
msg.Subject = pool.Subject;
msg.IsBodyHtml = pool.IsBodyHtml;
var attachs = pool.GetAttachments();
if (!attachs.IsNullOrEmpty())
{
foreach (var attach in attachs)
{
msg.Attachments.Add(attach);
}
}
try
{
smtp.Send(msg);
pool.SendStatus = SendStatus.Complete;
pool.AddOnlyProperties(o => o.SendStatus);
}
catch (SmtpException ex)
{
pool.SendStatus = SendStatus.Fail;
pool.AddOnlyProperties(o => o.SendStatus);
pool.Remark = ex.Message;
pool.AddOnlyProperties(o => o.Remark);
}
finally
{
pool.IsSended = true;
pool.AddOnlyProperties(o => o.IsSended);
}
return pool;
}
}
}
......@@ -52,8 +52,6 @@
<ItemGroup>
<Compile Include="Configs.cs" />
<Compile Include="Entities\EmailPool.cs" />
<Compile Include="Entities\SmtpConfig.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transforms\RestfulEmailPool.cs" />
</ItemGroup>
......
......@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2024.4090")]
[assembly: AssemblyFileVersion("5.4.2023.4090")]
\ No newline at end of file
[assembly: AssemblyVersion("5.4.2024.6190")]
[assembly: AssemblyFileVersion("5.4.2023.6190")]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment