SpringBoot整合JavaMail
简单使用-发送简单邮件
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- mail< / artifactId>
< / dependency>
spring:
mail:
host: smtp. qq. com
username:
password: mrqzoenvhtwledfb
@Override
public void sendMail ( ) {
SimpleMailMessage message = new SimpleMailMessage ( ) ;
message. setFrom ( from) ;
message. setTo ( to ) ;
message. setSubject ( subject) ;
message. setText ( context) ;
javaMailSender. send ( message) ;
}
package com. ustc ;
import com. ustc. service. SendService ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
@SpringBootTest
class Quick3ApplicationTests {
@Autowired
private SendService sendservice;
@Test
void contextLoads ( ) {
sendservice. sendMail ( ) ;
}
}
JavaMail发送多部件邮件
@Override
public void sendMail ( ) throws MessagingException {
MimeMessage m = javaMailSender. createMimeMessage ( ) ;
MimeMessageHelper message = new MimeMessageHelper ( m) ;
message. setFrom ( from) ;
message. setTo ( to ) ;
message. setSubject ( subject) ;
message. setText ( context) ;
File f1 = new File ( "D:\\1.txt" ) ;
message. addAttachment ( f1. getName ( ) , f1) ;
javaMailSender. send ( message) ;
}