日期:2014-05-17 浏览次数:20886 次
package qq;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* @see 该类用来发送qq邮件
* @version 1.0
* */
public class EmailTool implements Serializable {
public static void main(String[] args) {
try {
/**只要你的电脑能上网,发件人账号密码地址都正确设置,这个邮件就绝对能发出去*/
String email = "admin@163.com";// 收件人地址
String title = "找回密码";// 邮件标题
String url = "http://www.baidu.com";
String img = "http://img1.kaixin001.com.cn/i/sy_tp1_1.gif";
String templetPath = "D:\\test\\email\\findPass.txt";
String []args1 = new String[]{ "洪秀全", "hongxiuquan", email, url, url, url, img };// 邮件模板的参数设置
System.out.println(bean.sendEmail(email, title, templetPath, args1, templetPath));// 发送邮件
} catch (Exception e) {
e.printStackTrace();
}
}
private static final long serialVersionUID = 1L;
private static EmailTool bean = new EmailTool();
/**
* @see 以单例模式获得javabean
* @return Email
* */
public static EmailTool getBean(){
return bean;
}
/**
* @see 用QQ邮箱发邮件
* @param toEmail 收件人地址
* @param title 邮件标题
* @param templetPath 模板路径(物理路径)
* @param args 模板中需要替换的值
* @param affixPath 附件的路径(物理路径,可以为null)
* @return boolean
* */
public Boolean sendEmail(String toEmail, String title, String templetPath,
String []args, String affixPath) {
Boolean result = false;
try {
Properties properties = new Properties();
Session session = Session.getInstance(properties, null);
properties.put("mail.smtp.host", emailServerIP);// 设置服务器的IP或域名
properties.put("mail.smtp.auth", "true");// 允许smtp校验
Transport transport = session.getTransport("smtp");
transport.connect(emailServerIP, fromEmailAccount, fromEmailPassword);// 设置发件人的用户名和密码
Message message = new MimeMessage(session);
message.setSubject(title);// 设置邮件主题