// 收件人电子邮箱
//String to = "fupeng@okjiaoyu.cn";
// 发件人电子邮箱
String from = "fupeng@okjiaoyu.cn";
// 指定发送邮件的主机为 localhost
String host = "smtp.exmail.qq.com";
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("fupeng@okjiaoyu.cn", "kunpengkU1"); //发件人邮件用户名、密码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
String [] to = {"fupeng@okjiaoyu.cn","fupeng_2005@126.com"};
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
System.out.println("发送到:" + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}
// Set To: 头部头字段
message.setRecipients(Message.RecipientType.TO,
sendTo);
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....from w3cschool.cc");
}catch (MessagingException mex) {
mex.printStackTrace();
}
Java 发邮件
最新推荐文章于 2022-05-23 10:33:37 发布