"Code" JAVAMAIL+SPRING+QQ mailbox Send mail

Source: Internet
Author: User

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

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.