天天看點

jodd mail技術實作簡單發送email郵件

maven 依賴

<dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-mail</artifactId>
            <version>3.7</version>
        </dependency>
           

java jodd mail 用到的jar包下載下傳位址

程式

package com.mail.test;

import jodd.mail.*;
import org.junit.Test;

import java.io.File;

/**
 * Created by simon on 2016/4/20.
 */
public class test {

//支援html頁面展示
    public static void main(String args[]){
        String [] emails={"目标郵箱2","目标郵箱1"};
        SmtpServer<?> smtp = SmtpSslServer.create("你的SSL的smtp伺服器",);//設定SSL的smtp伺服器,我的,smtp.mxhichina.com
        smtp.authenticateWith("你的smtp郵箱", "你的密碼");//我的,[email protected],密碼省略....
        SendMailSession session = smtp.createSession();
        Email email = Email.create().from("你的smtp郵箱").subject("郵件标題")//設定了主題
                .to(emails)
                .addHtml("<p style='background:green'>html的檔案内容!</p>")
                ;
        session.open();//打開連接配接
        session.sendMail(email);//發送郵件
        session.close();//關閉連接配接
        System.out.println("發送成功");
    }
    //支付多附件
//    public static  void main(String ss[]) throws IOException {
//        sendMail("[email protected]","/home/simon/桌面/cwch824.2/cwch/WebRoot/download/yingyu/英語選修6電子檔.pdf");
//    }

    public static void sendMail(String mail,String filePath) throws IOException {

        EmailAttachment attachment = new FileAttachment(
                new File(filePath), "考的好.pdf", "pdf");

        String [] emails={mail};
        SmtpServer<?> smtp = SmtpSslServer.create("smtp.mxhichina.com",);
        smtp.authenticateWith("[email protected]", "****");
        SendMailSession session = smtp.createSession();
        Email email = Email.create().from("[email protected]").subject("考的好系統郵件")
                .to(emails).attach(attachment)
                .addText("你好,你下載下傳的内容在附件裡面")
                ;
        session.open();//打開連接配接
        session.sendMail(email);//發送郵件
        session.close();//關閉連接配接
        System.out.println("發送郵件成功");
    }
}
           

親測可用,不懂可追問。