forked from VahidN/DNTCommon.Web.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebMailServiceController.cs
More file actions
67 lines (61 loc) · 2.51 KB
/
WebMailServiceController.cs
File metadata and controls
67 lines (61 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DNTCommon.Web.Core.TestWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DNTCommon.Web.Core.TestWebApp.Controllers
{
public class WebMailServiceController : Controller
{
private readonly IWebMailService _webMailService;
private readonly IOptionsSnapshot<SmtpConfig> _smtpConfig;
public WebMailServiceController(
IWebMailService webMailService,
IOptionsSnapshot<SmtpConfig> smtpConfig // will be provided from the `appsettings.json` file.
)
{
_webMailService = webMailService;
_smtpConfig = smtpConfig;
_smtpConfig = smtpConfig ?? throw new ArgumentNullException(nameof(smtpConfig));
if (_smtpConfig.Value == null)
{
throw new ArgumentNullException(nameof(smtpConfig), "Please add SmtpConfig to your appsettings.json file.");
}
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> SendEmail()
{
await _webMailService.SendEmailAsync(
smtpConfig: _smtpConfig.Value,
emails: new List<MailAddress>
{
new MailAddress { ToName = "User 1", ToAddress = "user1@site.com" },
// ...
},
subject: "Hello!",
message: "Hello!<br/> This is an email from us!");
return RedirectToAction(nameof(Index));
}
public async Task<IActionResult> SendRazorTemplateEmail()
{
await _webMailService.SendEmailAsync(
smtpConfig: _smtpConfig.Value,
emails: new List<MailAddress>
{
new MailAddress { ToName = "User 1", ToAddress = "user1@site.com" },
// ...
},
subject: "Please verify your account",
viewNameOrPath: "~/Views/EmailTemplates/_Template1.cshtml",
viewModel: new EmailTemplateViewModel
{
EmailSignature = "DNT"
});
return RedirectToAction(nameof(Index));
}
}
}