springboot簡單使用郵件功能

1,準備

①,首先發送郵箱要開啓特定服務,這裏以qq郵箱爲例html

②,開啓java

③,獲取密碼web

點擊生成受權碼,而後發送短信到指定的地方,便能獲取密碼spring

④,點擊我已發送,獲得受權碼,也就是application.properties 要配置的密碼springboot

2,配置

①,pom.xmlsession

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.2.RELEASE</version>
</parent>

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

②,application.properties 配置app

spring.mail.host=smtp.qq.com
spring.mail.username=2425373545@qq.com
#這裏填寫你收到的那個密碼
spring.mail.password=xxx
#必須開啓,不然會報503錯誤
spring.mail.properties.mail.smtp.ssl.enable=true

3,查看MailSenderAutoConfiguration

它爲咱們配置了一個JavaMailSenderImpl,而咱們也就是經過這個bean進行收發郵件異步

@Bean
	public JavaMailSenderImpl mailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		if (this.session != null) {
			sender.setSession(this.session);
		}
		else {
			applyProperties(sender);
		}
		return sender;
	}

4,測試代碼編寫

①,因爲發送郵件通常不要求實時,因此建議開啓異步spring-boot

@EnableAsync//開啓異步
@SpringBootApplication
public class TaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(TaskApplication.class, args);
    }
}

②,發送郵件的代碼編寫測試

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    /*發送簡單郵件*/
    @Async
    public String send() {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject("告白");
        message.setText("見什麼世面,見見你就行了");

        message.setTo("2539517371@qq.com");
        message.setFrom("2425373545@qq.com");
        javaMailSender.send(message);
        return "郵件發送成功。。。。";
    }
    /*發送帶html文本以及帶上附件*/
    @Async
    public String sendHtml() {
        MimeMessage message=javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper=new MimeMessageHelper(message,true);//支持文件上傳
            helper.setSubject("告白");
            helper.setText("<font color='red'>想你,老戴裏都是你</font>",true);

            helper.setTo("2539517371@qq.com");
            helper.setFrom("2425373545@qq.com");

            helper.addAttachment("dm.jpg",new File("D:\\dev\\IdeaProjects\\springboot\\task\\src\\main\\resources\\dm.jpg"));
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return "郵件發送成功。。。。";
    }
}

③,controller層代碼以下

import com.ts.task.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailController {

    @Autowired
    private MailService mailService;

    @RequestMapping("send")
    public String send() {
      mailService.send();
      return "郵件發送成功。。。。";
    }
    /*發送html文本以及帶上附件*/
    @RequestMapping("sendHtml")
    public String sendHtml() {
        mailService.sendHtml();
        return "郵件發送成功。。。。";
    }
}

5,測試

①,發送簡單郵件

②,發送帶附件,html文本的郵件

6,結果

①,

②,

③,

相關文章
相關標籤/搜索