show friendly error when email send fails and warn on large attachments (#3008) (#3055)

This commit is contained in:
ACX
2026-02-26 20:42:07 -07:00
committed by GitHub
parent 1a8cc355b3
commit 78345eaf52
7 changed files with 99 additions and 38 deletions
@@ -30,6 +30,7 @@ import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
@Slf4j
@@ -80,9 +81,9 @@ public class SendEmailV2Service {
notificationService.sendMessage(Topic.LOG, LogNotification.info(successMessage));
log.info(successMessage);
} catch (Exception e) {
String errorMessage = "An error occurred while sending the book: " + bookTitle + " to " + recipientEmail + ". Error: " + e.getMessage();
notificationService.sendMessage(Topic.LOG, LogNotification.error(errorMessage));
log.error(errorMessage, e);
String userMessage = "Failed to send book: " + bookTitle + " to " + recipientEmail + ". " + extractUserFriendlyMessage(e);
notificationService.sendMessage(Topic.LOG, LogNotification.error(userMessage));
log.error("Email send failed for book '{}' to {}: {}", bookTitle, recipientEmail, e.getMessage(), e);
}
});
}
@@ -202,6 +203,20 @@ public class SendEmailV2Service {
.orElseThrow(ApiError.DEFAULT_EMAIL_PROVIDER_NOT_FOUND::createException);
}
private String extractUserFriendlyMessage(Exception e) {
Throwable cause = e;
while (cause != null) {
if (cause instanceof IOException) {
return "The email provider rejected or dropped the connection during transfer. This often happens when the attachment exceeds the provider's size limit.";
}
cause = cause.getCause();
}
if (e instanceof MessagingException) {
return "The email could not be sent due to a mail server error. Please verify your email provider settings.";
}
return "An unexpected error occurred: " + e.getMessage();
}
private enum ConnectionType {
SSL,
STARTTLS,