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 2 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 @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.annotation.DisableOnCondition;
import run.halo.app.exception.EmailException;
import run.halo.app.mail.MailService;
import run.halo.app.model.params.MailParam;
import run.halo.app.model.support.BaseResponse;
Expand All @@ -31,6 +32,11 @@ public MailController(MailService mailService) {
@ApiOperation("Tests the SMTP service")
@DisableOnCondition
public BaseResponse<String> testMail(@Valid @RequestBody MailParam mailParam) {
try {
mailService.testConnection();
} catch (EmailException e) {
return BaseResponse.ok(e.getMessage());
}
mailService.sendTextMail(mailParam.getTo(), mailParam.getSubject(), mailParam.getContent());
return BaseResponse.ok("已发送,请查收。若确认没有收到邮件,请检查服务器日志");
ruibaby marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/run/halo/app/mail/AbstractMailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ public void testConnection() {
try {
mailSender.testConnection();
} catch (MessagingException e) {
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
throw new EmailException("无法连接到邮箱服务器,请检查邮箱配置.[" + e.getMessage() + "]", e);
String message;
message = e.getMessage();
if (message.contains("No provider for")) {
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
message = "发送协议配置错误,请检查发送协议";
} else if (message.contains("Couldn't connect to host")) {
message = "无法连接至邮件服务器,请检查地址和端口号";
} else {
message = "邮箱账号密码验证失败,请检查密码是否应为授权码";
}
throw new EmailException(message, e);
//throw new EmailException("无法连接到邮箱服务器,请检查邮箱配置.[" + e.getMessage() + "]", e);
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -239,5 +249,4 @@ protected void clearCache() {
this.cachedMailProperties = null;
log.debug("Cleared all mail caches");
}

}