spring-mail
1.导jar包
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-context-support</artifactId> 4 <version>4.3.25.RELEASE</version> 5 </dependency> 6 <!-- https://mvnrepository.com/artifact/javax.mail/mail --> 7 <dependency> 8 <groupId>javax.mail</groupId> 9 <artifactId>mail</artifactId> 10 <version>1.4.7</version> 11 </dependency>
2.配置application-context.xml
1 <bean id="main" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 2 <property name="host" value="smtp.qq.com"></property> 3 <property name="defaultEncoding" value="UTF-8"></property> 4 <property name="username" value="947985629@qq.com"></property> 5 <property name="password" value="jntbpvyqqpopbfeb"></property> 6 <property name="javaMailProperties"> 7 <props> 8 <prop key="mail.smtp.auth">true</prop> 9 <prop key="mail.smtp.timeout">2000</prop> 10 <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> 11 <prop key="mail.smtp.socketFactory.port">465</prop> 12 </props> 13 </property> 14 </bean>
3.发送邮件
1 package com.blb; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.mail.SimpleMailMessage; 7 import org.springframework.mail.javamail.JavaMailSenderImpl; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 @RunWith(SpringJUnit4ClassRunner.class) 12 @ContextConfiguration(locations = "classpath*:*.xml") 13 public class AppTest 14 { 15 @Autowired 16 private JavaMailSenderImpl javaMailSender; 17 18 @Test 19 public void sendmail() 20 { 21 SimpleMailMessage mailMessage = new SimpleMailMessage(); 22 mailMessage.setTo("1925195556@qq.com"); //此处可为String的一个数组 存的都是收件人账号 23 mailMessage.setFrom("947985629@qq.com"); 24 mailMessage.setSubject("Hi,bro"); 25 mailMessage.setText("过来面试"); 26 javaMailSender.send(mailMessage); 27 } 28 }