用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("123@qq.com")
.to("123@126.com")
.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("123@126.com")
.to("23123@126.com")
.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