springboot实现邮箱发送(激活码)功能

第一步:现在邮箱里面开启smtp服务

springboot实现邮箱发送(激活码)功能_第1张图片

这里用163邮箱举例,配置一下授权密码,这个要提前记住

springboot实现邮箱发送(激活码)功能_第2张图片

第二步:引入依赖



    4.0.0
    com.example
    malisend
    0.0.1-SNAPSHOT
    malisend
    malisend
    
        1.8
        UTF-8
        UTF-8
        2.6.13
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
        

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    com.example.malisend.MalisendApplication
                    true
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    


第三步:写配置文件

spring:
  mail:
    host: smtp.163.com # 网站发送邮件邮箱服务 host
    port: 465
    username:  # 开启那个服务的邮箱
    password:  # 开启服务的那个认证码
    properties:
      mail:
        smtp:
          auth: true
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory
          starttls:
            enable: true

第四步:写代码

Order实体类:

package com.example.malisend.demos;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private int oid;
    private String oname;
    private float price;
}
OrderManager接口
package com.example.malisend.demos;

public interface OrderManager {

    void placeOrder(Order order);

}
SimpleOrderManager类
package com.example.malisend.demos;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

@Service
public class SimpleOrderManager implements OrderManager {

    private JavaMailSender mailSender;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void placeOrder(final Order order) {
        // Do the business calculations...
        // Call the collaborators to persist the order...

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setRecipient(Message.RecipientType.TO,
                        new InternetAddress("要发送给那个人的邮箱"));
                mimeMessage.setFrom(new InternetAddress("开启服务的那个邮箱"));
                mimeMessage.setText("Dear " + order.getOname() + " " +
                        order.getPrice() + ", thanks for your order. " +
                        "Your order number is "  + ".");
            }
        };

        try {
            this.mailSender.send(preparator);
        }
        catch (MailException ex) {
            // simply log it and go on...
            System.err.println(ex.getMessage());
        }
    }

}
OrderController类
package com.example.malisend.demos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    SimpleOrderManager orderManager;

    @Autowired
    JavaMailSenderImpl mailSender;

    Order order=new Order(1,"cyl",2.8f);
    @GetMapping("/sendMail")
    public void sendMail(){

        orderManager.setMailSender(mailSender);
        orderManager.placeOrder(order);
    }
}

访问:http://localhost:8080/sendMail

结果:


springboot实现邮箱发送(激活码)功能_第3张图片

你可能感兴趣的:(1024程序员节,spring,springboot)