import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
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 Mail01 {
public static void main(String[] agrs){
Properties props = new Properties();
//设置邮箱服务器地址
props.setProperty("mail.smtp.host", "smtp.126.com");
// 必须有这项设置
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.debug", "true");
// Session管理与stmp的链接信息
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("xieyang036@126.com","自己邮箱地址");
}
});
// 由邮件会话新建一个消息对象
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("xieyang036@126.com"));
//群发
//msg.addRecipients(Message.RecipientType.TO,"xieyang036@126.com,664262076@qq.com");
//单发
msg.addRecipients(Message.RecipientType.TO,"664262076@qq.com");
//设置邮件主题
msg.setSubject("测试邮件", "UTF-8");
msg.setSentDate(new Date());
//设置邮件内容
msg.setText("你好,这是一封测试邮件。收到请勿回复!终于成功了纠结死老子了!!!(+﹏+)~狂晕", "UTF-8");
// 使用静态的方法发送信息
//Transport.send(msg,new InternetAddress[]{new InternetAddress("lianyumoshen@sina.com")});
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
评论2
最新资源