About sending simple messages using Spring

Source: Internet
Author: User
Tags html form mailmessage

This is a test case for simple mail delivery through the features built into the spring framework.

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.