public class Demo1 {
/**
* @param args
* @throws MessagingException
*/
public static void main(String[] args) throws MessagingException {
//第一种方式方式
//send1();
//第二种发送方式
send2();
}
public static void send1()
{
try {
/**
构建发送环境
*/
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");//接受认证
properties.setProperty("mail.transport.protocol", "smtp");//设置发送协议
Session session =Session.getDefaultInstance(properties);
session.setDebug(true);//设置在控制台打印调试信息
/**
* 构建邮件
*/
Message msg = new MimeMessage(session);
msg.setText("逗你玩"); //发送内容
msg.setFrom(new InternetAddress("xxxx@qq.com"));//设置发送邮件方地址
/*
构建发送类
*/
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", 25, "xxxxxx用户名", "zxxxx密码");//设置要连接的服务器地址、端口、用户名、密码
transport.sendMessage(msg, new InternetAddress[]{new InternetAddress("13628303286@163.com")});//发送邮件给某些人
transport.close();//关闭发送链接
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void send2() throws AddressException, MessagingException
{
/**
构建发送环境
*/
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");//接受服务器认证
properties.setProperty("mail.transport.protocol", "smtp");//设置发送协议
properties.setProperty("mail.host", "smtp.qq.com");//设置要连接的服务器地址,端口默认25
Session session = Session.getInstance(properties,new Authenticator() { //策略模式
@Override
protected PasswordAuthentication getPasswordAuthentication() { //返回用户名和密码
// TODO Auto-generated method stub
return new PasswordAuthentication("xxxx用户名", "xxxxxxx密码"); //设置用户名和密码
}
});
session.setDebug(true); //显示调试信息
/**
* 构建邮件
*/
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("594389@qq.com"));//设置发送方地址
msg.setSubject("中文主题");
msg.setRecipients(RecipientType.TO, InternetAddress.parse("2399548@qq.com,13628303286@163.com,594389@qq.com")); //设置收件人的类型:TO:收件人;CC:抄送;BCC:暗送;和收件人
msg.setContent("<span style='color:red'>中文呵呵</span>", "text/html;charset=gbk");//设置发送内容,以及内容的类型和编码
/**
* 发送邮件
*/
Transport.send(msg);
}
}
注:工程需要引入mail.jar包,如果运行环境低于jdk6还需引入activation.jar包
下载地址:http://download.youkuaiyun.com/user/zl594389970