不BB,直接上码:
import javax.mail.*;
public void sendMail() {
try{InputStream inputStream = SysErrorLogServiveImpl.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
//读取配置文件。ps:SysErrorLogServiveImpl类为当前类,PROPERTIES_DEFAULT为“mailConfig.properties”配置文件名
msg.setFrom(new InternetAddress(sendAccount));//发件人邮箱号
//以下注释部分为添加多人收件使用
// StringBuffer sb = new StringBuffer();
// sb.append(receiveAccount);
// sb.append(",");
// sb.append(receiveAccount1);
//InternetAddress[] internetAddressTo = new InternetAddress().parse(sb.toString());
// msg.setRecipients(Message.RecipientType.TO, internetAddressTo);
transport.sendMessage(msg, new Address[] { new InternetAddress(receiveAccount) });//对方的地址
//transport.sendMessage(msg, msg.getAllRecipients());//多人地址
}
mailConfig.properties:
#服务器
mail.host=smtp.qq.com
#协议
mail.transport.protocol=smtp
mail.smtp.auth=true
#SSL加密
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#端口号
mail.smtp.port=465
mail.smtp.socketFactory.port=465
[email protected]
mail.send.password=xxxxxxxxxx //qq邮箱密钥可在邮箱设置-账户-POP3/SMTP服务和IMAP/SMTP服务开启时获得
maven依赖: