package com.yihu.utils; /** * */ import java.io.File; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; /** * @author 林小辉 * @company * 2017-05-26 */ public class MailUtil { public static class MyAuthenticator extends Authenticator{ String name; String password; public MyAuthenticator(String name,String password){ this.name = name; this.password = password; this.getPasswordAuthentication(); } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(name,password); } } /** * * @param subject 主题 * @param context 邮件内容 * @param emails 收件人(多人) * @param attachment 附件 (null标识不发送邮件) * @throws Exception */ public static void send(String subject,String context,String[] emails) throws Exception{ //发送邮箱的用户名和密码 String name = "mailmaster@jkzl.com"; String password = "qazwsx321$1"; Properties props = System.getProperties(); //设置smtp主机 props.put("mail.smtp.host", "smtp.263.net"); //设置smtp验证属性 props.put("mail.smtp.auth","true"); MyAuthenticator auth = new MyAuthenticator(name,password); //获得邮件会话对象 Session session = Session.getDefaultInstance(props, auth); //创建MIME邮件对象 MimeMessage message = new MimeMessage(session); subject = MimeUtility.encodeWord(subject, "UTF-8", "Q"); message.setSubject(subject); //设置邮件发送人 message.setFrom(new InternetAddress("mailmaster@jkzl.com")); //设置邮件收件人 InternetAddress[] as = new InternetAddress[emails.length]; for (int i = 0;i