我用的是微软azure发送的
email.properties
#如果是qq邮箱的话是 smtp.qq.com
host=smtp.sendgrid.net
#qq邮箱是 465
port=587
userName=azure帐号
#qq邮箱的话是授权码,而不是邮箱密码
password=密码
from=azure帐号
to=收件人帐号
pom.xml
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.2</version>
</dependency>
MailAuthenticator.java
import javax.mail.PasswordAuthentication;
public class MailAuthenticator extends Authenticator {
private String username;
private String password;
public MailAuthenticator() {
}
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
MailInfo.java
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailInfo {
private String host;// 邮件服务器域名或IP
private String mailPort;
private String from;// 发件人
private String to;// 收件人
private String cc;// 抄送人
private String username;// 发件人用户名
private String password;// 发件人密码
private String title;// 邮件的主题
private String content;// 邮件的内容
private String[] attachFileNames;//邮件附件的文件名
/**
* 获取邮件参数
*
* @return
* @throws GeneralSecurityException
*/
public Properties getProperties() throws GeneralSecurityException {
Properties props = new Properties();
props.put("mail.smtp.host", getHost());
props.put("mail.smtp.port", getMailPort());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
/** 如果用的qq邮箱,下面的注释放开 */
// MailSSLSocketFactory sslSF = new MailSSLSocketFactory();
// sslSF.setTrustAllHosts(true);
// props.put("mail.smtp.ssl.enable", "true");
// props.put("mail.smtp.ssl.socketFactory", sslSF);
pr