{"record":{"id":"6ddc9cb5508eaffa","repo":"paascloud/paascloud-master","slug":"opc10040005","errorCode":"OPC10040005","errorMessage":"OPC10040005","messagePattern":"OPC10040005","errorType":"error_code","errorClass":"OpcBizException","httpStatus":null,"severity":"error","filePath":"paascloud-provider/paascloud-provider-opc/src/main/java/com/paascloud/provider/service/impl/OptSendMailServiceImpl.java","lineNumber":102,"sourceCode":"\t\treturn result;\n\t}\n\n\tprivate MimeMessage getMimeMessage(String subject, String text, Set<String> to) {\n\t\tPreconditions.checkArgument(!PubUtils.isNull(subject, text, to), \"参数异常, 邮件信息不完整\");\n\n\t\tString[] toArray = setToArray(to);\n\t\tPreconditions.checkArgument(PublicUtil.isNotEmpty(toArray), \"请输入收件人邮箱\");\n\t\tMimeMessage mimeMessage = mailSender.createMimeMessage();\n\t\tMimeMessageHelper helper;\n\t\ttry {\n\t\t\thelper = new MimeMessageHelper(mimeMessage, true);\n\t\t\thelper.setFrom(from);\n\t\t\thelper.setTo(toArray);\n\t\t\thelper.setSubject(subject);\n\t\t\thelper.setText(text, true);\n\t\t} catch (MessagingException e) {\n\t\t\tlog.error(\"生成邮件消息体, 出现异常={}\", e.getMessage(), e);\n\t\t\tthrow new OpcBizException(ErrorCodeEnum.OPC10040005);\n\t\t}\n\t\treturn mimeMessage;\n\t}\n\n\t@Override\n\tpublic int sendTemplateMail(Map<String, Object> model, String templateLocation, String subject, Set<String> to) {\n\t\tlog.info(\"sendTemplateMail - 发送模板邮件. subject={}, model={}, to={}, templateLocation={}\", subject, model, to, templateLocation);\n\n\t\tString text;\n\t\ttry {\n\t\t\ttext = optVelocityService.getTemplate(model, templateLocation);\n\t\t} catch (IOException | TemplateException e) {\n\t\t\tlog.info(\"sendTemplateMail [FAIL] ex={}\", e.getMessage(), e);\n\t\t\tthrow new OpcBizException(ErrorCodeEnum.OPC10040006, e);\n\t\t}\n\t\treturn this.sendTemplateMail(subject, text, to);\n\t}\n","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-provider/paascloud-provider-opc/src/main/java/com/paascloud/provider/service/impl/OptSendMailServiceImpl.java#L84-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate all recipient addresses (and the from address) with an email regex / InternetAddress.validate() before calling the mail API.","Log and inspect the wrapped MessagingException (it is logged with log.error) to see the exact address or header that failed.","Verify mail sender configuration (spring.mail.username / from property) is present and non-null.","Normalize the input: split the 'to' set cleanly and trim whitespace before passing it to the service.","Catch OpcBizException with code 10040005 at the RPC boundary and return a descriptive error to the caller."],"exampleFix":"// before: raw user input passed straight to mail service\nmailService.sendTemplateMail(subject, text, toSet);\n// after: validate addresses first\nfor (String addr : toSet) {\n    if (!addr.matches(\"^[\\\\w.+-]+@[\\\\w-]+\\\\.[\\\\w.]+$\")) {\n        throw new IllegalArgumentException(\"invalid email: \" + addr);\n    }\n}\nmailService.sendTemplateMail(subject, text, toSet);","handlingStrategy":"validation","validationCode":"public static boolean isValidEmail(String s) {\n    return s != null && s.matches(\"^[\\\\w.+-]+@[\\\\w-]+(\\\\.[\\\\w-]+)+$\");\n}\n// run for every recipient and the from address before calling the mail API","typeGuard":"boolean validEmail(String s) { return s != null && s.contains(\"@\") && InternetAddress.parse(s, true).length == 1; }","tryCatchPattern":"try {\n    mailService.mimeMessage(...);\n} catch (OpcBizException e) {\n    if (e.getCode() == 10040005) {\n        log.error(\"Mail envelope build failed — check recipient/from addresses\", e);\n    }\n    throw e;\n}","preventionTips":["Validate email syntax on input at the API boundary.","Never pass null from/subject; supply defaults from config.","Keep mail starter/JavaMail versions aligned with Spring Boot.","Test templates with non-ASCII subjects to catch encoding issues.","Sanitize recipient sets (trim, dedupe, drop empties)."],"tags":["email","javamail","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}