Skip to content

Commit

Permalink
mail+
Browse files Browse the repository at this point in the history
  • Loading branch information
HeBianGu committed Jan 30, 2024
1 parent 6d3024b commit c270449
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 19 deletions.
11 changes: 9 additions & 2 deletions Solution/WPF-Control.sln
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Controls.ColorPicker", ".
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Extensions.Unit", "..\Source\Extensions\H.Extensions.Unit\H.Extensions.Unit.csproj", "{E4ECBFB2-BCD6-4BEB-B88E-932E3587E04B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "H.Controls.QRCoderBox", "..\Source\Controls\H.Controls.QRCoderBox\H.Controls.QRCoderBox.csproj", "{AEC475E3-607E-4290-93B6-5EDFE0229F6C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Controls.QRCoderBox", "..\Source\Controls\H.Controls.QRCoderBox\H.Controls.QRCoderBox.csproj", "{AEC475E3-607E-4290-93B6-5EDFE0229F6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "H.Controls.ScheduleBox", "..\Source\Controls\H.Controls.ScheduleBox\H.Controls.ScheduleBox.csproj", "{24785305-3565-4A79-B83D-CC990D20C42B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Controls.ScheduleBox", "..\Source\Controls\H.Controls.ScheduleBox\H.Controls.ScheduleBox.csproj", "{24785305-3565-4A79-B83D-CC990D20C42B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "H.Extensions.Mail", "..\Source\Extensions\H.Extensions.Mail\H.Extensions.Mail.csproj", "{7374B023-7B0F-4F6E-9E43-C6B81B8DB564}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -751,6 +753,10 @@ Global
{24785305-3565-4A79-B83D-CC990D20C42B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24785305-3565-4A79-B83D-CC990D20C42B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24785305-3565-4A79-B83D-CC990D20C42B}.Release|Any CPU.Build.0 = Release|Any CPU
{7374B023-7B0F-4F6E-9E43-C6B81B8DB564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7374B023-7B0F-4F6E-9E43-C6B81B8DB564}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7374B023-7B0F-4F6E-9E43-C6B81B8DB564}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7374B023-7B0F-4F6E-9E43-C6B81B8DB564}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -877,6 +883,7 @@ Global
{E4ECBFB2-BCD6-4BEB-B88E-932E3587E04B} = {09F8FD00-E76A-4D72-AAB4-7FA06DBE12D4}
{AEC475E3-607E-4290-93B6-5EDFE0229F6C} = {9E63ED70-6F96-4CB2-8B9D-EAEC77D0548E}
{24785305-3565-4A79-B83D-CC990D20C42B} = {9E63ED70-6F96-4CB2-8B9D-EAEC77D0548E}
{7374B023-7B0F-4F6E-9E43-C6B81B8DB564} = {09F8FD00-E76A-4D72-AAB4-7FA06DBE12D4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DF825C3D-F4F4-4E62-BBFF-B42A5E81112F}
Expand Down
20 changes: 20 additions & 0 deletions Source/Extensions/H.Extensions.Mail/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Windows;
using System.Windows.Markup;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,SMRZQEVBPSMJXTNG
// app, or any theme specific resource dictionaries) BMJGKKNPMBWLIOEO
)]

[assembly: XmlnsDefinition("QQ:908293466", "H.Extensions.Mail")]
[assembly: XmlnsPrefix("QQ:908293466", "h")]

[assembly: XmlnsDefinition("https://github.com/HeBianGu", "H.Extensions.Mail")]
[assembly: XmlnsPrefix("https://github.com/HeBianGu", "h")]

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "H.Extensions.Mail")]
[assembly: XmlnsPrefix("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "h")]
32 changes: 32 additions & 0 deletions Source/Extensions/H.Extensions.Mail/Extention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

using H.Extensions.Mail;
using H.Providers.Ioc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace System
{
public static class Extention
{
/// <summary>
/// 注册
/// </summary>
/// <param name="service"></param>
public static IServiceCollection AddMail(this IServiceCollection services, Action<SmtpSendOptions> setupAction = null)
{
services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IMailService, MailService>());
services.TryAdd(ServiceDescriptor.Singleton<IMailLogService, MailLogService>());
if (setupAction != null)
services.Configure(setupAction);
return services;
}

public static IApplicationBuilder UseMail(this IApplicationBuilder builder, Action<SmtpSendOptions> option = null)
{
SettingDataManager.Instance.Add(SmtpSendOptions.Instance);
option?.Invoke(SmtpSendOptions.Instance);
return builder;
}
}
}
6 changes: 6 additions & 0 deletions Source/Extensions/H.Extensions.Mail/H.Extensions.Mail.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\Providers\H.Providers.Ioc\H.Providers.Ioc.csproj" />
<ProjectReference Include="..\H.Extensions.Setting\H.Extensions.Setting.csproj" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions Source/Extensions/H.Extensions.Mail/IMailLogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace H.Extensions.Mail
{
public interface IMailLogService
{
bool Send(string subject, string body, out string message);
}
}
9 changes: 9 additions & 0 deletions Source/Extensions/H.Extensions.Mail/IMailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using H.Extensions.Mail;

namespace H.Extensions.Mail
{
public interface IMailService
{
bool Send(MailMessageItem messageItem, bool isBodyHtml, out string message);
}
}
19 changes: 19 additions & 0 deletions Source/Extensions/H.Extensions.Mail/MailLogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using H.Extensions.Mail;

namespace H.Extensions.Mail
{
public class MailLogService : IMailLogService
{
public bool Send(string subject, string body, out string message)
{
message = null;
MailMessageItem mail = new MailMessageItem();
mail.Subject = subject;
mail.Body = body;
mail.From = SmtpSendOptions.Instance.User;
mail.To = new string[] { SmtpSendOptions.Instance.User };
return Ioc<IMailService>.Instance?.Send(mail, SmtpSendOptions.Instance.IsBodyHtml, out message) == true;
}
}
}
81 changes: 81 additions & 0 deletions Source/Extensions/H.Extensions.Mail/MailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using H.Extensions.Mail;

namespace H.Extensions.Mail
{
public class MailService : IMailService
{
public bool Send(MailMessageItem messageItem, bool isBodyHtml, out string message)
{
message = null;
SmtpClient smtp = new SmtpClient()
{
Host = SmtpSendOptions.Instance.Host,
Port = SmtpSendOptions.Instance.Port,
EnableSsl = SmtpSendOptions.Instance.EnableSsl,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(SmtpSendOptions.Instance.User, SmtpSendOptions.Instance.Password)
};

using MailMessage msg = new();
msg.From = new MailAddress(messageItem.From);
foreach (var to in messageItem.To)
{
msg.To.Add(new MailAddress(to));
}

if (messageItem.Cc != null)
{
foreach (var cc in messageItem.Cc)
{
msg.CC.Add(new MailAddress(cc));
}
}

if (messageItem.Bcc != null)
{
foreach (var bcc in messageItem.Bcc)
{
msg.Bcc.Add(new MailAddress(bcc));
}
}

msg.Subject = messageItem.Subject;
msg.Body = messageItem.Body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = isBodyHtml;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
if (messageItem.Attachments != null)
{
foreach (var attachment in messageItem.Attachments)
{
Attachment data = new(attachment.FullName, MediaTypeNames.Application.Octet);
var disposition = data.ContentDisposition;
disposition!.CreationDate = File.GetCreationTime(attachment.FullName);
disposition.ModificationDate = File.GetLastWriteTime(attachment.FullName);
disposition.ReadDate = File.GetLastAccessTime(attachment.FullName);
msg.Attachments.Add(data);
}
}
try
{
smtp.Send(msg);
}
catch (System.Exception ex)
{
message = ex.Message;
IocLog.Instance?.Error(ex);
return false;
}
return true;
}


}

}
19 changes: 19 additions & 0 deletions Source/Extensions/H.Extensions.Mail/Provider/MailMessageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace H.Extensions.Mail
{
public class MailMessageItem
{
public string From { get; set; } = SmtpSendOptions.Instance.User;
public string[] To { get; set; } = { SmtpSendOptions.Instance.User };
public string[] Cc { get; set; }
public string[] Bcc { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public FileInfo[] Attachments { get; set; }
}
}
20 changes: 20 additions & 0 deletions Source/Extensions/H.Extensions.Mail/Provider/SendMainCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using H.Providers.Mvvm;
using System;

namespace H.Extensions.Mail
{
public class SendMainCommand : MarkupCommandBase
{
public override void Execute(object parameter)
{
if (parameter is MailMessageItem messageItem)
Ioc<IMailService>.Instance?.Send(messageItem, SmtpSendOptions.Instance.IsBodyHtml, out string message);
}

public override bool CanExecute(object parameter)
{
return Ioc<IMailService>.Instance != null;
}
}

}
99 changes: 99 additions & 0 deletions Source/Extensions/H.Extensions.Mail/SmtpSendOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using H.Extensions.Setting;
using H.Providers.Ioc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace H.Extensions.Mail
{
[Display(Name = "邮件外发设置", GroupName = SettingGroupNames.GroupSystem, Description = "邮件外发设置的信息")]
public class SmtpSendOptions : IocOptionInstance<SmtpSendOptions>
{
private string _host;
[Required]
[DefaultValue("smtp.163.com")]
[ReadOnly(true)]
[Display(Name = "服务器")]
public string Host
{
get { return _host; }
set
{
_host = value;
RaisePropertyChanged();
}
}


private int _port;
[DefaultValue(25)]
[ReadOnly(true)]
[Display(Name = "端口号")]
public int Port
{
get { return _port; }
set
{
_port = value;
RaisePropertyChanged();
}
}


private bool _enableSsl;
[DefaultValue(true)]
[Display(Name = "启用SSL")]
public bool EnableSsl
{
get { return _enableSsl; }
set
{
_enableSsl = value;
RaisePropertyChanged();
}
}

private string _user;
[Required]
[DefaultValue("HeBianGu2024@163.com")]
[Display(Name = "用户名")]
[ReadOnly(true)]
public string User
{
get { return _user; }
set
{
_user = value;
RaisePropertyChanged();
}
}


private string _password;
[Required]
[Display(Name = "密码")]
[Browsable(false)]
public string Password
{
get { return _password; }
set
{
_password = value;
RaisePropertyChanged();
}
}

private bool _isBodyHtml;
[Display(Name = "启用HTML格式")]
public bool IsBodyHtml
{
get { return _isBodyHtml; }
set
{
_isBodyHtml = value;
RaisePropertyChanged();
}
}

}

}
Loading

0 comments on commit c270449

Please sign in to comment.