设置收件:
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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 MessagingException {// TODO Auto-generated method stub
Properties props = new Properties();
//认证
props.setProperty("mail.smtp.auth", "true");
//没有这个协议,加入mail.transport.protocol
props.setProperty("mail.transport.protocol", "smtp");
//指定服务器地址,并且连接,端口默认
props.setProperty("mail.host", "smtp.sina.com");
Session session = Session.getInstance(props,
new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("shywjr", "123456");
}
}
//定义了一个类,覆盖了父类的方法
);
//加入调试信息,打印调试信息
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("1476739095@qq.com"));
msg.setSubject("中文主题");
//设置收件人,两个收件人
msg.setRecipients(RecipientType.TO, InternetAddress.parse("1476739095@qq.com ,lili@qq.com"));
//正文
msg.setContent("<span style='color:red'>哈哈哈哈</span>","text/html;charset=gbk");
Transport.send(msg);
}
}