JavaMail学习笔记

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.SimpleEmail;

public class SendMail {

    public void sendMail(String toAddr,String url) {    	
    	InputStream is = this.getClass().getResourceAsStream("/mailInfo.properties");
    	Properties prop = new Properties();
    	try {
			prop.load(is);//加载资源文件
		} catch (IOException e1) {
			e1.printStackTrace();
		}
    	try{
	        String msgText = "请点击下面的连接激活用户,如果不能点击请手动复制到地址栏中执行\n" + url;
	        String smtpHost = prop.get("smtpHost").toString();//SMTP服务器名
	        String from = prop.get("mailName").toString();//发信人地址
	        String pwd = prop.get("pwd").toString();//密码	        
	        SimpleEmail email = new SimpleEmail();//创建发送邮件的对象
	        email.setCharset("GB2312");//设置字符编码
	        email.setHostName(smtpHost);//设置邮箱服务器
	        email.setAuthentication(from, pwd);//设置登录邮箱和密码
	        email.setFrom(from);
	        email.addTo(toAddr);//设置收件人地址
	        email.setSentDate(new Date());//设置发送时间
	        email.setSubject("激活用户注册");//设置主题
	        email.setMsg(msgText);//设置邮件内容
	        email.send();  //发送邮件       
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    }
}
 

 

你可能感兴趣的:(javamail)