/**
* @Title: sendHTMLMail
* @Description: TODO(发送邮件)
* @author: cmh
* @date 2020年3月17日 下午4:03:12
* @param title 发送标题
* @param content 发送内容
* @param recipient 接受者邮箱
* @param from 发送人邮箱
* @param pass 发送人密码
* @param emailServer 邮箱服务器
* @param port 邮箱端口
* @param coding 编码格式
* @param addresser 发送人名称
* @throws
*/
public void sendHTMLMail(String title, String content, String recipient, String from, String pass,
String emailServer, Integer port, String coding, String addresser) {
try {
HtmlEmail hemail = new HtmlEmail ();
hemail.setHostName (emailServer);
hemail.setSmtpPort (port);
hemail.setCharset (coding);
hemail.addTo(recipient);
hemail.setFrom( from, addresser);
hemail.setAuthentication( from, pass);
hemail.setSubject(title);
hemail.setContent(content, EmailConstants.TEXT_HTML );
hemail.send ();
//hemail.setHtmlMsg(connect);
System.out.println ( "email发送成功!---------" );
} catch (Exception e) {
e.printStackTrace ();
System.out.println ( "email发送失败!----------" );
}
}
/**
* @Title: checkSend
* @Description: TODO(验证邮箱是否可以发送邮件)
* @author: cmh
* @date 2020年3月17日 下午4:06:37
* @param emailServer 邮箱服务器
* @param port 邮箱端口
* @param emailName 邮箱地址
* @param password 邮箱密码
* @param coding 编码格式
* @throws GeneralSecurityException
* @throws
*/
public boolean checkSend(String emailServer, Integer port, String emailName, String password, String coding) throws GeneralSecurityException {
Properties prop = new Properties();
prop.setProperty("mail.host", emailServer);
prop.setProperty("mail.smtp.port", port.toString());
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//设置超时时间为20秒
prop.setProperty("mail.smtp.timeout", "20000");
boolean result = false;
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts;
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
try {
ts = session.getTransport();
ts.connect(emailServer, emailName, password);
ts.close();
result = true;
} catch (NoSuchProviderException e1) {
result = false;
} catch (MessagingException e) {
result = false;
}
return result;
}