使用 JavaMail 發送郵件時拋出異常:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 554 MI:STC 0,smtp10,DsCowLC7LwPACzRN7qcQAA--.2883S2 1295256512
MI:STC:網易 163 郵箱反垃圾系統標識,代表「發送頻率超限,暫時封禁」。Guava RateLimiter 或線程池 + 延遲隊列,控制 QPS。SendFailedException 解析錯誤碼,觸發熔斷/告警,避免持續重試加重封禁。public void sendWithRetry(MimeMessage msg, int maxRetries) {
for (int i = 0; i < maxRetries; i++) {
try {
Transport.send(msg);
return;
} catch (SendFailedException e) {
if (e.getMessage().contains("554")) {
long delay = (long) Math.pow(2, i) * 1000L; // 1s, 2s, 4s...
log.warn("發送受限,{}ms 後重試 ({}/{})", delay, i+1, maxRetries);
Thread.sleep(delay);
} else {
throw e;
}
}
}
throw new IllegalStateException("超過最大重試次數");
}
554 錯誤本質是郵件服務商的流控保護,而非代碼缺陷。通過「頻率控制 + 賬號養號 + 內容合規 + 多通道備援」組合策略,可有效降低觸發概率,保障業務郵件送達率。