强力推荐Java架构师学习网址:https://qiuyadongsite.github.io/
这里使用maven项目进行开发
1.首先pom中加载jar
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
2.定义3个类来辅助开发:
第一个类:MailServerConfig:用于定义发送邮件的基本信息
public class MailServerConfig {
private String mailServerUrl;
private String mailServerPort;
private String mailAccount;
private String password;
private String sender;
private Boolean proxySet;
private String proxyType;
private String proxyHost;
private String proxyPort;
}
第二个类SimpleAuthenticator:用于定义验证发送邮件的账号密码:
public class SimpleAuthenticator extends Authenticator {
String userName = null;
String password = null;
public SimpleAuthenticator() {
}
public SimpleAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
第三个类MailServerConfig用于使用代理的情况
public class MailServerConfig {