Skip to content

Commit

Permalink
Handle exception while sending test email (#1860)
Browse files Browse the repository at this point in the history
* 添加邮件发送失败时的报错信息

* 按照修改建议进行修改

* 按照修改建议进行修改

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

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

* 删除调试输出,优化报错信息
  • Loading branch information
ntdgy authored Apr 20, 2022
1 parent c468bc0 commit 6d1fcc7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MailController(MailService mailService) {
@ApiOperation("Tests the SMTP service")
@DisableOnCondition
public BaseResponse<String> testMail(@Valid @RequestBody MailParam mailParam) {
mailService.testConnection();
mailService.sendTextMail(mailParam.getTo(), mailParam.getSubject(), mailParam.getContent());
return BaseResponse.ok("已发送,请查收。若确认没有收到邮件,请检查服务器日志");
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/run/halo/app/core/ControllerExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.halo.app.core;

import java.util.Map;
import javax.mail.MessagingException;
import javax.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
Expand All @@ -18,6 +19,7 @@
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;
import run.halo.app.exception.AbstractHaloException;
import run.halo.app.exception.EmailException;
import run.halo.app.model.support.BaseResponse;
import run.halo.app.utils.ExceptionUtils;
import run.halo.app.utils.ValidationUtils;
Expand Down Expand Up @@ -142,6 +144,32 @@ public BaseResponse<?> handleGlobalException(Exception e) {
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) {
Assert.notNull(t, "Throwable must not be null");

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/run/halo/app/mail/AbstractMailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void testConnection() {
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) javaMailSender;
try {
mailSender.testConnection();
} catch (MessagingException e) {
throw new EmailException("无法连接到邮箱服务器,请检查邮箱配置.[" + e.getMessage() + "]", e);
} catch (Throwable e) {
throw new EmailException(e.getMessage(), e);
}
}
}
Expand Down Expand Up @@ -239,5 +239,4 @@ protected void clearCache() {
this.cachedMailProperties = null;
log.debug("Cleared all mail caches");
}

}

0 comments on commit 6d1fcc7

Please sign in to comment.