Web project when sending mail using the mail Hutool Kit tools, the latest mail configuration changes do not take effect

Preface:

Hutool is a very good tool kit, which provides a number of excellent tools; use this project to its mailing tools , but when used also encountered some problems;

Problem Description: 

       First, this project is a web project, there is a mail profile page, which configure some parameters sent messages, such as: the sender's mailbox, password, and other recipient's mailbox, and then stored in the database;
       later when configuring the message to be modified, It will trigger a database program to re-read mail configuration, and then use the new e-mail configuration parameters when sending mail and then send the message. However, this time the problem occurred, is not using the new configuration parameters when sending the message again; for example: the beginning of a recipient's mailbox only, but I added a recipient's mailbox on the page, but once again when sending a message, the recipient does not receive new mail.

[Read this description of the problem to see if the same as if you're having problems, you can not the same across the]

Solve the problem: 

1, I first encountered this problem is to question your own code if there are problems, if they do not have to re-read the configuration program after modifying the configuration parameters of the message, but found himself through debug code without problems.


2, and then look to see the source code for the tools found the problem, the tools have parameters "isUseGlobalSession" global whether the session, the tool class code "send ()" mail transmission process "isUseGlobalSession = true "default is used, resulting in even reread the configuration parameters of the mail before the modified configuration is used.


3, the solution of the problem to find the time and then send the message; the new one "SendMailUtil" Tools, and then inherit "MailUtil" class and then override the "send ()" method, which does not use the global session default send method: calling this method overrides the send.

Code:

public class SendMailUtil extends MailUtil{

	/**
	 *  重写send方法让其不使用全局会话
	 *
	 *  setUseGlobalSession(false);设置是否使用全局会话为 false
	 */
	public static void send(MailAccount mailAccount, Collection<String> tos, String subject, String content, boolean isHtml, File... files) {
		Mail.create(mailAccount).setTos((String[])tos.toArray(new String[tos.size()])).setTitle(subject).setContent(content).setHtml(isHtml).setUseGlobalSession(false).setFiles(files).send();
	}

}

Do not forget to leave your footprints learning [thumbs + collection + Comment] Heyヾ 

Like all see the article does not point are "bullying" Hey ヾ (◍ ° ∇ ° ◍) Techno ゙! Joke, and move your hands, thumbs on the bin, you force everyone out of a (+ thumbs comments) will allow more learners to join in! thank you very much! ¯ω¯ =

 

Published 21 original articles · won praise 31 · views 10000 +

Guess you like

Origin blog.csdn.net/feichitianxia/article/details/104071943