|
|
|
@ -22,6 +22,7 @@ import jakarta.xml.bind.DatatypeConverter; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.context.MessageSource; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.core.NestedRuntimeException; |
|
|
|
@ -33,6 +34,7 @@ import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; |
|
|
|
import org.thingsboard.rule.engine.api.MailService; |
|
|
|
import org.thingsboard.rule.engine.api.TbEmail; |
|
|
|
import org.thingsboard.server.cache.limits.RateLimitService; |
|
|
|
import org.thingsboard.server.common.data.AdminSettings; |
|
|
|
import org.thingsboard.server.common.data.ApiFeature; |
|
|
|
import org.thingsboard.server.common.data.ApiUsageRecordKey; |
|
|
|
@ -43,6 +45,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.limit.LimitedApi; |
|
|
|
import org.thingsboard.server.common.stats.TbApiUsageReportClient; |
|
|
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|
|
|
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|
|
|
@ -84,6 +87,12 @@ public class DefaultMailService implements MailService { |
|
|
|
@Autowired |
|
|
|
private TbMailContextComponent tbMailContextComponent; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RateLimitService rateLimitService; |
|
|
|
|
|
|
|
@Value("${mail.per_tenant_rate_limits:}") |
|
|
|
private String perTenantRateLimitConfig; |
|
|
|
|
|
|
|
private TbMailSender mailSender; |
|
|
|
|
|
|
|
private String mailFrom; |
|
|
|
@ -214,6 +223,10 @@ public class DefaultMailService implements MailService { |
|
|
|
|
|
|
|
private void sendMail(TenantId tenantId, CustomerId customerId, TbEmail tbEmail, JavaMailSender javaMailSender, long timeout) throws ThingsboardException { |
|
|
|
if (apiUsageStateService.getApiUsageState(tenantId).isEmailSendEnabled()) { |
|
|
|
if (tenantId != null && !tenantId.isSysTenantId() && StringUtils.isNotEmpty(perTenantRateLimitConfig) && |
|
|
|
!rateLimitService.checkRateLimit(LimitedApi.EMAILS, (Object) tenantId, perTenantRateLimitConfig)) { |
|
|
|
throw new ThingsboardException("Rate limit for emails sending is exceeded", ThingsboardErrorCode.TOO_MANY_REQUESTS); |
|
|
|
} |
|
|
|
try { |
|
|
|
MimeMessage mailMsg = javaMailSender.createMimeMessage(); |
|
|
|
boolean multipart = (tbEmail.getImages() != null && !tbEmail.getImages().isEmpty()); |
|
|
|
|