-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailSender.cs
62 lines (57 loc) · 2.05 KB
/
EmailSender.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.UI.Services;
namespace AutoService
{
/// <summary>
/// Class EmailSender.
/// </summary>
/// <seealso cref="Microsoft.AspNetCore.Identity.UI.Services.IEmailSender" />
public class EmailSender : IEmailSender
{
// Our private configuration variables
private string _host;
private int _port;
private bool _enableSsl;
private string _userName;
private string _password;
/// <summary>
/// Initializes a new instance of the <see cref="EmailSender"/> class.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="port">The port.</param>
/// <param name="enableSSL">if set to <c>true</c> [enable SSL].</param>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The password.</param>
public EmailSender(string host, int port, bool enableSSL, string userName, string password)
{
this._host = host;
this._port = port;
this._enableSsl = enableSSL;
this._userName = userName;
this._password = password;
}
/// <summary>
/// Sends the email asynchronous.
/// </summary>
/// <param name="email">The email.</param>
/// <param name="subject">The subject.</param>
/// <param name="htmlMessage">The HTML message.</param>
/// <returns>Task.</returns>
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var client = new SmtpClient(_host, _port)
{
Credentials = new NetworkCredential(_userName, _password),
EnableSsl = _enableSsl
};
return client.SendMailAsync(
new MailMessage(_userName, email, subject, htmlMessage) { IsBodyHtml = true }
);
}
}
}