- First of all to ensure that the user name password is correct, and mailbox support SMTP service, special attention is QQ mailbox third-party login need to use authorization code as password instead of QQ password, authorization code in the Mailbox Settings > account can be obtained
- The SMTP service port number needs to be SLL encrypted port number 465 to link the mailbox, in the Spring-mail configuration file requires the following configuration
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/con Text/spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/s Pring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3 .0.xsd ">
<!--Configure Javamailsend - <BeanID= "Javamailsend"class= "Org.springframework.mail.javamail.JavaMailSenderImpl"> < Propertyname= "Host"value= "${mail.host}"></ Property> < Propertyname= "Port"value= "${mail.port}"></ Property> < Propertyname= "username"value= "${mail.username}"></ Property> < Propertyname= "Password"value= "${mail.password}"></ Property> < Propertyname= "Javamailproperties"> <Props> <propKey= "Mail.smtp.auth">True</prop> <!--to connect in an encrypted manner, you need to add the following line configuration - <propKey= "Mail.smtp.socketFactory.class">Javax.net.ssl.SSLSocketFactory</prop> <propKey= "Mail.smtp.timeout">25000</prop> </Props> </ Property> </Bean>
<!--Configure Freemark - <BeanID= "Freemarker"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> < Propertyname= "Templateloaderpath"value= "web-inf/template/"></ Property> < Propertyname= "Freemarkersettings"><!--setting Freemarker Environment Properties - <Props> <propKey= "Template_update_delay">1800</prop><!--Refresh the period of the template in seconds - <propKey= "Default_encoding">UTF-8</prop><!--encoding format for templates - <propKey= "Locale">Zh_cn</prop><!--Localization Settings - </Props> </ Property> </Bean> <BeanID= "Mailtemplate"class= "Com.fcq.util.MailTemplate"> < Propertyname= "Freemarker"ref= "Freemarker"></ Property> < Propertyname= "Javamailsend"ref= "Javamailsend"></ Property> </Bean> </Beans>
3. General build template, send mail tool classMailtemplate.java PackageCom.fcq.util;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.mail.MessagingException;ImportJavax.mail.internet.MimeMessage;Importorg.springframework.mail.MailException;ImportOrg.springframework.mail.MailMessage;ImportOrg.springframework.mail.javamail.JavaMailSender;ImportOrg.springframework.mail.javamail.MimeMailMessage;ImportOrg.springframework.mail.javamail.MimeMessageHelper;Importorg.springframework.ui.freemarker.FreeMarkerTemplateUtils;ImportOrg.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;Importfreemarker.template.Template;Importfreemarker.template.TemplateException; Public classMailtemplate {PrivateJavamailsender Javamailsend; PrivateFreemarkerconfigurer Freemarker; PublicJavamailsender getjavamailsend () {returnJavamailsend; } Public voidsetjavamailsend (Javamailsender javamailsend) { This. Javamailsend =Javamailsend; } Publicfreemarkerconfigurer Getfreemarker () {returnFreemarker; } Public voidSetfreemarker (Freemarkerconfigurer freemarker) { This. Freemarker =Freemarker; } //to convert a freemarker template to a string-type message template Publicstring Getmailtemplate (string name) {string Mailtemplate= ""; Date Date=NewDate (); SimpleDateFormat Sformat=NewSimpleDateFormat ("Yyyy-mm-dd"); String Time=Sformat.format (date); Map<String,String> map =NewHashmap<string,string>(); Try{Template Template= Freemarker.getconfiguration (). GetTemplate ("MAILTEMPLATE.FTL"); Map.put ("Username", name); Map.put ("CurrentTime", time); Mailtemplate=freemarkertemplateutils.processtemplateintostring (TEMPLATE,MAP); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(templateexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnmailtemplate; } Public BooleanSendMail (String mail) {Boolean Sendflag=false; Try{mimemessage message=Javamailsend.createmimemessage (); Mimemessagehelper Helper=NewMimemessagehelper (Message,true, "UTF-8"); Helper.setsubject ("Register-Template Mail"); Helper.setfrom ("* * * * * @qq. com"); Helper.setto (mail); Helper.settext (getmailtemplate (mail)); Javamailsend.send (message); Sendflag=true; } Catch(mailexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(messagingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnSendflag; } }
Spring Send message verification does not pass the issue