标签:
package com.yxy.monitor.common; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; import java.util.Properties; /** * 发送邮件共用类 * * @author northEastTycoon * @version 1.0.0 2016/9/21 */ public class MailCommonUtil { protected final static Logger logger = LogManager.getLogger(); // 邮件服务 private static String host; // 发送邮件的用户名 private static String username; // 发送邮件的密码 private static String password; // info 与 from 组成邮箱的发件人信息 private static String from; private static String info; static { try { host = "smtp.163.com"; username = "northEastTycoon@163.com"; password = "northEastTycoon"; from = "northEastTycoon@163.com"; info = "邮件监控"; } catch (Exception e) { e.printStackTrace(); } } /** * 发送邮件 * * @param to 收件人列表,以","分割 * @param subject 标题 * @param body 内容 * @param filepath 附件列表,无附件传递null * @return * @throws MessagingException * @throws AddressException * @throws UnsupportedEncodingException */ public static boolean sendMail(String to, String subject, String body, List<String> filepath) throws AddressException, MessagingException, UnsupportedEncodingException { // 参数修饰 if (body == null) { body = ""; } if (subject == null) { logger.info("发送的邮件无主题!"); } // 创建Properties对象 Properties props = System.getProperties(); // 创建信件服务器 props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); // 通过验证 // 得到默认的对话对象 Session session = Session.getDefaultInstance(props, null); // 创建一个消息,并初始化该消息的各项元素 MimeMessage msg = new MimeMessage(session); info = MimeUtility.encodeText(info); msg.setFrom(new InternetAddress(info + "<" + from + ">")); // 创建收件人列表 if (to != null && to.trim().length() > 0) { String[] arr = to.split(","); int receiverCount = arr.length; if (receiverCount > 0) { InternetAddress[] address = new InternetAddress[receiverCount]; for (int i = 0; i < receiverCount; i++) { address[i] = new InternetAddress(arr[i]); } msg.addRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); // 后面的BodyPart将加入到此处创建的Multipart中 Multipart mp = new MimeMultipart(); // 附件操作 if (filepath != null && filepath.size() > 0) { for (String filename : filepath) { MimeBodyPart mbp = new MimeBodyPart(); // 得到数据源 FileDataSource fds = new FileDataSource(filename); // 得到附件本身并至入BodyPart mbp.setDataHandler(new DataHandler(fds)); // 得到文件名同样至入BodyPart mbp.setFileName(fds.getName()); mp.addBodyPart(mbp); } MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(body); mp.addBodyPart(mbp); // 移走集合中的所有元素 filepath.clear(); // Multipart加入到信件 msg.setContent(mp); } else { // 设置邮件正文 msg.setText(body); } // 设置信件头的发送日期 msg.setSentDate(new Date()); msg.saveChanges(); // 发送信件 Transport transport = session.getTransport("smtp"); transport.connect(host, username, password); transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); transport.close(); return true; } else { logger.error("不存在接收地址!"); return false; } } else { logger.error("不存在接收地址!"); return false; } } } import com.yxy.monitor.common.MailCommonUtil; import javax.mail.MessagingException; import javax.mail.internet.AddressException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; /** * 测试邮件发送操作类 * * @author northEastTycoon * @version 1.0.0 2016/9/21 */ public class testMail { public static void main(String[] args) throws AddressException, UnsupportedEncodingException, MessagingException { List<String> filepath = new ArrayList<>(); // 实际存放的文件地质 filepath.add("E:\\ceshi.txt"); MailCommonUtil.sendMail("northEastTycoon@qq.com,northEastTycoon@qq.com", "测试邮件", "监控功能邮件", null); } }
我用的gradle建立的工程,以上以163邮箱为例子代码,完成以上代码需要jar包,在build.gradle中添加
compile(‘javax.mail:mail:1.4.7‘)
标签:
原文地址:http://www.cnblogs.com/northeastTycoon/p/5908862.html