用jodd發送電子郵件:
這裏用QQ和126的郵箱進行測試,首先保證你的郵箱服務器已經開通了smtp的服務,一般在設置裏面,比如QQ的就是設置-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務
開啓後項目導入jodd的mail包就可以開始發送郵件了
import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;
import jodd.mail.SmtpSslServer;
public class Test {
public static void main(String[] args) {
// sendQQMail();
send126Mail();
}
public static void sendQQMail(){
Email email = Email.create()
.from("[email protected]")
.to("[email protected]")
.subject("testQQ")
.addText("ab你好!cd")
.addHtml("<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
"<body><h1>你好v</h1></body></html>");
SendMailSession mailSession =
new SmtpSslServer("smtp.qq.com","1234566", "1212121")
.createSession();
mailSession.open();
mailSession.sendMail(email);
mailSession.close();
System.out.println("發送QQ成功!...");
}
public static void send126Mail(){
Email email = Email.create()
.from("[email protected]")
.to("[email protected]")
.subject("test126")
.addHtml("<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" +
"<body>123123123")
.addText("ab你好!cd")
.addHtml("<h1>你好v</h1></body></html>");
SendMailSession mailSession =
new SmtpServer("smtp.126.com","123123", "123123")
.createSession();
mailSession.open();
mailSession.sendMail(email);
mailSession.close();
System.out.println("發送126成功!...");
}
}
由於QQ和126的服務器配置不同,雖然在其各自的配置說明中都說明(POP3/IMAP/SMTP/CardDAV/CalDAV服務均支持SSL連接)但是在QQ中採用SmtpSslServer對象訪問沒有問題,而在126的發送時卻報錯只能用SmtpServer