使用 Mailgun 傳送電子郵件

Mailgun API 以 HTTP 為基礎並符合 REST 樣式,它使用資源導向的可預測網址,並且內建用於參數傳送及驗證的 HTTP 功能。Mailgun API 會使用標準 HTTP 回應碼來回應,以表示錯誤狀態並傳回 JSON 值。

Mailgun 已發佈各種程式語言適用的程式庫。您可以使用這些程式庫或是自己偏好的 HTTP/REST 程式庫,向 Mailgun 發出 HTTP 呼叫。

如需更多以其他程式設計語言撰寫的程式碼範例,請參閱 Mailgun 說明文件

定價

Google Cloud Platform 使用者每月傳送的前 30,000 封電子郵件免費。請參閱申請網頁上的每月計費工具,進一步瞭解額外電子郵件的定價和大量寄信的折扣。

設定

  • 為了確保您以 Google Cloud Platform 客戶的身分取得優惠價格,請透過註冊入口網站建立新的 Mailgun 帳戶
  • 將所需的 jersey REST 用戶端模組新增至應用程式。例如,如果您使用 Maven,請新增至專案的 pom.xml:

Java 8

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-core</artifactId>
  <version>1.19.4</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.19.4</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey.contribs</groupId>
  <artifactId>jersey-multipart</artifactId>
  <version>1.19.4</version>
</dependency>

Java 7

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-core</artifactId>
  <version>1.19.4</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.19.4</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey.contribs</groupId>
  <artifactId>jersey-multipart</artifactId>
  <version>1.19.4</version>
</dependency>

傳送純文字電子郵件

Java 8

@SuppressWarnings("VariableDeclarationUsageDistance")
private ClientResponse sendSimpleMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  MultivaluedMapImpl formData = new MultivaluedMapImpl();
  formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.add("to", recipient);
  formData.add("subject", "Simple Mailgun Example");
  formData.add("text", "Plaintext content");
  return webResource
      .type(MediaType.APPLICATION_FORM_URLENCODED)
      .post(ClientResponse.class, formData);
}

Java 7

private ClientResponse sendSimpleMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  MultivaluedMapImpl formData = new MultivaluedMapImpl();
  formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.add("to", recipient);
  formData.add("subject", "Simple Mailgun Example");
  formData.add("text", "Plaintext content");
  WebResource webResource = client.resource(
      "https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,
      formData);
}

傳送 HTML 與文字電子郵件

傳送含有 HTML 與文字部分的電子郵件,以下範例也會在電子郵件中附加檔案:

Java 8

@SuppressWarnings("VariableDeclarationUsageDistance")
private ClientResponse sendComplexMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  FormDataMultiPart formData = new FormDataMultiPart();
  formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.field("to", recipient);
  formData.field("subject", "Complex Mailgun Example");
  formData.field("html", "<html>HTML <strong>content</strong></html>");
  ClassLoader classLoader = getClass().getClassLoader();
  File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
  formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
  return webResource
      .type(MediaType.MULTIPART_FORM_DATA_TYPE)
      .post(ClientResponse.class, formData);
}

Java 7

private ClientResponse sendComplexMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  FormDataMultiPart formData = new FormDataMultiPart();
  formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.field("to", recipient);
  formData.field("subject", "Complex Mailgun Example");
  formData.field("html", "<html>HTML <strong>content</strong></html>");
  ClassLoader classLoader = getClass().getClassLoader();
  File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
  formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
  WebResource webResource = client.resource(
      "https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
      .post(ClientResponse.class, formData);
}

回應範例:

{
    "message": "Queued. Thank you.",
    "id": "<20111114174239.25659.5817@samples.mailgun.org>"
}

瞭解詳情

如需進一步瞭解更多範例與資訊 (包括如何追蹤和轉送電子郵件),請參閱 Mailgun 說明文件

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Java 適用的 App Engine 標準環境