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
    
  • 相关阅读:
    VS2013 update4+Cocos2d-x 3.7 Win8下安装方法及配置
    它处资料:二分图最大匹配的匈牙利算法
    DataGuard备库ORA-01196故障恢复一则
    Leetcode41: Remove Duplicates from Sorted List
    BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 使用Napa开发SharePoint应用程序
    关于OC的内存管理-01
    P2002 消息扩散
    P1726 上白泽慧音
    2594 解药还是毒药
    P3385 【模板】负环
  • 原文地址:https://www.cnblogs.com/ye-hcj/p/9639320.html
Copyright © 2011-2022 走看看