package com.java.mail; import java.io.File; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Message; import javax.mail.Multipart; 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; public class SimpleMail { public static boolean sendTextMail(MailInfo mailInfo) { MyAuthenticator authenticator = null; Properties properties = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUsername(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(properties, authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(mailInfo.getContent(),"text/html; charset=utf-8"); mainPart.addBodyPart(messageBodyPart); String[] filePaths = mailInfo.getAttachFileNames(); if (filePaths != null && filePaths.length > 0) { for(String filePath:filePaths){ messageBodyPart = new MimeBodyPart(); File file = new File(filePath); if(file.exists()){ FileDataSource fds = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setFileName(file.getName()); mainPart.addBodyPart(messageBodyPart); } } } mailMessage.setContent(mainPart); Transport.send(mailMessage); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public static boolean sendHtmlMail(MailInfo mailInfo) { MyAuthenticator authenticator = null; Properties properties = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUsername(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(properties, authenticator); try{ Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(mailInfo.getContent(),"text/html; charset=utf-8"); mainPart.addBodyPart(messageBodyPart); String[] filePaths = mailInfo.getAttachFileNames(); if (filePaths != null && filePaths.length > 0) { for(String filePath:filePaths){ messageBodyPart = new MimeBodyPart(); File file = new File(filePath); if(file.exists()){ FileDataSource fds = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setFileName(file.getName()); mainPart.addBodyPart(messageBodyPart); } } } mailMessage.setContent(mainPart); Transport.send(mailMessage); return true; }catch (Exception e) { e.printStackTrace(); } return false; } }

1358849392 LV21
2022年12月27日
moomin709 LV24
2022年7月6日
瓜不皮 LV15
2020年12月9日
kayok LV19
2019年4月17日
2469684164 LV10
2019年3月16日
落雪飞花 LV6
2018年12月7日
Joyyoe LV4
2018年10月9日
nanjing2017 LV15
2017年9月8日
hkp0521 LV7
2017年8月23日
cas LV9
2017年5月27日