-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
411 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
Source/Extensions/H.Extensions.Mail/Provider/MailMessageItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
Source/Extensions/H.Extensions.Mail/Provider/SendMainCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.