1. Dependencies: The spring version changes according to the specific environment
<dependency> <groupId>javax.mail</groupId> <artifactid>mail</artifactid> <version>1.4</version> </Dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </Dependency> |
2. Configure Spring
Injecting mail objects with spring management
Note the Mail port uses 587, not the legendary 465, when sending mail using 465, the program is running for a long time, unable to send mail
<bean id=" MailSender "class=" Org.springframework.mail.javamail.JavaMailSenderImpl <!--inject the required elements--> <!--mail server use QQ mailbox--> <property name= "host" value= "smtp.qq.com"/> <!--sender e-mail address--> <property name= "username" value= "[email protected]"/> <!--send mail Port-- <property name= "Port" value= "587"/> <!--password use authorization code--> <property name= "Password" Value= "XXXXXXXX"/> <!----; <property name= "javamailproperties"; <props> <prop key= "Mail.smtp.host" >SMTP.QQ.COM</PROP> <prop key= "Mail.transport.protocol" >smtp</ Prop> <prop key= "Mail.smtp.auth" >TRUE</PROP> </props> </property> </ Bean> |
3. Write code to send mail
Inject Javamailsender object before sending mail @Autowired Private Javamailsenderimpl MailSender; |
Building a Simple Mail object Simplemailmessage SMM = new Simplemailmessage (); Set Message parameters Smm.setfrom (Mailsender.getusername ()); Set recipient Smm.setto ("Mail inbox @qq.com"); Message subject Smm.setsubject ("This is a simple e-mail"); Message content Smm.settext ("Hello World via Spring Mail Sender"); Send mail Mailsender.send (SMM); |
"Code" JAVAMAIL+SPRING+QQ mailbox Send mail