2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
// any third-party rights.
|
|
|
|
//
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
2022-01-31 13:56:30 +00:00
|
|
|
using Message = Amazon.SimpleEmail.Model.Message;
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
namespace ASC.Core.Notify.Senders;
|
|
|
|
|
2022-04-01 16:17:21 +00:00
|
|
|
[Singletone]
|
2022-04-14 13:52:51 +00:00
|
|
|
public class AWSSender : SmtpSender, IDisposable
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
private readonly object _locker = new object();
|
|
|
|
private AmazonSimpleEmailServiceClient _amazonEmailServiceClient;
|
|
|
|
private TimeSpan _refreshTimeout;
|
|
|
|
private DateTime _lastRefresh;
|
|
|
|
private DateTime _lastSend;
|
|
|
|
private TimeSpan _sendWindow = TimeSpan.MinValue;
|
|
|
|
private GetSendQuotaResponse _quota;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-04-01 16:17:21 +00:00
|
|
|
public AWSSender(
|
|
|
|
IConfiguration configuration,
|
|
|
|
IServiceProvider serviceProvider,
|
2022-04-26 17:53:48 +00:00
|
|
|
ILoggerProvider options) : base(configuration, serviceProvider, options)
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-04-26 17:53:48 +00:00
|
|
|
_logger = options.CreateLogger("ASC.Notify.AmazonSES");
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public override void Init(IDictionary<string, string> properties)
|
|
|
|
{
|
|
|
|
base.Init(properties);
|
|
|
|
var region = properties.ContainsKey("region") ? RegionEndpoint.GetBySystemName(properties["region"]) : RegionEndpoint.USEast1;
|
|
|
|
_amazonEmailServiceClient = new AmazonSimpleEmailServiceClient(properties["accessKey"], properties["secretKey"], region);
|
|
|
|
_refreshTimeout = TimeSpan.Parse(properties.ContainsKey("refreshTimeout") ? properties["refreshTimeout"] : "0:30:0");
|
|
|
|
_lastRefresh = DateTime.UtcNow - _refreshTimeout; //set to refresh on first send
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public override NoticeSendResult Send(NotifyMessage m)
|
|
|
|
{
|
|
|
|
NoticeSendResult result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.DebugSendTo(m.TenantId, m.Reciever);
|
2022-04-14 19:23:57 +00:00
|
|
|
using var scope = _serviceProvider.CreateScope();
|
2022-04-01 16:17:21 +00:00
|
|
|
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
|
2022-02-25 17:16:00 +00:00
|
|
|
tenantManager.SetCurrentTenant(m.TenantId);
|
2022-04-01 16:17:21 +00:00
|
|
|
|
|
|
|
var configuration = scope.ServiceProvider.GetService<CoreConfiguration>();
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!configuration.SmtpSettings.IsDefaultSettings)
|
|
|
|
{
|
|
|
|
result = base.Send(m);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = SendMessage(m);
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.Debug(result.ToString());
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.ErrorSend(m.TenantId, m.Reciever, e);
|
2022-02-15 11:52:43 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
result = NoticeSendResult.MessageIncorrect;
|
|
|
|
}
|
|
|
|
catch (MessageRejectedException)
|
|
|
|
{
|
|
|
|
result = NoticeSendResult.SendingImpossible;
|
|
|
|
}
|
|
|
|
catch (AmazonSimpleEmailServiceException e)
|
|
|
|
{
|
|
|
|
result = e.ErrorType == ErrorType.Sender ? NoticeSendResult.MessageIncorrect : NoticeSendResult.TryOnceAgain;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
result = NoticeSendResult.SendingImpossible;
|
2020-09-04 09:06:30 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (result == NoticeSendResult.MessageIncorrect || result == NoticeSendResult.SendingImpossible)
|
|
|
|
{
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.DebugAmazonSendingFailed(result);
|
2022-02-15 11:52:43 +00:00
|
|
|
result = base.Send(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private NoticeSendResult SendMessage(NotifyMessage m)
|
2020-08-24 18:41:06 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
//Check if we need to query stats
|
|
|
|
RefreshQuotaIfNeeded();
|
|
|
|
if (_quota != null)
|
|
|
|
{
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
if (_quota.Max24HourSend <= _quota.SentLast24Hours)
|
|
|
|
{
|
|
|
|
//Quota exceeded, queue next refresh to +24 hours
|
|
|
|
_lastRefresh = DateTime.UtcNow.AddHours(24);
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.WarningQuotaLimit(_lastRefresh);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return NoticeSendResult.SendingImpossible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var dest = new Destination
|
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
ToAddresses = m.Reciever.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(a => MailAddressUtils.Create(a).Address).ToList(),
|
2022-02-15 11:52:43 +00:00
|
|
|
};
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var subject = new Content(MimeHeaderUtils.EncodeMime(m.Subject)) { Charset = Encoding.UTF8.WebName, };
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
Body body;
|
|
|
|
if (m.ContentType == Pattern.HtmlContentType)
|
2020-08-24 18:41:06 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
body = new Body(new Content(HtmlUtil.GetText(m.Content)) { Charset = Encoding.UTF8.WebName })
|
|
|
|
{
|
|
|
|
Html = new Content(GetHtmlView(m.Content)) { Charset = Encoding.UTF8.WebName }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
body = new Body(new Content(m.Content) { Charset = Encoding.UTF8.WebName });
|
2020-08-24 18:41:06 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
|
|
|
var from = MailAddressUtils.Create(m.Sender).ToEncodedString();
|
2022-02-15 11:52:43 +00:00
|
|
|
var request = new SendEmailRequest { Source = from, Destination = dest, Message = new Message(subject, body) };
|
|
|
|
if (!string.IsNullOrEmpty(m.ReplyTo))
|
2020-09-30 14:47:42 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
request.ReplyToAddresses.Add(MailAddressUtils.Create(m.ReplyTo).Address);
|
2020-09-30 14:47:42 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
ThrottleIfNeeded();
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var response = _amazonEmailServiceClient.SendEmailAsync(request).Result;
|
|
|
|
_lastSend = DateTime.UtcNow;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return response != null ? NoticeSendResult.OK : NoticeSendResult.TryOnceAgain;
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private void ThrottleIfNeeded()
|
|
|
|
{
|
|
|
|
//Check last send and throttle if needed
|
|
|
|
if (_sendWindow != TimeSpan.MinValue)
|
|
|
|
{
|
|
|
|
if (DateTime.UtcNow - _lastSend <= _sendWindow)
|
|
|
|
{
|
|
|
|
//Possible BUG: at high frequncies maybe bug with to little differences
|
|
|
|
//This means that time passed from last send is less then message per second
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.DebugSendRate(_sendWindow);
|
2022-02-15 11:52:43 +00:00
|
|
|
Thread.Sleep(_sendWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private void RefreshQuotaIfNeeded()
|
|
|
|
{
|
2022-03-17 15:01:39 +00:00
|
|
|
if (!IsRefreshNeeded())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
if (IsRefreshNeeded())//Double check
|
|
|
|
{
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.DebugRefreshingQuota(_refreshTimeout, _lastRefresh);
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
//Do quota refresh
|
|
|
|
_lastRefresh = DateTime.UtcNow.AddMinutes(1);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var r = new GetSendQuotaRequest();
|
|
|
|
_quota = _amazonEmailServiceClient.GetSendQuotaAsync(r).Result;
|
|
|
|
_sendWindow = TimeSpan.FromSeconds(1.0 / _quota.MaxSendRate);
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.DebugQuota(_quota.SentLast24Hours, _quota.Max24HourSend, _quota.MaxSendRate, _sendWindow);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-05-05 14:13:47 +00:00
|
|
|
_logger.ErrorRefreshingQuota(e);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private bool IsRefreshNeeded()
|
|
|
|
{
|
|
|
|
return _quota == null || (DateTime.UtcNow - _lastRefresh) > _refreshTimeout;
|
|
|
|
}
|
2022-04-14 13:52:51 +00:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (_amazonEmailServiceClient != null)
|
|
|
|
{
|
|
|
|
_amazonEmailServiceClient.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|