import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.*; /** * @description: 发送邮件工具类 * @author: hesn **/ @Service public class SendMailUtil { /** * log4j 日志 */ private Logger logger = LoggerFactory.getLogger(this.getClass().getName()); /** * 邮箱host */ //exmail. private static final String EMAIL_HOST = "smtp.163.com"; // private static final String EMAIL_HOST = "smtp.exmail.qq.com"; /** * 邮箱用户名 */ private static final String USER_NAME = "XXXXX@163.com"; /** * 邮箱密码 */ private static final String USER_PASS = "XXXX";//授权码 private static SendMailUtil instance = null; private SendMailUtil() { } public static SendMailUtil getInstance() { if (instance == null) { instance = new SendMailUtil(); } return instance; } /** * 邮件发送 * * @param to 邮件接收人 * @param cs 抄送邮箱 * @param ms 密送邮箱 * @param subject 邮件主题 * @param content 邮件内容 * @param fileList 附件 */ public void send(String to, String cs, String ms, String subject, String content, String fileList[]) throws MessagingException, UnsupportedEncodingException { // try { Properties p = new Properties(); p.put("mail.smtp.auth", "true"); p.put("mail.transport.protocol", "smtp"); p.put("mail.smtp.host", EMAIL_HOST); //994 587 465 端口不对就试别的 p.put("mail.smtp.port", "465"); p.setProperty("mail.smtp.ssl.enable", "true"); // 建立会话 Session session = Session.getInstance(p); // 建立信息 Message msg = new MimeMessage(session); BodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); // 发件人 msg.setFrom(new InternetAddress(USER_NAME)); // 发送, if (to != null) { InternetAddress[] iaToList = InternetAddress.parse(to); // 收件人 msg.setRecipients(Message.RecipientType.TO, iaToList); } // 抄送 if (cs != null) { InternetAddress[] iaToListcs = InternetAddress.parse(cs); // 抄送人 msg.setRecipients(Message.RecipientType.CC, iaToListcs); } // 密送 if (ms != null) { InternetAddress[] iaToListms = InternetAddress.parse(ms); // 密送人 msg.setRecipients(Message.RecipientType.BCC, iaToListms); } // 发送日期 msg.setSentDate(new Date()); // 主题 msg.setSubject(subject); // 内容 msg.setText(content); // 显示以html格式的文本内容 messageBodyPart.setContent(content, "text/html;charset=gbk"); multipart.addBodyPart(messageBodyPart); // 2.保存多个附件 if (fileList != null) { addTach(fileList, multipart); } msg.setContent(multipart); // 邮件服务器进行验证 Transport tran = session.getTransport("smtp"); tran.connect(EMAIL_HOST, USER_NAME, USER_PASS); // 发送 tran.sendMessage(msg, msg.getAllRecipients()); logger.info("邮件发送成功,发送客户为:{}", to); /*} catch (Exception e) { logger.error("给客户 {} 发送邮件失败,异常信息为:{}", to, e.getMessage()); }*/ } /** * 添加多个附件 * * @param fileList 附件列表 * @param multipart 格式 * @throws MessagingException * @throws UnsupportedEncodingException */ public void addTach(String fileList[], Multipart multipart) throws MessagingException, UnsupportedEncodingException { for (int index = 0; index < fileList.length; index++) { MimeBodyPart mailArchieve = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileList[index]); mailArchieve.setDataHandler(new DataHandler(fds)); mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(), "GBK", "B")); multipart.addBodyPart(mailArchieve); } } public void sendMailOperation(String filePath) throws UnsupportedEncodingException, MessagingException { SendMailUtil send = SendMailUtil.getInstance(); String receiver = "XXXX@qingclass.com"; String cs = "XXXXX@163.com"; String ms = null; SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日"); String subject = " 课程数据表-" + sdf.format(new Date()); String content = "详见附件"; String fileList[] = {filePath}; send.send(receiver, cs, ms, subject, content, fileList); } }
发邮件
最新推荐文章于 2019-02-24 09:20:26 发布