Java邮件发送解决ssl javax.mail实现方式
package test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.mail.*; import javax.mail.internet.*; import javax.mail.util.ByteArrayDataSource; import java.io.ByteArrayInputStream; import java.util.Properties; /** * Java邮件发送解决ssl javax.mail实现方式 * Caused by: javax.mail.AuthenticationFailedException: 535 5.7.0 ERR.LOGIN.NONSSL 需要add 解决ssl发送邮件 属性的配置 */ public class MultiEmailTest { private static Logger logger = LoggerFactory.getLogger(MultiEmailV3.class); /** * 系统属性 */ private static Properties props = System.getProperties(); /** * 邮件会话对象 */ private static Session session; /** * MIME邮件对象 */ private static MimeMessage mimeMsg; private static Multipart mp = null; private static final String HOST_NAME = "smtp.qiye.163.com"; //邮件服务器 可配 private static final String FROM_ADDR = "xxx@xx.com"; //邮件发送者地址 可配 private static final String FROM_NAME = "xxx@xx.com"; //邮件发送者名称 可配 private static final String FROM_PWD = "xx"; //邮件发送者密码 可配 private static final String SMTP = "smtp"; static{ //add 解决ssl发送邮件 props.put("mail.smtp.starttls.enable",true); } /** * 发送邮件 * @param tos 收件人 * @param copyTos 抄送人 * @param subject 主题(标题) * @param content 正文(内容) * @throws Exception 异常 */ public static void sendMail(String[] tos, String[] copyTos, String subject, String content) throws Exception { // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 BodyPart bp = setBodyPart(tos, subject, content); send(copyTos, bp); } /** * 发送邮件(带附件) * @param tos 收件人 * @param copyTos 抄送人 * @param subject 主题(标题) * @param content 正文(内容) * @throws Exception 异常 */ public static void sendMailAttachment(String[] tos, String[] copyTos, String subject, String content, ByteArrayInputStream stream, String fileName, String type) throws Exception { // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 BodyPart bp = setBodyPart(tos, subject, content); // 邮件内容部分2---附件1 MimeBodyPart body1 = new MimeBodyPart(); DataSource dataSource1 = new ByteArrayDataSource(stream, type); // 代表项目根目录下 body1.setDataHandler(new DataHandler(dataSource1)); // 中文附件名,解决乱码 body1.setFileName(MimeUtility.encodeText(fileName)); mp.addBodyPart(bp); send(copyTos, body1); } /** * 构建body * @param tos * @param subject * @param content * @return * @throws MessagingException */ private static BodyPart setBodyPart(String[] tos, String subject, String content) throws MessagingException { mp = new MimeMultipart(); mimeMsg = new MimeMessage(session); //add 解决ssl发送邮件 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.port", 465); props.put("mail.smtp.port", 465); session = Session.getDefaultInstance(props, null); session.setDebug(false); // 设置发信人 mimeMsg.setFrom(new InternetAddress(FROM_ADDR)); // 设置接收人 InternetAddress[] sendTo = new InternetAddress[tos.length]; for (int i = 0; i < tos.length; i++) { sendTo[i] = new InternetAddress(tos[i]); } mimeMsg.setRecipients(Message.RecipientType.TO, sendTo); // 设置主题 mimeMsg.setSubject(subject); // 设置正文 BodyPart bp = new MimeBodyPart(); bp.setContent(content, "text/html;charset=utf-8"); return bp; } /** * body追加部分 * @param copyTos * @param bp * @throws MessagingException */ private static void send(String[] copyTos, BodyPart bp) throws MessagingException { mp.addBodyPart(bp); mimeMsg.setContent(mp); mimeMsg.saveChanges(); // 发送邮件 Transport transport = session.getTransport(SMTP); transport.connect(HOST_NAME, FROM_NAME, FROM_PWD); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); if (copyTos != null && copyTos.length > 0) { // 设置抄送人 for (int i = 0; i < copyTos.length; i++) { mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyTos[i])); } transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC)); } transport.close(); } //测试 public static void main(String[] args) throws Exception { String[] tos = {"xxx@xx.com"}; //接收者的邮箱地址 String errorMsg = "123\n223"; ByteArrayInputStream stream = new ByteArrayInputStream(errorMsg.getBytes()); //带附件的邮件 // sendMailAttachment(tos, null, "测试邮件-附件邮件含附件", "Dear ALL 2233:<br><br> 详见附件", stream, "文件.txt", "application/txt"); //普通邮件 sendMail(tos,null,"测试邮件-普通邮件不含附件","邮件内容这是一封测试邮件"); } }