栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring boot email(1)

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot email(1)

Email protocols
  • SMTP: upload and transfer between mail servers.

  • POP3: pull emails to local.

  • IMAP: After receiving the email, the email is still reserved on the server. Mapping client and server.

  • Mime: SMTP was using ASCII. Mime extends to binary transfer.

Unit test

We need to add two annotations in the unit test class

@RunWith(SpringRunner.class)
@SpringBootTest
Spring-boot-starter-mail
  • Maven dependency

    org.springframework.boot
    spring-boot-starter-mail

  • property file
spring.mail.host=smtp.gmail.com
spring.mail.username=
spring.mail.password=
spring.mail.default-encoding=utf-8

#If you use Gmail, the follow two lines are necessary
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
Sending a simple text email
@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    public void sendSimpleMail(String to, String subject, String content) {
 SimpleMailMessage message = new SimpleMailMessage();
 message.setTo(to);
 message.setSubject(subject);
 message.setText(content);
 message.setFrom(from);

 mailSender.send(message);
    }
}

Test this service

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailServiceTest {

    @Resource
    MailService mailService;

    @Test
    public void sendSimpleMail() {
 mailService.sendSimpleMail("",
    "Test email",
    "Hello World");
    }
}
Sending a HTML email
public void sendHTMLMail(String to, String subject, String content) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    mailSender.send(message);
}

Test this service

@Test
public void sendHTMLMail() throws MessagingException {
    String content = "nn Hello world! This is a HTML email.nn";
    mailService.sendHTMLMail("", "HTML Email", content);
}
Sending an email with attachments
public void sendAttachmentMail(String to, String subject, String content, String filePath) throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);

    FileSystemResource file = new FileSystemResource(new File(filePath));
    String filename = file.getFilename();
    helper.addAttachment(filename, file);
    mailSender.send(message);
}
@Test
public void sendAttachmentMail() throws MessagingException {
    String filePath = "C:\Users\***";
    mailService.sendAttachmentMail("",
     "Test email",
     "Hello World", filePath);
}
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号