一、目录结构
记得要导包:
SendMail代码: package cn.xh.mail; import java.util.Properties; 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; import javax.mail.internet.MimeMessage.RecipientType; import org.junit.Test; public class SendMail { @Test public void sendMail() throws Exception { /* * 1. 得到session */ Properties pro=new Properties(); pro.setProperty("mail.host", "smtp.163.com"); pro.setProperty("mail.smtp.auth", "true"); Authenticator authenticator=new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("163邮箱账号", "授权后的密码");//记得邮箱要授权 } }; Session session=Session.getInstance(pro, authenticator); /* * 2. 创建MimeMessage */ MimeMessage message=new MimeMessage(session); message.setFileName("测试邮件"); message.setFrom(new InternetAddress("163邮箱账号")); message.setRecipients(RecipientType.TO, "2xxxxxxx5@qq.com");//接收者的账号 message.setSubject("来自外星的邮件"); message.setContent("这是一封秘密邮件", "text/html;charset=utf-8"); /* * 3. 狂发 */ for(int i=0;i<=100;i++) { Transport.send(message); } } }