邮件发送配置--jdk
@Configuration
public class EmailConfig {
@Value("${email.host}")
private String host;
@value("${email.sender}")
private String sender;
@Value("$(emait.password}")
private String password;
@Bean
public Session getSession(){
Propertiesproperties= System.getProperties();
propenties.setProperty("mail.smtp.host",host);
properties.put("mail.smtp.auth","true");
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl,enable",“true");
properties.put("mai.smtp.ssl.socketFactory",sf);
}catch (GeneralSecurityException generalSecurityException){
generalSecurityException.printStackTrace();
}
Session session = Session.getDefaultInstance(properties, getPasswordAuthentication()->{
return new PasswordAuthentication(sender, password);
});
retupn session;
}
public static void sendMail(Session session, String subject,String text, bytel] bytes, String fileName,tring sender, String peceiver){
try {
MimeMessage message = new MimeMessage(session);
String addAsial=MimeUtility.encodeText(word:“省办管理平台");
InternetAddress internetAddress = new InternetAddress(addAsial+"<"+sender+">");
message.setFrom(internetAddress);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(receiver));
message.setSubject(subject);
BodyPant bodyPart=new MimeBodyPart();
text="<div>"+text+"</div>";
bodyPart.setContent(text, type:"text/html;charset=utf-8");Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
if (bytes != null){
try {
MimeBodyPartfileBodyPart=new MimeBodyPart();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
Datasource source = new ByteAmrayDataourcebyteAmraylmputstream, pe: "application/vnd.oenxmlformats-0fficedocument.spreadsheetml.template");
fileBodyPart.setDataHandler(new DataHandler(source));
BASE64Encoder base64Encoder=new BASE64Encoder();
fileBodyPart.setFileName("=?UTF-8?B?” + base64Encodepencode(fileName.getBytes()) +"?=");
multipart.addBodyPart(fileBodyPart);
}catch(Exception e){
}
message.setContent(multipart);
message.saveChanges();
Transport.send(message);
System.out.println("==========发送成功===========");
}catch((MessagingException | UnsupportedEncodingException messagingException){
messagingException.printStackTrace();
}
}
}


备注:仅供参考
浙公网安备 33010602011771号