Java代码发送Email邮件教程(详细)
其他
2020-03-23 13:30:16
阅读次数: 0
Java代码发送Email邮件教程(详细)
一. 环境准备
1.1 本节要点
1.2 概述
- 在自己的项目中引入MailUtil工具类,以及相关依赖
- 将授权码以及其他信息补全
- 按照示例,直接调用即可
1.3 实战步骤
- 打开IDEA,创建一个项目,随意取名;
- 引入相关依赖:(完整pom.xml如下:)
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- excel end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建MailUtil工具类: 代码如下:
package com.example.demo.util;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class MailUtil {
public static String myEmailAccount = "[email protected]";
public static String myEmailPassword = "xxx";
public static String myEmailSMTPHost = "smtp.qq.com";
public static String receiveMailAccount = "[email protected]";
public static void sendActiveMail(String receiveMailAccount, String mailActiveCode) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount, mailActiveCode);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String mailActiveCode) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sendMail, "这里写发件人名称", "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "您好", "UTF-8"));
message.setSubject("我是邮件主题", "UTF-8");
String activeUrl = "http://localhost:8080/web_pro/user/activemail?mailcode=" + mailActiveCode;
message.setContent("尊敬的用户,您好!xxxxx邀请您击激活链接完成邮箱激活<a href=" + activeUrl + " target='_blank'>" + activeUrl + "</a>", "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
public static void main(String[] args) throws Exception {
String email = "[email protected]";
try {
MailUtil.sendActiveMail(email, new Date().getTime() + "");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 这里我们需要指定收件人以及填入内容。在做这个之前,我们需要先生成授权码。通过授权码我们才能够将信息发送给收件人。这里获取授权码以qq邮箱举例:
- 登录qq邮箱,然后点击设置,然后再点击账户,如图所示:
- 往下翻,找到POP3…服务,然后点击开启服务:POP3/SMTP服务,最后再下方点击生成授权码,如图所示:
- 将得到的授权码复制进入代码中,如图所示:
1.4 源码参考
发布了127 篇原创文章 ·
获赞 52 ·
访问量 2万+
转载自blog.csdn.net/qq_37128049/article/details/103729805