Newer
Older
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
68
69
70
71
72
using MailKit;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class EmailService : IEmailService
{
private readonly MailSettings _mailSettings;
public EmailService(IOptions<MailSettings> mailSettings)
{
_mailSettings = mailSettings.Value;
}
public async Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message)
{
ServiceResult<bool> result = new ServiceResult<bool>();
message.Sender = MailboxAddress.Parse(_mailSettings.Mail);
var client = new SmtpClient();
client.CheckCertificateRevocation = false;
try
{
await client.ConnectAsync(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls);
}
catch (Exception e)
{
result.HasError = true;
result.Message = ErrorMessages.CannotConnectToSmtpServer;
return result;
}
try
{
await client.AuthenticateAsync(_mailSettings.Mail, _mailSettings.Password);
}
catch (Exception)
{
result.HasError = true;
result.Message = ErrorMessages.CannotAuthenticateToSmtpServer;
return result;
}
try
{
await client.SendAsync(message);
}
catch (Exception)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.CannotSendEmail, message.Subject);
return result;
}
await client.DisconnectAsync(true);
return result;
}
public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> mails)
{
var message = new MimeMessage();
}
message.Subject = $"Nuevo Post: {postCreateDto.Title}";
message.Body = new TextPart("html")
{
Text = $"<p> Hay un nuevo post perteneciente al foro suscripto."
};
var result = await SendEmailAsync(message);
return result;
}
}
}