Spring implements JavaMail mail sending function

Spring has a mechanism for managing Javamail sending. It is responsible for managing JavaMail sending under the org.springframework.mail package. I encapsulate the classes under this package into a Jar file for use as a jar package resource (download if necessary) can be downloaded from my resources), where org.springframework.mail.javamail.JavaMailSenderImpl is the class we need to use to send emails. The configuration in Spring is as follows:

 

[html]  view plain copy
 
  1. <beanid="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl">    
  2.         <propertyname="username"><value>${mail.smtp.username}</value></property>   
  3.         <propertyname="password"><value>${mail.smtp.password}</value></property>   
  4.         <propertyname="host"><value>${mail.smtp.host}</value></property>   
  5.         <propertyname="javaMailProperties">   
  6.             <props>  
  7.                 <propkey="mail.smtp.auth">${mail.smtp.auth}</prop>   
  8.                 <propkey="mail.smtp.timeout">${mail.smtp.timeout}</prop>   
  9.             </props>  
  10.         </property>  
  11. </bean>  

Various properties: username mail sending username, password mail sending password, host mail sending server, other properties of JavaMailProperties,

 

Whether mail.smtp.auth needs to pass password authentication, mail.smtp.timeout mail sending delay time.

After that, you need to set up the content of the mail sending. The mail sending is divided into ordinary mail format and hypertext mail format. The following is the format code for sending hypertext mail:

 

[java]  view plain copy
 
  1. MimeMessage mime = mailSender.createMimeMessage();  
  2. MimeMessageHelper mimehelper = new MimeMessageHelper(mime, true);  
  3. mimehelper.setFrom(mail.getFrom()); //Set the sender  
  4. mimehelper.setTo(mail.getTo()); //Set recipient  
  5. if (mail.getCc() != null && !"".equals(mail.getCc()))  
  6.     mimehelper.setCc(mail.getCc()); //Set the CC person  
  7. if (mail.getAcc() != null && !"".equals(mail.getAcc()))  
  8.     mimehelper.setBcc(mail.getAcc()); //Set the secret delivery  
  9. mimehelper.setSentDate( new  Date()); //Set the send date  
  10. mimehelper.setSubject(mail.getSubject()); //Set the subject  
  11. mimehelper.setText(mail.getContent(), true);//设置邮件内容为HTML超文本格式  
  12. FileSystemResource fsr = new FileSystemResource(attement.get(i));//设置附件内容  
  13. mimehelper.addInline(attementFileName.get(i), fsr);//添加附件  
  14. mailSender.send(mime);//将邮件发送  

设置附件发送类型不能为exe文件,一般是判断文件类型和文件后缀名。代码如下:

 

[java]  view plain copy
 
  1. for (int i = 0; attement != null && i < attementContentType.size(); i++) {  
  2.     if ("application/octet-stream".equals(attementContentType.get(i))  
  3.             && attementFileName.get(i).endsWith(".exe")) {  
  4.         this.addFieldError("attement", attementFileName.get(i)  
  5.                 +  "It is an executable file. For security reasons, this file is not allowed to be added." );  
  6.     }  
  7. }  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327066746&siteId=291194637