1 importjava.io.File;2 importjava.io.UnsupportedEncodingException;3 importjava.util.Properties;4
5 importjavax.activation.DataHandler;6 importjavax.activation.FileDataSource;7 importjavax.mail.Authenticator;8 importjavax.mail.BodyPart;9 importjavax.mail.Message.RecipientType;10 importjavax.mail.MessagingException;11 importjavax.mail.Multipart;12 importjavax.mail.PasswordAuthentication;13 importjavax.mail.Session;14 importjavax.mail.Transport;15 importjavax.mail.internet.InternetAddress;16 importjavax.mail.internet.MimeBodyPart;17 importjavax.mail.internet.MimeMessage;18 importjavax.mail.internet.MimeMultipart;19 importjavax.mail.internet.MimeUtility;20
21 public classMailUtil {22
23 /**
24 * @Description: 使用QQ邮箱发送不带附件的邮件,收件人邮箱类型不限。25 * @date: 2019年12月17日 下午4:51:0126 *@author: ggwudivs27 *@paramsubject 邮件标题28 *@paramcontent 邮件内容29 *@paramfromUser 发件人邮箱30 *@paramfromPass 发件人邮箱密码,应为16位SMTP口令31 *@paramtO_Recipients 收件人邮箱,多个收件人用","分隔32 *@paramopenSSL 是否开启SSL33 *@throwsUnsupportedEncodingException34 *@throwsMessagingException:35 *@return: void36 */
37 public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, boolean openSSL) throwsUnsupportedEncodingException, MessagingException{38 sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", null, openSSL);39 }40
41 /**
42 * @Description: 使用QQ邮箱发送带附件的邮件,收件人邮箱类型不想。43 * @date: 2019年12月17日 下午5:25:1444 *@author: ggwudivs45 *@paramsubject 邮件标题46 *@paramcontent 邮件内容47 *@paramfromUser 发件人邮箱48 *@paramfromPass 发件人邮箱密码,应为16位SMTP口令49 *@paramtO_Recipients 收件人邮箱,多个收件人用","分隔50 *@paramattachmentFilesPath 邮件附件路径,多个附件用","分隔51 *@paramopenSSL 是否开启SSL52 *@throwsUnsupportedEncodingException53 *@throwsMessagingException:54 *@return: void55 */
56 public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, String attachmentFilesPath, boolean openSSL) throwsUnsupportedEncodingException, MessagingException{57 sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", attachmentFilesPath, openSSL);58 }59
60 /**
61 * @Description: smtp发送邮件62 * @date: 2019年12月17日 下午3:22:3563 *@author: ggwudivs64 *@paramsubject 邮件标题65 *@paramcontent 邮件文本内容66 *@paramfromUser 发件人邮箱67 *@paramfromPass 发件人邮箱密码,QQ邮箱应为16位SMTP口令68 *@paramnickname 发件人昵称69 *@paramtO_Recipients 收件人邮箱,多个收件人用","分隔70 *@paramcC_Recipients 抄送人邮箱,多个抄送人用","分隔71 *@parambCC_Recipients 密送人邮箱,多个密送人用","分隔72 *@paramsmtpHost smtp服务器地址73 *@paramsmtpPort smtp服务器端口74 *@paramattachmentFilesPath 邮件附件路径,多个附件用","分隔75 *@throwsMessagingException76 *@throwsUnsupportedEncodingException:77 *@return: void78 */
79 public static void sendMessage(String subject, String content, String fromUser, String fromPass, String nickname, String tO_Recipients, String cC_Recipients, String bCC_Recipients, String smtpHost, String smtpPort, String attachmentFilesPath, boolean openSSL) throwsMessagingException, UnsupportedEncodingException {80 //创建Properties类,用于记录邮箱的一些属性
81 Properties props = newProperties();82 //表示SMTP发送邮件,必须进行身份验证
83 props.put("mail.smtp.auth", "true");84 //SMTP服务器地址
85 props.put("mail.smtp.host", smtpHost);86 //是否开启SSL
87 if(openSSL){88 //SMTP服务器端口号
89 props.put("mail.smtp.port", smtpPort);90 }else{91 //SMTP服务器ssl端口号
92 props.setProperty("mail.smtp.socketFactory.port", smtpPort);93 props.setProperty("mail.smtp.socketFactory.fallback", "false");94 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");95 }96
97 //构建授权信息,用于进行SMTP进行身份验证
98 Authenticator authenticator = newAuthenticator() {99 protectedPasswordAuthentication getPasswordAuthentication() {100 //发件人账号,发件人密码(QQ邮箱应为16位SMTP口令)
101 return newPasswordAuthentication(fromUser, fromPass);102 }103 };104
105 //使用环境属性和授权信息,创建邮件会话
106 Session mailSession =Session.getInstance(props, authenticator);107
108 //创建邮件消息
109 MimeMessage message = newMimeMessage(mailSession);110
111 //设置发件人,有昵称时同时设置昵称
112 try{113 message.setFrom((nickname==null||"".equals(nickname))?new InternetAddress(fromUser):new InternetAddress(fromUser, nickname, "UTF-8"));114 } catch(UnsupportedEncodingException e) {115 e.printStackTrace();116 }117
118 //设置一个或多个收件人
119 message.setRecipients(RecipientType.TO, tO_Recipients);120
121 //设置一个或多个抄送人
122 message.setRecipients(RecipientType.CC, cC_Recipients);123
124 //设置一个或多个密送人
125 message.setRecipients(RecipientType.BCC, bCC_Recipients);126
127 //设置邮件标题
128 message.setSubject(subject);129
130 //设置邮件的内容
131 if(attachmentFilesPath == null || "".equals(attachmentFilesPath)){132 //设置邮件的正文文本
133 message.setContent(content, "text/html;charset=UTF-8");134 }else{135 //向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
136 Multipart multipart = newMimeMultipart();137
138 //添加邮件文本内容
139 BodyPart contentBodyPart = newMimeBodyPart();140 contentBodyPart.setContent(content, "text/html;charset=utf-8");141 multipart.addBodyPart(contentBodyPart);142
143 //添加邮件附件内容
144 BodyPart attachmentBodyPart = newMimeBodyPart();145 String[] attachmentFiles = attachmentFilesPath.split(",");146 for(String attachmentFile : attachmentFiles) {147 if (attachmentFile != null && !"".equals(attachmentFile)) {148 attachmentBodyPart = newMimeBodyPart();149
150 //根据附件路径获取文件,
151 FileDataSource dataSource = new FileDataSource(newFile(attachmentFile));152 attachmentBodyPart.setDataHandler(newDataHandler(dataSource));153
154 //MimeUtility.encodeWord可以避免文件名乱码
155 String strFileName=dataSource.getFile().getName();156 attachmentBodyPart.setFileName(MimeUtility.encodeText(strFileName));157
158 multipart.addBodyPart(attachmentBodyPart);159 }160 }161
162 //设置邮件的正文内容
163 message.setContent(multipart);164 }165
166 //发送邮件
167 Transport.send(message);168 }169 }