This time I only talked about the email sending function, but I was afraid of the lack of content, so I added some file compression function explanations.
First, email is sent. The email function has corresponding dependencies in springboot. This:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
The development of email function is quite simple in springboot. Here I will summarize the development content:
A>Add dependency package
B>Configure the basic parameters of Mail (in ymal or property)
C>Inject JavaMailSender into Service and call relevant methods
However, there may be a problem here, that is, when the specific server is deployed, the server will block the email service port and ordinary email security issues. I will give a solution when explaining here.
First, you need to introduce the email component in the project's pom.xml. The component version needs to correspond to the springboot version (you can not write it, I omit it here):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
Next is to configure the basic parameters of email in the configuration file:
spring: mail: host: smtp.exmail.qq.com username: [email protected] password: Password default-encoding: UTF-8 ssl: trust: smtp.exmail.qq.com properties: mail: smtp: auth: true #Does authentication need socketFactory: class: javax.net.ssl.SSLSocketFactory #SSL certificate Socket factory port: 465 #Use SMTP465 port
When configuring parameters, you must pay attention to indentation, because I give yaml configuration format. If it is a property configuration, it is roughly like this (example): spring.mail.host:smtp.exmail.qq.com. Each child is in a complete format. At the beginning, I omitted the configuration below the properties item (serious, SSL, port). Later, I found that the server blocked the 25th port of the mail, so it is possible locally but it doesn't work on the server, so it needs to specify the mail service port to be 465. I am using a qq mailbox here. If using 163 or other mailboxes, you need to check the ports supported by the service provider by yourself. As for email security issues, two need to be declared here, one is SSL trust and the socket factory of mail. For details, please see the above red section. The above configuration is only valid for qq mailboxes, and it does not guarantee that other mailboxes are also applicable.
OK, the configuration is completed, here we start writing the specific implementation class:
import XXX.common.util.DateUtil;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;import javax.mail.internet.MimeMessage;import java.util.Date;import java.util.List;import java.util.Map;@Servicepublic class MailService { private static final Logger LOG = LoggerFactory.getLogger(MailService.class); @Value("${spring.mail.username}") private String SEND_USER_ADDR; @Autowired private JavaMailSender mailSender; /** * Send simple email* @param receive Recipient* @param obj Send topic* @param content Email content*/ public void sendSimpleMail(String receive,String obj,String content) { if(!StringUtils.isNotBlank(content) || !StringUtils.isNotBlank(receive)) return;//Don't send empty mail SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(SEND_USER_ADDR); if(receive.contains(";")) message.setTo(receive.split(";")); else message.setTo(receive); message.setSubject(obj); message.setText(content); try { mailSender.send(message); LOG.info("Simple mail send success!"); } catch (Exception e) { LOG.error("sendSimpleMail ERROR!", e); } } private StringBuilder strBuilder; /** * The form of sending html mail multi-list list* @param receive Recipient* @param obj Send topic(topic) * @param content Email content*/ public void sendHtmlMailByList(String receive,String obj,List<Map> content){ if(content.isEmpty() || !StringUtils.isNotBlank(receive) || null==obj) return; MimeMessage msg = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8"); //Solve the garbled problem helper.setFrom(SEND_USER_ADDR); if(receive.contains(";")) helper.setTo(receive.split(";")); else helper.setTo(receive); helper.setSubject(obj); strBuilder=new StringBuilder(); strBuilder.append("<!DOCTYPE html><html><head><meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/"></head><body style=/"padding:3% 2%;/">"); strBuilder.append("<h2>This message is automatically sent to the system.</h2>"); strBuilder.append("<h2>Send Date by "+DateUtil.getDateFormat(new Date(),DateUtil.DATETIME_DEFAULT_FORMAT) +"</h2>"); strBuilder.append("<h2>The following is the details:</h2>"); strBuilder.append("<table border=/"2px solid red/" width=/"100%/">"); //Head strBuilder.append("<th>"+st[i]+"</th>"); strBuilder.append("</tr>"); strBuilder.append("</tr>"); Object[] st=content.get(0).keySet().toArray(); for(int i=0;i<st.length;i++) strBuilder.append("<th>"+st[i]+"</th>"); strBuilder.append("</tr>"); strBuilder.append("</tr>"); strBuilder.append("</tr>"); //body strBuilder.append("<tbody>"); for(Map item:content){ strBuilder.append("<tr>"); for(Object str:st) strBuilder.append("<td>"+item.get(str)+"</td>"); strBuilder.append("</tr>"); } strBuilder.append("</tbody>"); strBuilder.append("</table>"); strBuilder.append("</table>"); strBuilder.append("<h3 style=/"text-align:right/">Best wishes</h3>"); strBuilder.append("</body></html>"); //LOG.info(strBuilder.toString()); helper.setText(strBuilder.toString(),true); }catch (Exception e){ LOG.error("sendHtmlMail ERROR:",e); } mailSender.send(msg); } /** * Send html mail single column record form* @param receive Recipient* @param obj Send topic(title) * @param content Email content*/ public void sendHtmlMailByItem(String receive,String obj,List<String> content){ if(content.isEmpty() || !StringUtils.isNotBlank(receive) || null==obj) return; MimeMessage msg = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8"); //Solve the garbled problem helper.setFrom(SEND_USER_ADDR); if(receive.contains(";")) helper.setTo(receive.split(";")); else helper.setTo(receive); helper.setSubject(obj); strBuilder=new StringBuilder(); strBuilder.append("<!DOCTYPE html><html><head><meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/"></head><body style=/"padding:3% 2%;/">"); strBuilder.append("<h3>This message is automatically sent to the system.</h3>"); strBuilder.append("<h3>Send Date by "+DateUtil.getDateFormat(new Date(),DateUtil.DATETIME_DEFAULT_FORMAT) +"</h3>"); strBuilder.append("<h3>The following is the details:</h3>"); strBuilder.append("<table border=/"2px solid red/" width=/"100%/">"); //Head strBuilder.append("<table border=/"2px solid red/" width=/"100%/">"); //Head strBuilder.append("<th>"+obj.toUpperCase()+" DETAIL</th>"); strBuilder.append("<th>"+obj.toUpperCase()+" DETAIL</th>"); strBuilder.append("</table>"); //body strBuilder.append("<tbody>"); for(String item:content){ strBuilder.append("<tr><td>"+item+"</td></tr>"); } strBuilder.append("</tbody>"); strBuilder.append("</table>"); strBuilder.append("<h3 style=/"text-align:right;font-weight:normal;/">Best wishes</h3>"); strBuilder.append("</body></html>"); strBuilder.append("</body></html>"); strBuilder.append("</body></html>"); strBuilder.append("</body></html>"); LOG.info(strBuilder.toString()); helper.setText(strBuilder.toString(),true); }catch (Exception e){ LOG.error("sendHtmlMail ERROR:",e); } mailSender.send(msg); }}
The above is to encapsulate the email function into a service class. When using it, you only need to inject the current class and call it directly. The above encapsulates two methods: one is simple email sending, and the other is an email with an html table. If you need to send attachments, you need to put the attachments into MimeMessageHelper (called addAttachment("file name", file)) method. Because there is no actual requirement here, it is omitted. Okay, the email sending function has been completed. Let's see the actual effect here:
The email function has been implemented. Now I will talk about the file compression function. There are roughly four types of compression function implementations, namely:
A>Use the API compression provided by java.util.zip
B>Use api compression provided by apache's ant package (org.apache.tools.ant.taskdefs.Zip)
C>Use api compression provided by zip4j (net.lingala.zip4j)
D>Call the shell command compression of the host
There are three issues that need to be mentioned here:
A>Ordinary email compressed Chinese garbled code (not supported in Chinese)
B>Cannot decompress after compression (decompression error)
C>Problem of adding compressed passwords for file compression
The compression function has actually been developed. The above three points are particularly troublesome for novices. Here I will share the problems I encountered in developing compression function before.
Using the compression provided by the native java.util package, if the compressed file is used in Chinese, it will be garbled (it is said to be a bug in jdk), and the code implemented by the compression is relatively complex (especially setting passwords), especially for cross-directory compression and multi-file compression.
Although using the zip tool provided by apache avoids the above problems, it should be noted that this ant package conflicts with webLogic (an error will be reported during deployment) and cannot compress the password. If you are using webLogic instead of tomocat, you must pay attention to this problem.
Using Java to call the host's shell command is also a good choice, but it requires writing shell commands. It is not very friendly to deploy on the Windows platform, and porting is more troublesome.
Finally, for the above issues, I recommend zip4j here. The following is also an explanation of the compression implementation of zip4j.
First, you need to introduce dependency packages:
<!--Compression: Support for encryption compression--> <dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>1.3.2</version> </dependency>
Furthermore, encapsulate a compression/decompression tool class for easy use:
import net.lingala.zip4j.core.ZipFile;import net.lingala.zip4j.exception.ZipException;import net.lingala.zip4j.model.ZipParameters;import net.lingala.zip4j.util.Zip4jConstants;import org.springframework.util.StringUtils;import java.io.File;/** * This tool class uses Zip4j for compression and decompression*/public class ZipUtil { //Declare the compressed object private static ZipParameters parameters; //Decompress file object private static ZipFile zipFile; /** * * @param sourceFilePath The path of the compressed file (single file, folder) * @param zipFilePath Compressed file path* @param password Compressed password* @return Compressed successfully: true, compression failed: false */ public static Boolean singleFileCompress(String sourceFilePath,String zipFilePath,String password){ parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // Compression method (default method) parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // Compression level (default level) // Compression encryption settings if (!StringUtils.isEmpty(password)) { parameters.setEncryptFiles(true); // Whether to set file encryption (default is No) parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // Encryption method (here is standard compression) parameters.setPassword(password.toCharArray()); } try { ZipFile zipFile = new ZipFile(zipFilePath); //If it is a file, compress it directly. If it is a folder, traverse the files and compress them all if(new File(sourceFilePath).isFile()) { zipFile.setFileNameCharset("GBK"); zipFile.addFile(new File(sourceFilePath), parameters); return true; } //File ff=new File(sourceFilePath); File[] flst=new File(sourceFilePath).listFiles(); System.out.println("Number of files =>"+flst.length); for(File f:flst){ zipFile.setFileNameCharset("GBK"); zipFile.addFile(f, parameters); } return true; } catch (ZipException e) { e.printStackTrace(); return false; } catch (Exception id){ id.printStackTrace(); return false; } } public static Boolean unZip(String zipFile,String unZipDir){ try { ZipUtil.zipFile = new ZipFile(zipFile); ZipUtil.zipFile.setFileNameCharset("GBK");//Set the encoding format//Use the built-in method to check whether the zip file is legal, including whether the file exists, whether it is a zip file, whether it is a zip file, whether it is a corruption, etc. If (!ZipUtil.zipFile.isValidZipFile()) { throw new ZipException("The file is illegal or does not exist"); } // Compared with Java's own built-in, the file path will be automatically generated without judging ZipUtil.zipFile.extractAll(unZipDir); return true; }catch(ZipException e){ return false; } }} The above compression method comes with password compression function, which can compress single files or directory files. Compared with the native implementation, it is much refreshing all of a sudden. The only thing to be noted here is that the compressed target file must not be penetrated before compression, otherwise an error will be reported! In addition, when decompressing, you must pay attention to file encoding and determine whether the file exists.
Summarize
The above is the springboot implementation of adding email sending and compression functions that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!