首页 > 学院 > 开发设计 > 正文

邮件群发(配置文件读取多个发送对象)

2019-11-10 18:52:26
字体:
来源:转载
供稿:网友
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;public class SendMailAll { 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; i < str.length; i++) { 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(); } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表