发送邮件的实现
需要事先引入以下几个架包,最重要的架包是jodd-3.7这个

以上架包下载地址:http://pan.baidu.com/s/1kVs7Tyv 提取密码:h22x
新建一个Util类,其中emails.txt 是用来动态配置需要发送邮件的发送对象
package quartz;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*
* @author DONG
*/
public class Util{
public static final String format = "HH:mm";
public static final SimpleDateFormat sdf = new SimpleDateFormat(format);
public static String content = "以下电桩断网已超过1小时" +"【"+ sdf.format(new Date())+"】";//发送邮件内容
public static Date lastSend = null;
public static List getEmailList(){
return getList("emails.txt");
}
public static List getList(String fileName){
try{
InputStream is = Util.class.getResourceAsStream(fileName);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
List list = new ArrayList();
String line = "";
while( (line = br.readLine()) != null ){
if(!"".equals(line.trim())) list.add(line);
}
br.close();
isr.close();
is.close();
return list;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
以上代码可直接copy复用
接下来就是发送邮件了
public void run() {
List<String> emails = Util.getEmailList();//获取邮件发送对象的集合
if (emails.isEmpty()) {
System.out.println("no email receiver");
return;
}
String from = "********@sina.com";//用户名,登录邮箱的账号
String psw = "**********";//密码
String[] to = emails.toArray(new String[0]);
Email email = Email.create()
.from(from).to(to)
.subject("电桩断网超时提醒")//邮件主题
.addText(Util.content);//邮件内容
SmtpServer smtpServer = SmtpServer.create("smtp.sina.com")//调用新浪邮箱服务器
.authenticateWith(from, psw);
SendMailSession session = smtpServer.createSession();
session.open();
session.sendMail(email);//执行发送
session.close();
System.out.println("--email send success. receivers: " + Arrays.deepToString(emails.toArray()));
}
在需要发送邮件的地方调用run方法即可。以上就是一个超简易的发送邮件示例,亲测有效

下一篇,将补充如何自定义添加邮件内容
- 感谢你的阅读。如果你觉得这篇文章对你有帮助或者有启发,就请推荐一下吧~你的精神支持是博主强大的写作动力。欢迎转载!
- 博主的文章没有高度、深度和广度,只是凑字数。由于博主的水平不高(其实是个菜B),不足和错误之处在所难免,希望大家能够批评指出。
- 欢迎加入.NET 从入门到精通技术讨论群→523490820 期待你的加入
- 不舍得打乱,就永远学不会复原。被人嘲笑的梦想,才更有实现的价值。
- 我的博客:http://www.cnblogs.com/zhangxiaoyong/