Skip to content

Commit 2414446

Browse files
committed
complete asp.net core example
1 parent ee65836 commit 2414446

7 files changed

Lines changed: 64 additions & 26 deletions

File tree

‎LICENSE‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎example/NETCore.MailKit.Web/Controllers/HomeController.cs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Mvc;
6+
using NETCore.MailKit.Core;
67

78
namespace NETCore.MailKit.Web.Controllers
89
{
910
public class HomeController : Controller
1011
{
12+
private readonly IEmailService _EmailService;
13+
14+
public HomeController(IEmailService emailService)
15+
{
16+
_EmailService = emailService;
17+
}
18+
1119
public IActionResult Index()
1220
{
1321
return View();
@@ -27,6 +35,16 @@ public IActionResult Contact()
2735
return View();
2836
}
2937

38+
public IActionResult Email()
39+
{
40+
ViewData["Message"] = "ASP.NET Core mvc send email example";
41+
42+
_EmailService.Send("shenbo.128@163.com", "ASP.NET Core mvc send email example", "Send from asp.net core mvc action");
43+
44+
return View();
45+
}
46+
47+
3048
public IActionResult Error()
3149
{
3250
return View();
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp1.1</TargetFramework>
55
</PropertyGroup>
66

77
<PropertyGroup>
88
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
9+
<UserSecretsId>e81e4d4a-9579-464d-97c0-ee073f03b7fb</UserSecretsId>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
1213
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
1314
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
1415
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
1517
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
1618
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
1719
</ItemGroup>
1820
<ItemGroup>
1921
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
2022
</ItemGroup>
23+
<ItemGroup>
24+
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild3-final" />
25+
</ItemGroup>
26+
<ItemGroup>
27+
<ProjectReference Include="..\..\src\NETCore.MailKit\NETCore.MailKit.csproj" />
28+
</ItemGroup>
2129

2230
</Project>

‎example/NETCore.MailKit.Web/Startup.cs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
@@ -7,6 +7,8 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
10+
using NETCore.MailKit.Extensions;
11+
using NETCore.MailKit.Infrastructure.Internal;
1012

1113
namespace NETCore.MailKit.Web
1214
{
@@ -19,6 +21,13 @@ public Startup(IHostingEnvironment env)
1921
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
2022
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
2123
.AddEnvironmentVariables();
24+
25+
//add sercets.json https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets
26+
if (env.IsDevelopment())
27+
{
28+
builder.AddUserSecrets<Startup>();
29+
}
30+
2231
Configuration = builder.Build();
2332
}
2433

@@ -29,6 +38,22 @@ public void ConfigureServices(IServiceCollection services)
2938
{
3039
// Add framework services.
3140
services.AddMvc();
41+
42+
//Add MailKit
43+
services.AddMailKit(optionBuilder =>
44+
{
45+
46+
optionBuilder.UseMailKit(new MailKitOptions()
47+
{
48+
//get options from sercets.json
49+
Server = Configuration["Server"],
50+
Port = Convert.ToInt32(Configuration["Port"]),
51+
SenderName = Configuration["SenderName"],
52+
SenderEmail = Configuration["SenderEmail"],
53+
Account = Configuration["Account"],
54+
Passord = Configuration["Passord"]
55+
});
56+
});
3257
}
3358

3459
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
@{
3+
ViewData["Title"] = "MailKit";
4+
}
5+
6+
<h2>@ViewData["Title"].</h2>
7+
<h3>@ViewData["Message"]</h3>

‎example/NETCore.MailKit.Web/Views/Shared/_Layout.cshtml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
1+
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
22
<!DOCTYPE html>
33
<html>
44
<head>
@@ -35,6 +35,7 @@
3535
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
3636
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
3737
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
38+
<li><a asp-area="" asp-controller="Home" asp-action="Email">Email</a></li>
3839
</ul>
3940
</div>
4041
</div>

‎src/NETCore.MailKit/Extensions/ServicesCollectionExtensions.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NETCore.MailKit.Extensions
1010
{
1111
public static class ServicesCollectionExtensions
1212
{
13-
public static IServiceCollection AddRabbitmqKit(this IServiceCollection serviceCollection, Action<MailKitOptionsBuilder> optionsAction)
13+
public static IServiceCollection AddMailKit(this IServiceCollection serviceCollection, Action<MailKitOptionsBuilder> optionsAction)
1414
{
1515
Check.Argument.IsNotNull(serviceCollection, nameof(serviceCollection), "IServiceCollection is not dependency injection");
1616
Check.Argument.IsNotNull(optionsAction, nameof(optionsAction));

0 commit comments

Comments
 (0)