Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exception while sending test email #1860

Merged
merged 6 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("已发送,请查收。若确认没有收到邮件,请检查服务器日志");
ruibaby marked this conversation as resolved.
Show resolved Hide resolved
}
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");
}

}