Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
performance
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zry
performance
Commits
e53e5151
Commit
e53e5151
authored
Mar 27, 2019
by
ruyun.zhang@suvalue.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
阿里邮箱发送邮件功能集成
parent
db4002cd
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
187 additions
and
1 deletions
+187
-1
performance/Performance.Api/Startup.cs
+11
-1
performance/Performance.Api/appsettings.json
+6
-0
performance/Performance.ConsoleApp/Performance.ConsoleApp.csproj
+2
-0
performance/Performance.ConsoleApp/Program.cs
+23
-0
performance/Performance.ConsoleApp/appsettings.json
+6
-0
performance/Performance.Infrastructure/InfrastructureExtensions.cs
+27
-0
performance/Performance.Infrastructure/Util/EmailService.cs
+112
-0
No files found.
performance/Performance.Api/Startup.cs
View file @
e53e5151
...
@@ -95,7 +95,8 @@ public void ConfigureServices(IServiceCollection services)
...
@@ -95,7 +95,8 @@ public void ConfigureServices(IServiceCollection services)
services
services
.
Configure
<
AppConnection
>(
Configuration
.
GetSection
(
"AppConnection"
))
.
Configure
<
AppConnection
>(
Configuration
.
GetSection
(
"AppConnection"
))
.
Configure
<
Application
>(
Configuration
.
GetSection
(
"Application"
))
.
Configure
<
Application
>(
Configuration
.
GetSection
(
"Application"
))
.
Configure
<
HuyiSmsConfig
>(
Configuration
.
GetSection
(
"HuyiSmsConfig"
));
.
Configure
<
HuyiSmsConfig
>(
Configuration
.
GetSection
(
"HuyiSmsConfig"
))
.
Configure
<
EmailOptions
>(
Configuration
.
GetSection
(
"EmailOptions"
));
//services.AddSwaggerGen(c =>
//services.AddSwaggerGen(c =>
//{
//{
...
@@ -106,6 +107,15 @@ public void ConfigureServices(IServiceCollection services)
...
@@ -106,6 +107,15 @@ public void ConfigureServices(IServiceCollection services)
services
.
AddScoped
<
HuyiSmsNotify
>();
services
.
AddScoped
<
HuyiSmsNotify
>();
//用户身份信息服务
//用户身份信息服务
services
.
AddScoped
<
ClaimService
>();
services
.
AddScoped
<
ClaimService
>();
//阿里邮箱配置
var
emailOption
=
services
.
BuildServiceProvider
().
GetService
<
IOptions
<
EmailOptions
>>();
//邮件发送
services
.
AddEmailUtil
(
options
=>
{
options
.
Account
=
emailOption
.
Value
.
Account
;
options
.
Password
=
emailOption
.
Value
.
Password
;
options
.
SmtpServer
=
emailOption
.
Value
.
SmtpServer
;
});
//ef配置
//ef配置
var
connection
=
services
.
BuildServiceProvider
().
GetService
<
IOptions
<
AppConnection
>>();
var
connection
=
services
.
BuildServiceProvider
().
GetService
<
IOptions
<
AppConnection
>>();
...
...
performance/Performance.Api/appsettings.json
View file @
e53e5151
...
@@ -16,6 +16,12 @@
...
@@ -16,6 +16,12 @@
"Account"
:
"cf_szjk"
,
"Account"
:
"cf_szjk"
,
"Password"
:
"123456"
"Password"
:
"123456"
},
},
//阿里邮箱
"EmailOptions"
:
{
"SmtpServer"
:
"smtpdm.aliyun.com"
,
"Account"
:
"service@email.suvalue.com"
,
"Password"
:
"SuValue123456"
},
"Application"
:
{
"Application"
:
{
//登录过期时间
//登录过期时间
"ExpirationMinutes"
:
"120"
,
"ExpirationMinutes"
:
"120"
,
...
...
performance/Performance.ConsoleApp/Performance.ConsoleApp.csproj
View file @
e53e5151
...
@@ -28,6 +28,8 @@
...
@@ -28,6 +28,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="MySql.Data" Version="8.0.15" />
<PackageReference Include="MySql.Data" Version="8.0.15" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.15" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.15" />
</ItemGroup>
</ItemGroup>
...
...
performance/Performance.ConsoleApp/Program.cs
View file @
e53e5151
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Options
;
using
Performance.DtoModels
;
using
Performance.DtoModels
;
using
Performance.DtoModels.AppSettings
;
using
Performance.DtoModels.AppSettings
;
using
Performance.DtoModels.AutoMapper
;
using
Performance.DtoModels.AutoMapper
;
...
@@ -31,6 +33,27 @@ static void Main(string[] args)
...
@@ -31,6 +33,27 @@ static void Main(string[] args)
IConfigurationRoot
configuration
=
builder
.
Build
();
IConfigurationRoot
configuration
=
builder
.
Build
();
ServiceCollection
services
=
new
ServiceCollection
();
ServiceCollection
services
=
new
ServiceCollection
();
services
.
AddLogging
();
//serviceProvider.GetService<ILoggerFactory>().AddConsole();
services
.
Configure
<
EmailOptions
>(
configuration
.
GetSection
(
"EmailOptions"
));
var
option
=
services
.
BuildServiceProvider
().
GetService
<
IOptions
<
EmailOptions
>>();
services
.
AddEmailUtil
(
config
=>
{
config
.
Account
=
option
.
Value
.
Account
;
config
.
Password
=
option
.
Value
.
Password
;
config
.
SmtpServer
=
option
.
Value
.
SmtpServer
;
});
var
emailService
=
services
.
BuildServiceProvider
().
GetService
<
IEmailService
>();
var
message
=
new
EmailMessage
{
To
=
new
List
<
string
>
{
"ruyun.zhang@suvalue.com"
,
"huiliang.wang@suvalue.com"
},
Attachments
=
new
List
<
string
>
{
@"E:\code_git\performance\performance\Performance.Api\Files\9\201911\医院绩效分配系统数据收集模板V120190307133444707.xlsx"
},
DisplayName
=
"溯直健康"
,
Subject
=
"测试"
,
Body
=
"我是测试"
};
emailService
.
Send
(
message
);
var
task
=
emailService
.
SendAsync
(
message
);
task
.
Wait
();
Mapper
.
Initialize
(
cfg
=>
cfg
.
AddProfile
<
AutoMapperConfigs
>());
Mapper
.
Initialize
(
cfg
=>
cfg
.
AddProfile
<
AutoMapperConfigs
>());
services
.
AddAutoMapper
();
services
.
AddAutoMapper
();
...
...
performance/Performance.ConsoleApp/appsettings.json
View file @
e53e5151
...
@@ -16,6 +16,12 @@
...
@@ -16,6 +16,12 @@
"Account"
:
"cf_szjk"
,
"Account"
:
"cf_szjk"
,
"Password"
:
"123456"
"Password"
:
"123456"
},
},
//阿里邮箱
"EmailOptions"
:
{
"SmtpServer"
:
"smtpdm.aliyun.com"
,
"Account"
:
"service@email.suvalue.com"
,
"Password"
:
"SuValue123456"
},
"Application"
:
{
"Application"
:
{
//登录过期时间
//登录过期时间
"ExpirationMinutes"
:
"120"
,
"ExpirationMinutes"
:
"120"
,
...
...
performance/Performance.Infrastructure/InfrastructureExtensions.cs
0 → 100644
View file @
e53e5151
using
Microsoft.Extensions.DependencyInjection
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
namespace
Performance.Infrastructure
{
public
static
class
InfrastructureExtensions
{
/// <summary>
/// 添加邮件服务
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public
static
IServiceCollection
AddEmailUtil
(
this
IServiceCollection
services
,
Action
<
EmailOptions
>
configure
=
null
)
{
if
(
configure
!=
null
)
{
EmailOptions
options
=
new
EmailOptions
();
configure
.
Invoke
(
options
);
services
.
AddSingleton
(
options
);
}
services
.
AddSingleton
<
IEmailService
,
EmailService
>();
return
services
;
}
}
}
performance/Performance.Infrastructure/Util/EmailService.cs
0 → 100644
View file @
e53e5151
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Logging
;
using
System
;
using
System.Collections.Generic
;
using
System.Net
;
using
System.Net.Mail
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Performance.Infrastructure
{
public
class
EmailOptions
{
/// <summary>
/// smtp
/// </summary>
public
string
SmtpServer
{
get
;
set
;
}
/// <summary>
/// 发送邮箱
/// </summary>
public
string
Account
{
get
;
set
;
}
/// <summary>
/// 密码
/// </summary>
public
string
Password
{
get
;
set
;
}
}
public
class
EmailMessage
{
/// <summary>
/// 邮件显示名称
/// </summary>
public
string
DisplayName
{
get
;
set
;
}
/// <summary>
/// 邮件主题
/// </summary>
public
string
Subject
{
get
;
set
;
}
/// <summary>
/// 邮件内容
/// </summary>
public
string
Body
{
get
;
set
;
}
/// <summary>
/// 发送地址
/// </summary>
public
List
<
string
>
To
{
get
;
set
;
}
=
new
List
<
string
>();
/// <summary>
/// 附件
/// </summary>
public
List
<
string
>
Attachments
{
get
;
set
;
}
=
new
List
<
string
>();
}
public
interface
IEmailService
{
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
bool
Send
(
EmailMessage
message
);
Task
<
bool
>
SendAsync
(
EmailMessage
message
);
}
public
class
EmailService
:
IEmailService
{
private
EmailOptions
options
;
private
ILogger
<
EmailService
>
logger
;
public
EmailService
(
EmailOptions
options
,
ILogger
<
EmailService
>
logger
)
{
this
.
options
=
options
;
this
.
logger
=
logger
;
}
public
Task
<
bool
>
SendAsync
(
EmailMessage
message
)
{
return
Task
.
Run
<
bool
>(()
=>
{
return
Send
(
message
);
});
}
public
bool
Send
(
EmailMessage
message
)
{
try
{
MailMessage
mail
=
new
MailMessage
();
mail
.
From
=
new
MailAddress
(
options
.
Account
,
message
.
DisplayName
);
//多邮箱地址处理
message
.
To
.
ForEach
(
t
=>
mail
.
To
.
Add
(
t
));
mail
.
Subject
=
message
.
Subject
;
//主题
mail
.
Body
=
message
.
Body
;
//内容
mail
.
BodyEncoding
=
Encoding
.
UTF8
;
//正文编码
mail
.
IsBodyHtml
=
true
;
//设置为HTML格式
mail
.
Priority
=
MailPriority
.
Low
;
//优先级
//发送附件 指明附件的绝对地址
message
.
Attachments
.
ForEach
(
t
=>
mail
.
Attachments
.
Add
(
new
Attachment
(
t
)));
SmtpClient
client
=
new
SmtpClient
(
options
.
SmtpServer
,
80
);
client
.
Timeout
=
10000
;
client
.
UseDefaultCredentials
=
true
;
client
.
Credentials
=
new
NetworkCredential
(
options
.
Account
,
options
.
Password
);
//用户名和密码
client
.
Send
(
mail
);
return
true
;
}
catch
(
SmtpException
ex
)
{
logger
.
LogError
(
ex
.
ToString
());
throw
ex
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment