import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; import java.util.Properties; /** * @description: * @date:2022/6/10 10:00 */ public class MailSenderUtils { private final static Logger log = LoggerFactory.getLogger(MailSenderUtils.class); public static void sendMail(MailServerConfiguration mailServerConfiguration, MailSendInfo mailSendInfo){ Message message = null; Session session = null; try { //定义邮箱服务器配置 Properties properties = new Properties(); properties.put("mail.transport.protocol",mailServerConfiguration.getProtocol()); properties.put("mail.smtp.ssl.enable",true); //邮件服务器地址 properties.put("mail.smtp.host",mailServerConfiguration.getServerHost()); //邮件服务器端口 properties.put("mail.smtp.port",mailServerConfiguration.getServerPort()); //邮件服务器认证属性 properties.put("mail.smtp.auth",mailServerConfiguration.getFlag()); //身份认证类 UserPwdMailAuthenticator authenticator = new UserPwdMailAuthenticator(mailServerConfiguration.getUserName(),mailServerConfiguration.getUserPwd()); //创建session session = Session.getDefaultInstance(properties,authenticator); //开启Session的debug模式 session.setDebug(true); //创建message邮件对象 message = new MimeMessage(session); Address from = new InternetAddress(mailServerConfiguration.getFromAddress()); //设置发送方的邮箱地址 message.setFrom(from); //设置发送时间 message.setSentDate(new Date()); ListtoAddresses = mailSendInfo.getToAddress(); if(CollectionUtils.isNotEmpty(toAddresses)){ Address[] addresses = new Address[toAddresses.size()]; for(int i = 0 ; i < toAddresses.size(); i++){ Address address1 = new InternetAddress(toAddresses.get(i)); addresses[i] = address1; } //设置接收方的邮件地址 message.setRecipients(Message.RecipientType.TO,addresses); //甚至邮件主题 message.setSubject(mailSendInfo.getSubject()); //创建多媒体对象容器 Multipart multipart = new MimeMultipart(); //创建正文内容 BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(mailSendInfo.getContent(),"text/html;charset=utf-8"); multipart.addBodyPart(bodyPart); //获取附件文件 List files = mailSendInfo.getFiles(); if(CollectionUtils.isNotEmpty(files)){ for(File file : files){ if(file.exists()){ // 如果附件存在,创建附件对象 BodyPart attachPart = new MimeBodyPart(); attachPart.setDataHandler(new DataHandler(new FileDataSource(file))); // 设置文件名 (解决附件名乱码) attachPart.setFileName(MimeUtility.encodeText(file.getName())); // 添加附件 (将附件内容添加到多媒体对象容器中) multipart.addBodyPart(attachPart); } } } message.setContent(multipart); Transport.send(message); } } catch (AddressException e) { log.error("邮件发送失败,错误原因:" + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error("邮件发送失败,错误原因:" + e.getMessage()); } catch (NoSuchProviderException e) { log.error("邮件发送失败,错误原因:" + e.getMessage()); } catch (MessagingException e) { log.error("邮件发送失败,错误原因:" + e.getMessage()); } } }
#邮件服务器 audaque.mail.host=smtp.qq.com #邮件服务器端口 audaque.mail.port=465 ###邮箱号 audaque.mail.username=XX.qq.com ###邮箱密码或邮箱授权码,qq邮箱发送短信获取16位数的授权码 audaque.mail.password=XX #传输协议 audaque.mail.protocol=stmp
QQ邮箱发送。也可以使用腾讯企业邮箱,因为是收费版,只需要密码不需要授权码,qq就需要授权码,其他类邮箱类似可以配置