properties配置文件
address=15938702026@163.com;ytfunny@126.com;2812420513@qq.com
package sendmailtest
import java.io.FileInputStream
import java.util.Properties
import javax.mail.Address
import javax.mail.Authenticator
import javax.mail.Message
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
/**
*properties配置文件的读写解析和邮件群发
*/
public class SendMailAll {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
// 收件人电子邮箱
// String to="15938702026@163.com;ytfunny@126.com;2812420513@qq.com"
FileInputStream fis=new FileInputStream("MailAddress")
byte[] ma=new byte[1024]
int a=fis.read(ma)
String s=new String(ma,0,a)
String[] str=s.split(";")
// String[] str=to.split(";")
Address[] add=new Address[str.length]
for (int i = 0
Address adr =new InternetAddress(str[i])
add[i]=adr
}
// 发件人电子邮箱
String from="ytfunny@126.com"
// 获取系统属性
Properties pro=System.getProperties()
// 设置邮件服务器
pro.setProperty("mail.smtp.host","smtp.126.com")
pro.put("mail.smtp.auth", "true")
// 获取默认的 Session 对象。
Session session=Session.getDefaultInstance(pro,new Authenticator() {
public javax.mail.PasswordAuthentication getPasswordAuthentication(){
//发件人邮件用户名、密码
return new javax.mail.PasswordAuthentication("ytfunny@126.com", "ytfssg001")
}
})
// 创建默认的 MimeMessage 对象。
try {
MimeMessage message=new MimeMessage(session)
// Set From: 头部头字段
message.setFrom(new InternetAddress(from))
// Set To: 头部头字段
// message.addRecipient(Message.RecipientType.TO, new InternetAddress("ytfunny@126.com"))
message.addRecipients(Message.RecipientType.TO, add)
// Set Subject: 头部头字段
message.setSubject("百炼江湖人未尽!")
// 设置消息体
// message.setContent(message,"枯骨如山杯莫停!")
message.setText("内容")
// 发送消息
Transport.send(message)
System.out.println("发送成功")
} catch (Exception e) {
e.printStackTrace()
}
}
}