java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot邮箱发送激活码

springboot实现邮箱发送(激活码)功能的示例代码

作者:村口曹大爷

这篇文章主要为大家详细介绍了如何利用springboot实现邮箱发送(激活码)功能,文中的示例代码简洁易懂,有需要的小伙伴可以跟随小编一起学习一下

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

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

第二步:引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>malisend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>malisend</name>
    <description>malisend</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.malisend.MalisendApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 
</project>

第三步:写配置文件

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实现邮箱发送(激活码)功能的示例代码的详细内容,更多关于springboot邮箱发送激活码的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文