Commit 00a59c05 by 陶然

init

parents
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################
/.vs/Kivii.Biz.EmailPools/v17
/Src/bin/Debug
/Src/obj
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.EmailPools
{
internal class Configs
{
public const string TableNameEmailPool = "EML_EmailPools";
public const string TableNameSmtpConfig = "EML_SmtpConfigs";
public const string EmailPoolDbFolderPath = "/EmailPools/Files";
}
}
using Kivii.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kivii.EmailPools.Entities
{
/// <summary>
/// 发送状态
/// </summary>
public enum SendStatus
{
/// <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
{ }
public abstract class EmailPoolBase : EntityWithMetadata
{
#region 关联相关
[IgnoreUpdate]
[Required]
public Guid OwnerKvid { get; set; }
[StringLength(500)]
public string OwnerName { get; set; }
#endregion
[ApiMember(Description = "收件地址")]
[StringLength(100)]
public string ToAddress { get; set; }
[ApiMember(Description = "收件名称")]
[StringLength(100)]
public string ToName { get; set; }
[ApiMember(Description = "寄件地址")]
[StringLength(100)]
public string FromAddress { get; set; }
[ApiMember(Description = "寄件名称")]
[StringLength(100)]
public string FromName { get; set; }
[ApiMember(Description = "邮件内容")]
[StringLength(int.MaxValue)]
public string Body { get; set; }
[ApiMember(Description = "邮件主题")]
[StringLength(200)]
public string Subject { get; set; }
[ApiMember(Description = "邮件正文是否为 HTML 格式")]
public bool IsBodyHtml { get; set; }
[ApiMember(Description = "分类")]
[StringLength(50), Default("")]
public string Category { get; set; }
[ApiMember(Description = "是否发送")]
[InternalSetter]
public bool IsSended { get; set; }
[ApiMember(Description = "发送状态")]
public SendStatus SendStatus { get; set; }
[ApiMember(Description = "附件地址")]
[StringLength(1000)]
public string FilePath { get; set; }
[ApiMember(Description = "备注,最大3000字")]
[StringLength(3000), Default("")]
public string Remark { get; set; }
[InternalSetter]
[DefaultEmptyGuid]
public Guid OperatorKvid { get; set; }
[ApiMember(Description = "操作人")]
[InternalSetter]
[StringLength(100), Default("")]
public string OperatorName { get; set; }
[ApiMember(Description = "创建人Kvid ")]
[IgnoreUpdate]
[CurrentMemberKvid]
public Guid CreatorKvid { get; set; }
[ApiMember(Description = "创建人")]
[IgnoreUpdate]
[StringLength(50), CurrentMemberName]
public string CreatorName { get; set; }
[ApiMember(Description = "更新人Kvid ")]
[CurrentMemberKvid]
public Guid UpdaterKvid { get; set; }
[ApiMember(Description = "更新人")]
[StringLength(50), CurrentMemberName]
public string UpdaterName { 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>();
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;
}
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;
}
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;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FAC16B5D-CEB2-4CD3-94E3-3BADF8CECCE5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Kivii.EmailPools</RootNamespace>
<AssemblyName>Kivii.Biz.EmailPools.V4.5</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Kivii.Common.V4.5, Version=5.6.2023.4000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Kivii.Common.5.6.2023.4000\lib\net45\Kivii.Common.V4.5.dll</HintPath>
</Reference>
<Reference Include="Kivii.Core.V4.5, Version=5.6.2023.4000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Kivii.Core.5.6.2023.4000\lib\net45\Kivii.Core.V4.5.dll</HintPath>
</Reference>
<Reference Include="Kivii.Linq.V4.5, Version=5.6.2023.3000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Kivii.Linq.5.6.2023.3000\lib\net45\Kivii.Linq.V4.5.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<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>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Kivii.Biz.EmailPools.V4.5")]
[assembly: AssemblyDescription("电子邮箱推送")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Kivii.Biz.EmailPools.V4.5")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("fac16b5d-ceb2-4cd3-94e3-3badf8cecce5")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2024.4090")]
[assembly: AssemblyFileVersion("5.4.2023.4090")]
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Kivii.Common" version="5.6.2023.4000" targetFramework="net45" />
<package id="Kivii.Core" version="5.6.2023.4000" targetFramework="net45" />
<package id="Kivii.Linq" version="5.6.2023.3000" targetFramework="net45" />
</packages>
\ 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