• SpringBoot 发送邮件


    SpringBoot发送邮件

    1、pom

    		<parent>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-parent</artifactId>
    			<version>2.3.4.RELEASE</version>
    			<relativePath/> <!-- lookup parent from repository -->
    		</parent>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
                <scope>test</scope>
            </dependency>
    

    2、yml

    spring.mail.username=90xxxxx31@qq.com
    spring.mail.password=qq邮箱授权码
    spring.mail.host=smtp.qq.com
    #开启加密验证
    spring.mail.properties.mail.smtp.enable=true
    
    

    3、mailSender

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @SpringBootTest
    class SpringbootTaskApplicationTests {
    
        @Autowired
        JavaMailSenderImpl mailSender;
    
        @Test
        void contextLoads() {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    
            simpleMailMessage.setSubject("桔梗,你好");
            simpleMailMessage.setText("感谢你一生的陪伴");
            simpleMailMessage.setSubject("桔梗");
    
            simpleMailMessage.setTo("26xxxxx83@qq.com");
            simpleMailMessage.setFrom("90xxxxx31@qq.com");
    
            mailSender.send(simpleMailMessage);
        }
    
        @Test
        void contextLoads2() throws MessagingException {
            //一个复杂的邮件
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    
            helper.setSubject("桔梗,你好");
            helper.setText("<p style='color:red'>感谢你一生的陪伴 +" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + "+</p>", true);
            helper.addAttachment("美女1.jpeg", new File("C:\Users\A\Desktop\美女1.jpeg"));
            helper.addAttachment("美女2.jpg", new File("C:\Users\A\Desktop\美女2.jpg"));
    
            helper.setTo("26xxxxx83@qq.com");
            helper.setFrom("90xxxxx31@qq.com");
    
            mailSender.send(mimeMessage);
        }
    
    }
    
    
  • 相关阅读:
    learning hdmi edid protocol
    得到本地应用程序的EXE的路径
    获取系统特殊文件夹路径
    判断计算机的联机状态
    判断计算机是否连接网络
    得到本地机器的IP地址
    获取屏幕分辨率
    获取声音设备名称
    获取显示设备的名称及PNPDeviceID
    判断驱动器类型并获其属性
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/14615271.html
Copyright © 2020-2023  润新知