paascloud/paascloud-master · error · OpcBizException

OPC10040005

OPC10040005

Error message

OPC10040005

What it means

OPC10040005 ("生成邮件消息体失败") is thrown by OptSendMailServiceImpl.getMimeMessage when building the MIME message with Spring's MimeMessageHelper fails with a MessagingException. It wraps the underlying javax.mail.MessagingException after logging. This means the email envelope (from, to, subject, body) could not be constructed, before any network send is attempted.

Solutions

  1. Validate all recipient addresses (and the from address) with an email regex / InternetAddress.validate() before calling the mail API.
  2. Log and inspect the wrapped MessagingException (it is logged with log.error) to see the exact address or header that failed.
  3. Verify mail sender configuration (spring.mail.username / from property) is present and non-null.
  4. Normalize the input: split the 'to' set cleanly and trim whitespace before passing it to the service.
  5. Catch OpcBizException with code 10040005 at the RPC boundary and return a descriptive error to the caller.

Example fix

// before: raw user input passed straight to mail service
mailService.sendTemplateMail(subject, text, toSet);
// after: validate addresses first
for (String addr : toSet) {
    if (!addr.matches("^[\\w.+-]+@[\\w-]+\\.[\\w.]+$")) {
        throw new IllegalArgumentException("invalid email: " + addr);
    }
}
mailService.sendTemplateMail(subject, text, toSet);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean isValidEmail(String s) {
    return s != null && s.matches("^[\\w.+-]+@[\\w-]+(\\.[\\w-]+)+$");
}
// run for every recipient and the from address before calling the mail API

Type guard

boolean validEmail(String s) { return s != null && s.contains("@") && InternetAddress.parse(s, true).length == 1; }

Try / catch

try {
    mailService.mimeMessage(...);
} catch (OpcBizException e) {
    if (e.getCode() == 10040005) {
        log.error("Mail envelope build failed — check recipient/from addresses", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling the mail service's mimeMessage/send path when helper.setFrom/setTo/setSubject/setText throws MessagingException — typically malformed recipient addresses, invalid 'from' address, or unsupported encoding in subject/text.

Common situations: User-supplied email lists containing invalid addresses (e.g. missing '@', commas splitting wrongly); null 'from' when mail config is incomplete; charset/encoding problems with Chinese or emoji content; JavaMail version mismatch after upgrading spring-boot/mail starter.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of paascloud/paascloud-master@781281a950 (2026-09-10). Data as JSON: /api/errors/6ddc9cb5508eaffa. Report an issue: GitHub.

Appendix: source

Thrown at paascloud-provider/paascloud-provider-opc/src/main/java/com/paascloud/provider/service/impl/OptSendMailServiceImpl.java:102

		return result;
	}

	private MimeMessage getMimeMessage(String subject, String text, Set<String> to) {
		Preconditions.checkArgument(!PubUtils.isNull(subject, text, to), "参数异常, 邮件信息不完整");

		String[] toArray = setToArray(to);
		Preconditions.checkArgument(PublicUtil.isNotEmpty(toArray), "请输入收件人邮箱");
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);
			helper.setTo(toArray);
			helper.setSubject(subject);
			helper.setText(text, true);
		} catch (MessagingException e) {
			log.error("生成邮件消息体, 出现异常={}", e.getMessage(), e);
			throw new OpcBizException(ErrorCodeEnum.OPC10040005);
		}
		return mimeMessage;
	}

	@Override
	public int sendTemplateMail(Map<String, Object> model, String templateLocation, String subject, Set<String> to) {
		log.info("sendTemplateMail - 发送模板邮件. subject={}, model={}, to={}, templateLocation={}", subject, model, to, templateLocation);

		String text;
		try {
			text = optVelocityService.getTemplate(model, templateLocation);
		} catch (IOException | TemplateException e) {
			log.info("sendTemplateMail [FAIL] ex={}", e.getMessage(), e);
			throw new OpcBizException(ErrorCodeEnum.OPC10040006, e);
		}
		return this.sendTemplateMail(subject, text, to);
	}

View on GitHub (pinned to 781281a950)