1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package com.pay.utils;
import com.pay.utils.enums.MailType; import org.springframework.util.StringUtils;
import Javax.activation.*; import Javax.mail.*; import Javax.mail.internet.*; import Javax.mail.util.ByteArrayDataSource; import Java.io.*; import Java.util.HashMap; import Java.util.Map; import Java.util.Properties;
public class MailSender { private MailType mailType; private String userName; private String passWord; private Properties properties; public MailSender(MailType mailType, String userName, String passWord) { this.mailType = mailType; this.userName = userName; this.passWord = passWord; this.properties = getProperties(); } public boolean sender(String recivers, String cc, String mailTitle, String mailContent, boolean isHtml, Map<String, byte[]> mapFile) throws MessagingException, IOException { Session session = Session.getInstance(properties); Transport ts = session.getTransport(); ts.connect(properties.getProperty("mail.host"), userName, this.passWord); MimeMessage mm = new MimeMessage(session); mm.setFrom(new InternetAddress(userName)); Address[] address = new InternetAddress().parse(recivers); mm.setRecipients(Message.RecipientType.TO, address); if (!StringUtils.isEmpty(cc)) { mm.setRecipient(Message.RecipientType.CC, new InternetAddress(cc)); } mm.setSubject(mailTitle); if (!isHtml) { mailContent = String.format("<pre>%s</pre>", mailContent); } Multipart multipart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(mailContent, "text/html;charset=utf-8"); multipart.addBodyPart(bodyPart); if (mapFile != null) { MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mc.addMailcap("text/html;; x-Java-content-handler=com.sun.mail.handlers.text_html"); mc.addMailcap("text/xml;; x-Java-content-handler=com.sun.mail.handlers.text_xml"); mc.addMailcap("text/plain;; x-Java-content-handler=com.sun.mail.handlers.text_plain"); mc.addMailcap("multipart/*;; x-Java-content-handler=com.sun.mail.handlers.multipart_mixed"); mc.addMailcap("message/rfc822;; x-Java-content-handler=com.sun.mail.handlers.message_rfc822"); CommandMap.setDefaultCommandMap(mc); for (Map.Entry<String, byte[]> map : mapFile.entrySet()) { BodyPart messageBodyPart = new MimeBodyPart(); InputStream inputStream = new ByteArrayInputStream(map.getValue()); DataSource source = new ByteArrayDataSource(inputStream, "application/txt"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText(map.getKey())); multipart.addBodyPart(messageBodyPart); } mm.setContent(multipart); } ts.sendMessage(mm, mm.getAllRecipients()); return true; } private Properties getProperties() { if (this.mailType.equals(MailType.m163)) { Properties prop = new Properties(); prop.put("mail.host", "smtp.163.com"); prop.put("mail.transport.protocol", "smtp"); prop.put("mail.smtp.auth", true); return prop; } if (this.mailType.equals(MailType.qq)) { Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.qq.com"); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); prop.setProperty("mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); prop.setProperty("mail.smtp.port", "465"); prop.setProperty("mail.smtp.socketFactory.port", "465"); return prop; } return null; } }
```
``` Java public enum MailType { m163, qq }
|