Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit acd5d2a

Browse files
send email using smtp protocol
1 parent ab55469 commit acd5d2a

File tree

8 files changed

+150
-3
lines changed

8 files changed

+150
-3
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/HomeController.cs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@ public class HomeController : Controller
1717
private readonly NewBookAlertConfig _thirdPartyBookconfiguration;
1818
private readonly IMessageRepository _messageRepository;
1919
private readonly IUserService _userService;
20+
private readonly IEmailService _emailService;
2021

21-
public HomeController(IOptionsSnapshot<NewBookAlertConfig> newBookAlertconfiguration,
22+
public HomeController(IOptionsSnapshot<NewBookAlertConfig> newBookAlertconfiguration,
2223
IMessageRepository messageRepository,
23-
IUserService userService)
24+
IUserService userService,
25+
IEmailService emailService)
2426
{
2527
_newBookAlertconfiguration = newBookAlertconfiguration.Get("InternalBook");
2628
_thirdPartyBookconfiguration = newBookAlertconfiguration.Get("ThirdPartyBook");
2729
_messageRepository = messageRepository;
2830
_userService = userService;
31+
_emailService = emailService;
2932
}
3033

31-
public ViewResult Index()
34+
public asyncTask<ViewResult> Index()
3235
{
3336

37+
UserEmailOptions options = new UserEmailOptions
38+
{
39+
ToEmails = new List<string>() { "test@gmail.com"}
40+
};
41+
42+
await _emailService.SendTestEmail(options);
43+
3444
//var userId = _userService.GetUserId();
3545
//var isLoggedIn = _userService.IsAuthenticated();
3646

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
</head>
7+
<body>
8+
<p>Hello from book store web app</p>
9+
</body>
10+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Models
7+
{
8+
public class SMTPConfigModel
9+
{
10+
public string SenderAddress { get; set; }
11+
public string SenderDisplayName { get; set; }
12+
public string UserName { get; set; }
13+
public string Password { get; set; }
14+
public string Host { get; set; }
15+
public int Port { get; set; }
16+
public bool EnableSSL { get; set; }
17+
public bool UseDefaultCredentials { get; set; }
18+
public bool IsBodyHTML { get; set; }
19+
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Models
7+
{
8+
public class UserEmailOptions
9+
{
10+
public List<string> ToEmails { get; set; }
11+
public string Subject { get; set; }
12+
public string Body { get; set; }
13+
}
14+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Microsoft.Extensions.Options;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Mail;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Webgentle.BookStore.Models;
11+
12+
namespace Webgentle.BookStore.Service
13+
{
14+
public class EmailService : IEmailService
15+
{
16+
private const string templatePath = @"EmailTemplate/{0}.html";
17+
private readonly SMTPConfigModel _smtpConfig;
18+
19+
public async Task SendTestEmail(UserEmailOptions userEmailOptions)
20+
{
21+
userEmailOptions.Subject = "This is test email subject from book store web app";
22+
userEmailOptions.Body = GetEmailBody("TestEmail");
23+
24+
await SendEmail(userEmailOptions);
25+
}
26+
27+
public EmailService(IOptions<SMTPConfigModel> smtpConfig)
28+
{
29+
_smtpConfig = smtpConfig.Value;
30+
}
31+
32+
private async Task SendEmail(UserEmailOptions userEmailOptions)
33+
{
34+
MailMessage mail = new MailMessage
35+
{
36+
Subject = userEmailOptions.Subject,
37+
Body = userEmailOptions.Body,
38+
From = new MailAddress(_smtpConfig.SenderAddress, _smtpConfig.SenderDisplayName),
39+
IsBodyHtml = _smtpConfig.IsBodyHTML
40+
};
41+
42+
foreach (var toEmail in userEmailOptions.ToEmails)
43+
{
44+
mail.To.Add(toEmail);
45+
}
46+
47+
NetworkCredential networkCredential = new NetworkCredential(_smtpConfig.UserName, _smtpConfig.Password);
48+
49+
SmtpClient smtpClient = new SmtpClient
50+
{
51+
Host = _smtpConfig.Host,
52+
Port = _smtpConfig.Port,
53+
EnableSsl = _smtpConfig.EnableSSL,
54+
UseDefaultCredentials = _smtpConfig.UseDefaultCredentials,
55+
Credentials = networkCredential
56+
};
57+
58+
mail.BodyEncoding = Encoding.Default;
59+
60+
await smtpClient.SendMailAsync(mail);
61+
}
62+
63+
private string GetEmailBody(string templateName)
64+
{
65+
var body = File.ReadAllText(string.Format(templatePath, templateName));
66+
return body;
67+
}
68+
}
69+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Threading.Tasks;
2+
using Webgentle.BookStore.Models;
3+
4+
namespace Webgentle.BookStore.Service
5+
{
6+
public interface IEmailService
7+
{
8+
Task SendTestEmail(UserEmailOptions userEmailOptions);
9+
}
10+
}

‎Webgentle.BookStore/Webgentle.BookStore/Startup.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ public void ConfigureServices(IServiceCollection services)
6969
services.AddSingleton<IMessageRepository, MessageRepository>();
7070
services.AddScoped<IAccountRepository, AccountRepository>();
7171
services.AddScoped<IUserService, UserService>();
72+
services.AddScoped<IEmailService, EmailService>();
7273

7374
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, ApplicationUserClaimsPrincipalFactory>();
7475

76+
services.Configure<SMTPConfigModel>(_configuration.GetSection("SMTPConfig"));
7577
services.Configure<NewBookAlertConfig>("InternalBook", _configuration.GetSection("NewBookAlert"));
7678
services.Configure<NewBookAlertConfig>("ThirdPartyBook", _configuration.GetSection("ThirdPartyBook"));
7779
}

‎Webgentle.BookStore/Webgentle.BookStore/appsettings.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,16 @@
2929
"key3": {
3030
"key3obj1": "key 3 obj 1 value"
3131
}
32+
},
33+
"SMTPConfig": {
34+
"SenderAddress": "no-reply@bookstoreapp.com",
35+
"SenderDisplayName": "Book store team",
36+
"UserName": "0704ea2a3d3699",
37+
"Password": "ceba43d423f90a",
38+
"Host": "smtp.mailtrap.io",
39+
"Port":587,
40+
"EnableSSL": true,
41+
"UseDefaultCredentials": true,
42+
"IsBodyHTML": true
3243
}
3344
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /