package com.cn.javamail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo2 {
public static void main(String[] args) throws Exception{
//定一个Properties对象,来设置服务器环境信息
Properties props=new Properties();
//设置要与服务器会话要进行身份认证
props.setProperty("mail.smtp.auth", "true");
//定义邮件传输的协议是方式
props.setProperty("mail.transport.protocol", "smtp");
//设置服务器的主机
props.setProperty("mail.host", "smtp.sina.com");
Session session=Session.getInstance(props,
new Authenticator() {
//发件人邮箱的用户民、密码
private String userName="XXXXXX";
private String password="XXXXX";
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
}
);
session.setDebug(true);
//新建一个邮件对象,用于创建一份邮件和解析邮件
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));
msg.setSubject("多人发送邮件测试");
//msg.setText("XXXXXXXXXXXXXXXXXXXXXXXX--------XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
msg.setContent("<span style='color:red'>XXXXXXXXXXXXXXXXXXXXXXXX----中文----XXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>", "text/html;charset=utf-8");
msg.setSentDate(new Date());
//设置收件人的类型,以及收件人邮箱
msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]"));
//发送邮件
Transport.send(msg);
}
}