一、开启QQ邮箱里的POP3/SMTP服务
①:开启步骤
1.邮箱设置 -->账号

2.开启服务(并复制 授权码
)


②:引入依赖
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>javax.mailgroupId>
<artifactId>mailartifactId>
<version>1.4.7version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-emailartifactId>
<version>1.4version>
dependency>
③:代码实现
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) {
sendEmailUtil("[email protected]","[email protected]","验证码","1314");
}
public static String sendEmailUtil(String sendName, String name,String title, String context){
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.user",sendName);
props.put("mail.password", "wztbfcxoduxwbhfh");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession);
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
InternetAddress to = new InternetAddress(name);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(title);
message.setContent(context, "text/html;charset=UTF-8");
Transport.send(message);
} catch (MessagingException e) {
return "您的验证码发送失败======>"+ e;
}
return "您的验证码发送成功======>验证码为:"+ context;
}
}
④:测试
