Handle exception while sending test email (#1860)

* 添加邮件发送失败时的报错信息

* 按照修改建议进行修改

* 按照修改建议进行修改

* 将exception的异常处理修改为instanceof

* 在ControllerWxceptionHandler中添加对应异常处理

* 删除调试输出,优化报错信息
pull/1900/head
ntdgy 2022-04-20 10:18:09 +08:00 committed by GitHub
parent c468bc0dfa
commit 6d1fcc73b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -31,6 +31,7 @@ public class MailController {
@ApiOperation("Tests the SMTP service") @ApiOperation("Tests the SMTP service")
@DisableOnCondition @DisableOnCondition
public BaseResponse<String> testMail(@Valid @RequestBody MailParam mailParam) { public BaseResponse<String> testMail(@Valid @RequestBody MailParam mailParam) {
mailService.testConnection();
mailService.sendTextMail(mailParam.getTo(), mailParam.getSubject(), mailParam.getContent()); mailService.sendTextMail(mailParam.getTo(), mailParam.getSubject(), mailParam.getContent());
return BaseResponse.ok("已发送,请查收。若确认没有收到邮件,请检查服务器日志"); return BaseResponse.ok("已发送,请查收。若确认没有收到邮件,请检查服务器日志");
} }

View File

@ -1,6 +1,7 @@
package run.halo.app.core; package run.halo.app.core;
import java.util.Map; import java.util.Map;
import javax.mail.MessagingException;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException; import org.springframework.web.servlet.NoHandlerFoundException;
import run.halo.app.exception.AbstractHaloException; import run.halo.app.exception.AbstractHaloException;
import run.halo.app.exception.EmailException;
import run.halo.app.model.support.BaseResponse; import run.halo.app.model.support.BaseResponse;
import run.halo.app.utils.ExceptionUtils; import run.halo.app.utils.ExceptionUtils;
import run.halo.app.utils.ValidationUtils; import run.halo.app.utils.ValidationUtils;
@ -142,6 +144,32 @@ public class ControllerExceptionHandler {
return baseResponse; return baseResponse;
} }
@ExceptionHandler(EmailException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public BaseResponse<?> handleMessagingException(MessagingException e) {
BaseResponse<?> baseResponse = handleBaseException(e);
String message;
if (e instanceof com.sun.mail.util.MailConnectException) {
if (e.getCause() instanceof java.net.UnknownHostException) {
message = "SMTP 服务器解析错误,请检查 SMTP 服务器地址";
} else if (e.getCause() instanceof java.net.ConnectException) {
message = "无法连接至邮件服务器,请检查地址和端口号";
} else if (e.getCause() instanceof java.net.SocketException) {
message = "网络连接超时,请检查网络连通性";
} else {
message = "无法连接至邮件服务器,请检查地址和端口号";
}
} else if (e instanceof javax.mail.NoSuchProviderException) {
message = "发送协议配置错误,请检查发送协议";
} else if (e instanceof javax.mail.AuthenticationFailedException) {
message = "邮箱账号密码验证失败,请检查密码是否应为授权码";
} else {
message = "出现未知错误,请检查系统日志";
}
baseResponse.setMessage(message);
return baseResponse;
}
private <T> BaseResponse<T> handleBaseException(Throwable t) { private <T> BaseResponse<T> handleBaseException(Throwable t) {
Assert.notNull(t, "Throwable must not be null"); Assert.notNull(t, "Throwable must not be null");

View File

@ -67,8 +67,8 @@ public abstract class AbstractMailService implements MailService {
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) javaMailSender; JavaMailSenderImpl mailSender = (JavaMailSenderImpl) javaMailSender;
try { try {
mailSender.testConnection(); mailSender.testConnection();
} catch (MessagingException e) { } catch (Throwable e) {
throw new EmailException("无法连接到邮箱服务器,请检查邮箱配置.[" + e.getMessage() + "]", e); throw new EmailException(e.getMessage(), e);
} }
} }
} }
@ -239,5 +239,4 @@ public abstract class AbstractMailService implements MailService {
this.cachedMailProperties = null; this.cachedMailProperties = null;
log.debug("Cleared all mail caches"); log.debug("Cleared all mail caches");
} }
} }