使用Jodd簡化郵件服務的開發

最近須要作一個郵件相關的功能,考慮到自建郵件服務器維護起來比較麻煩,因而選擇第三方郵件服務。郵件發送服務的對比能夠看這篇文章html

最後我選擇了:java

sendCloud向QQ用戶發送,mailgun則負責其餘用戶。這就須要我同時爲這兩種服務編寫相應的接口。可是它們的文檔裏給出的示例代碼又各不相同,有的還不夠優雅,好比sendcloud的java示例。二者依賴的庫又不相同,好比mailgun依賴web

  • jersey-client.jar (version ~ 1.17 - 1.18.1)
  • jersey-core.jar (version ~ 1.17 - 1.18.1)
  • jersey-multipart.jar (version ~ 1.17 - 1.18.1)

sendCloud則依賴HttpClient。有沒有一種既能簡化代碼又能實現功能的方案呢,而後我發現了Joddjson

Jodd介紹:segmentfault

Jodd is set of Java micro frameworks, tools and utilities, under 1.5 MB.
Designed with common sense to make things simple, but not simpler.
Get things done! Build your Beautiful Ideas! Kickstart your Startup!
And enjoy the coding.api

很喜歡Jodd的理念:服務器

Think Lightweight, Be Awesome, Get Things Done!

下面開始正題:須要用到的依賴以下:session

<properties>
    <jodd.version>3.6.5</jodd.version>
  </properties>

  <dependency>
        <groupId>org.jodd</groupId>
        <artifactId>jodd-http</artifactId>
        <version>${jodd.version}</version>
  </dependency>

  <dependency>
         <groupId>org.jodd</groupId>
         <artifactId>jodd-mail</artifactId>
         <version>${jodd.version}</version>
  </dependency>

Talk is cheap,貼出代碼。如下僅僅給出簡單的發送功能,起拋磚引玉的做用,其餘功能詳見官網文檔。post

mailgun:ui

import jodd.http.HttpRequest;
import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;

import java.util.HashMap;
import java.util.Map;


public class MailGun {

    private String SMTP_HOST = "smtp.mailgun.org";

    private String SMTP_USER="YOUR_USER";

    private String SMTP_PASS = "YOUR_PASSWORD";

    private String HTTP_URL="https://api.mailgun.net/v3/YOUR_DOMAIN/messages";

    private String HTTP_API="YOUR_API";


    /**
     * 使用SMTP觸發發送
     * @param from 發件人
     * @param to 收件人
     * @param subject 主題
     * @param text 內容
     */
    public void sendBySMTP(String from,String to,String subject,String text){
        SmtpServer smtpServer = SmtpServer.create(SMTP_HOST)
                .authenticateWith(SMTP_USER, SMTP_PASS);
        SendMailSession session = smtpServer.createSession();
        session.open();

        Email email = Email.create()
                .from(from)
                .to(to)
                .subject(subject)
                .addHtml(text);
        session.sendMail(email);
        session.close();
    }

    /**
     * 使用HTTP方式發送
     * @param from 發件人
     * @param to 收件人
     * @param subject 主題
     * @param text 內容
     */
    public void sendByHTTP(String from,String to,String subject,String text){
        Map<String, Object> formData = new HashMap<String, Object>();
        formData.put("from", from);
        formData.put("to", to);
        formData.put("subject", subject);
        formData.put("text", text);
        HttpRequest
                .post(HTTP_URL)
                .basicAuthentication("api",HTTP_API)
                .form(formData).send();
    }
}

sendCloud:

import jodd.http.HttpRequest;
import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;

import java.util.HashMap;
import java.util.Map;

public class SendCloud {

    private String HTTP_URL ="http://sendcloud.sohu.com/webapi/mail.send.json";

    private String HTTP_API_USER ="YOUR_API_USER";

    private String HTTP_API_KEY = "YOUR_API_KEY";

    private String FROM = "YPUR_DOMAIN_EMAIL";

    private String SMTP_HOST = "smtpcloud.sohu.com";

    private String SMTP_USER="YOUR_USER";

    private String SMTP_PASS = "YOUR_PASS";

    public void sendBySMTP(String to, String subject, String text){
        SmtpServer smtpServer = SmtpServer.create(SMTP_HOST)
                .authenticateWith(SMTP_USER, SMTP_PASS);
        SendMailSession session = smtpServer.createSession();
        session.open();

        Email email = Email.create()
                .from(FROM)
                .to(to)
                .subject(subject)
                .addHtml(text);
        session.sendMail(email);
        session.close();
    }

    public void sendByHTTP(String to, String subject, String text){
        Map<String, Object> formData = new HashMap<String, Object>();
        formData.put("api_user", HTTP_API_USER);
        formData.put("api_key", HTTP_API_KEY);
        formData.put("from", FROM);
        formData.put("to", to);
        formData.put("subject", subject);
        formData.put("html", text);
        HttpRequest
                .post(HTTP_URL)
                .form(formData)
                .send();
    }
}
相關文章
相關標籤/搜索