本文代码主要意图是将log内容发给指定list里的联系人。
package com.yinger.util.MailRobot;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* SMTP Mail
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 12:42:52
* @version 1.00
*/
public final class SMTPMail {
private static Properties props;
private SMTPMail() {
}
/**
* 静态构造器
*/
static {
SMTPMail.props = new Properties();
// 存储发送邮件服务器的信息
SMTPMail.props.put("mail.smtp.host", MailRobotConst.SMTPServerName);
SMTPMail.props.put("mail.smtp.port", MailRobotConst.Port);
// 同时通过验证
SMTPMail.props.put("mail.smtp.auth", MailRobotConst.Auth);
SMTPMail.props.put("mail.smtp.socketFactory.port", "465");
SMTPMail.props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
}
public static void sent(String concept) {
List<String> mailAddrLs = new ArrayList<String>();
mailAddrLs = (new GetMembers()).getXMLMembers();
for (int i = 0; i < mailAddrLs.size(); i++) {
String addr = mailAddrLs.get(i);
System.out.println("====>" + addr);
sendMail(addr, MailRobotConst.titleMode, concept);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 发送邮件
*
* @param emailAddr:收信人邮件地址
* @param mailTitle:邮件标题
* @param mailConcept:邮件内容
*/
public static void sendMail(String emailAddr, String mailTitle,
String mailConcept) {
// 根据属性新建一个邮件会话
Session s = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MailRobotConst.SMTPUserName, MailRobotConst.SMTPPassword);
}
});
// 设置调试标志,要查看经过邮件服务器邮件命令,可以用该方法
s.setDebug(true);
// 由邮件会话新建一个消息对象
Message message = new MimeMessage(s);
try {
// 设置发件人
Address from = new InternetAddress(MailRobotConst.SenderEmailAddr);
message.setFrom(from);
// 设置收件人
Address to = new InternetAddress(emailAddr);
message.setRecipient(Message.RecipientType.TO, to);
// 设置主题
message.setSubject(mailTitle);
// 设置信件内容
message.setText(mailConcept);
// 设置发信时间
message.setSentDate(new Date());
// 存储邮件信息
message.saveChanges();
Transport.send(message);
System.out.println("发送邮件,邮件地址:" + emailAddr + " 标题:" + mailTitle
+ " 内容:" + mailConcept + "成功!");
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("发送邮件,邮件地址:" + emailAddr + " 标题:" + mailTitle
+ " 内容:" + mailConcept + "失败! 原因是" + e.getMessage());
}
}
}
GetMembers.java
package com.yinger.util.MailRobot;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* members
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 11:42:14
* @version 1.00
*/
public class GetMembers {
public List<String> getXMLMembers() {
List<String> membersLs = new ArrayList<String>();
Document document;
Element root;
String xmlFile = "mailmembers.xml";
File file = new File(xmlFile);
try {
if (file.exists()) {
SAXReader reader = new SAXReader();
document = reader.read(file);
root = document.getRootElement();
} else {
document = DocumentHelper.createDocument();
root = document.addElement("members");
}
List infoNodes = root.elements("member");
for (Iterator it = infoNodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
String addr = elm.elementText("addr");
membersLs.add(addr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return membersLs;
}
}
mailmembers.xml
<?xml version="1.0" encoding="UTF-8"?>
<members>
<member>
<addr>linying0620@gmail.com</addr>
</member>
<member>
<addr>melody.crazycoding@gmail.com</addr>
</member>
</members>
MailRobotConst.java
注:注册部分为163配置代码
package com.yinger.util.MailRobot;
/**
* main const
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 11:38:47
* @version 1.00
*/
public class MailRobotConst {
// // 邮件发送者地址
// public static final String SenderEmailAddr = "xxx@163.com";
//
// // 邮件发送者邮箱用户
// public static final String SMTPUserName = "xxx@163.com";
//
// // 邮件发送者邮箱密码
// public static final String SMTPPassword = "xxx";
//
// // 邮件发送者邮箱SMTP服务器
// public static final String SMTPServerName = "xxx";
//
// public static final String Port = "25";
// 邮件发送者地址
public static final String SenderEmailAddr = "yinger.android@gmail.com";
// 邮件发送者邮箱用户
public static final String SMTPUserName = "yinger.android@gmail.com";
// 邮件发送者邮箱密码
public static final String SMTPPassword = "xxx";
// 邮件发送者邮箱SMTP服务器
public static final String SMTPServerName = "smtp.gmail.com";
public static final String Port = "465";// 587
// 传输类型
public static final String TransportType = "smtp";
// host
public static String Host = SMTPServerName;
public static String Auth = "true";
public static String titleMode = "Log";
}
调用方法:
SMTPMail.sent("test");
posted on 2011-07-21 09:07
Ying-er 阅读(835)
评论(0) 编辑 收藏 所属分类:
SMTP