This is a test case for simple mail delivery through the features built into the spring framework.
- Import the associated jar package.
The main package for the Spring message abstraction layer is org.springframework.mail.
It includes the primary interface for sending e-mail mailsender, and the value object simplemailmessage, which encapsulates the properties of a simple message.
such as FROM,TO,CC, Subject,text.
2. Open the mail delivery service in the mailbox settings:
3. Create a mail.properties file in the SRC directory containing the contents
mail.host=smtp.exmail.qq.com (send domain settings for mailboxes)
Mail.username= Your mailbox name
Mail.password= your e-mail password
Mail.from= Sender's mailbox
4. Spring Configuration
1 <!--Load Properties File -2 <BeanID= "Configurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">3 < Propertyname= "Locations">4 <List>5 <value>Classpath:mail.properties</value>6 </List>7 </ Property>8 </Bean>9 <BeanID= "MailMessage"class= "Org.springframework.mail.SimpleMailMessage">Ten < Propertyname= "from"> One <value>${mail.from}</value> A </ Property> - <!--View Simplemailmessage Source code can also be injected into the title, content, etc. - - </Bean> the <!--affirm Javamailsenderimpl Object - - <BeanID= "MailSender"class= "Org.springframework.mail.javamail.JavaMailSenderImpl"> - < Propertyname= "Defaultencoding"value= "UTF-8" /> - < Propertyname= "Host"value= "${mail.host}" /> + < Propertyname= "username"value= "${mail.username}" /> - < Propertyname= "Password"value= "${mail.password}" /> + < Propertyname= "Javamailproperties"> A <Props> at <!--Setting the authentication switch - - <propKey= "Mail.smtp.auth">True</prop> - <!--start the debug switch - - <propKey= "Mail.debug">True</prop> - <!--Set Send delay - - <propKey= "Mail.smtp.timeout">0</prop> in </Props> - </ Property> to </Bean> + </Beans>
5. Add a simple send method to the action
1 /**2 * This type of test message is sent in HTML form3 */4 Public classSinglemailsend {5 StaticApplicationContext Actx =6 NewClasspathxmlapplicationcontext ("Applicationcontext.xml");7 StaticMailSender sender = (mailsender) actx.getbean ("MailSender");8 StaticSimplemailmessage MailMessage =9(simplemailmessage) Actx.getbean ("MailMessage"));Ten Public Static voidMain (String args[])throwsmessagingexception { OneMailmessage.setsubject ("Hello"); AMailmessage.settext ("This is a small program that sends mail through the spring framework"); -Mailmessage.setto ("****** @qq. com"); - sender.send (mailmessage); the } -}
About sending simple messages using Spring