zoukankan      html  css  js  c++  java
  • springboot成神之——发送邮件

    本文介绍如何用spring发送邮件

    目录结构

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

    MailConfig

    package com.springlearn.learn.config;
    
    import java.util.Properties;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    @Configuration
    public class MailConfig {
    
        @Bean
        public JavaMailSender getjavamailsender() {
    
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost("smtp.qq.com");
            mailSender.setPort(587);
    
            mailSender.setUsername("你的qq邮箱");
            mailSender.setPassword("你的授权码");
    
            Properties props = mailSender.getJavaMailProperties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.from", "你的qq邮箱");
     
            return mailSender;
        }
    }
    

    TestController

    package com.springlearn.learn.controller;
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.mail.BodyPart;
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.servlet.http.HttpServletResponse;
    
    import com.springlearn.learn.DemoApplication;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class TestController {
    
        @Autowired
        public JavaMailSender emailSender;
    
        @ResponseBody
        @RequestMapping(value = "/sendmail", method = RequestMethod.GET)
        public String Test(HttpServletResponse response) throws IOException, MessagingException {
            // 发送简单短信
            // SimpleMailMessage message = new SimpleMailMessage();
            // message.setTo("756029960@qq.com");
            // message.setSubject("测试邮件");
            // message.setText("发送成功");
            // this.emailSender.send(message);
    
            
            // 发送附件
            // MimeMessage message = emailSender.createMimeMessage();
    
            // MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
    
            // helper.setTo("你要发送人的邮件");
            // helper.setSubject("测试邮件,有附件!");
    
            // helper.setText("给你发一份附件!", true);
    
            // System.out.println();
    
            // FileSystemResource file1 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo.txt"));
            // helper.addAttachment("Txt1 file", file1);
    
            // FileSystemResource file2 = new FileSystemResource(new File(DemoApplication.class.getResource("").getPath(), "demo1.txt"));
            // helper.addAttachment("Txt1 file", file2);
    
            // emailSender.send(message);
    
            return "Ok!";
        }
    }
    

    测试

    http://localhost:9001/sendmail
    
  • 相关阅读:
    P4141 消失之物(退背包模板)
    P5829 【模板】失配树
    P4827 [国家集训队] Crash 的文明世界
    P4074 [WC2013]糖果公园
    P3242 [HNOI2015]接水果
    P2371 [国家集训队]墨墨的等式
    P4819 [中山市选]杀人游戏
    P5331 [SNOI2019]通信
    BZOJ1082 [SCOI2005]栅栏
    poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]
  • 原文地址:https://www.cnblogs.com/ye-hcj/p/9639320.html
Copyright © 2011-2022 走看看