Interface Mailer

All Known Implementing Classes:
DirectMailer, RoutingMailer

public interface Mailer

郵件服務的消費端介面

消費端注入 Mailer、以 compose() 組信寄出即可,不需要知道背後是哪種實作

  • DirectMailer:綁定單一 JavaMailSender 的具體 Mailer(外寄政策於建構時套用)
  • RoutingMailer:寄信當下依 MailerSpecProvider 解析目標(範圍專屬設定 → 系統預設), 對消費端呈現為「一個會自己找到正確設定的 Mailer」

組裝(誰當 Mailer bean、政策長什麼樣)collect 在應用的 mail 組態(seam), 消費端與其他 feature 一律面向本介面。

寄信

mailer.compose()
      .to("alice@example.com")
      .subject("Hello")
      .html("<p>hi</p>")
      .send();

寄件者(from)由實作預填(DirectMailerdefaultFrom),呼叫端可用 MimeMessageBuilder.from(String) 覆寫。

無可用郵件服務時

RoutingMailer 在「解析不到任何設定」時,compose() / send(MimeMessage) 拋出 MailUnavailableException。「這個部署完全沒配郵件」則以「無 Mailer bean」表達, 消費端以 ObjectProvider<Mailer> 接。

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    連接測試結果
  • Method Summary

    Modifier and Type
    Method
    Description
    組一封信(from 已預填為本 Mailer 的預設寄件者)
    void
    send(jakarta.mail.internet.MimeMessage mimeMessage)
    寄出一封已組好的 MimeMessage(外寄政策——防火牆/改寄/特殊訊息——在此套用)
    測試與郵件伺服器的連線
  • Method Details

    • compose

      組一封信(from 已預填為本 Mailer 的預設寄件者)
      Throws:
      MailUnavailableException - 無可解析的郵件設定(見類別註解)
    • send

      void send(jakarta.mail.internet.MimeMessage mimeMessage) throws org.springframework.mail.MailException
      寄出一封已組好的 MimeMessage(外寄政策——防火牆/改寄/特殊訊息——在此套用)
      Throws:
      MailBlockedException - 全部收件者被防火牆擋下
      MailUnavailableException - 無可解析的郵件設定
      org.springframework.mail.MailException
    • testConnection

      測試與郵件伺服器的連線
      Throws:
      MailUnavailableException - 無可解析的郵件設定