测试成功页面展示:
1、邮箱配置文件----mailConfig.properties
/*smtp服务器**/
mailHost=smtp.163.com
/*smtp服务器**/
mailPort=25
/*邮箱账号**/
mailUsername=****@163.com
/*163的授权码**/
mailPassword=*****
/*超时时间**/
mailTimeout=25000
/*收信人**/
to=****@163.com
2、初始化邮箱字段
package com.art.plat.domain;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailConfig{
private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
public static String host;//smtp服务器
public static Integer port;//服务器端口
public static String userName;//邮箱账号
public static String passWord;// 163的授权码
public static String emailForm;//邮箱账号
public static String to = ""; // 收件人地址
public static String subject = ""; // 邮件标题
public static String affix = ""; // 附件地址
public static String affixName = ""; // 附件名称
public static Properties properties;
static String[] TOS = null;
static{
init();
}
/**
* 初始化
*/
private static void init() {
properties = new Properties();
try{
/* InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);*/
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_DEFAULT));
host = properties.getProperty("mailHost");
port = Integer.parseInt(properties.getProperty("mailPort"));
to = properties.getProperty("to");
TOS=to.split(",");
userName = properties.getProperty("mailUsername");
passWord = properties.getProperty("mailPassword");
affix = properties.getProperty("affix");
affixName = properties.getProperty("affixName");
subject = properties.getProperty("subject");
} catch(IOException e){
e.printStackTrace();
}
}
}
3、测试
package cgi.designer;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import com.art.plat.domain.MailConfig;
import javax.mail.Message;
import javax.mail.MessagingException;
public class Mail {
public static void main(String[] args) throws AddressException, MessagingException {
MailConfig config = new MailConfig();
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", config.host);// 主机名
properties.put("mail.smtp.port", config.port);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress(config.userName));
// 设置收件人邮箱地址,如果554 DT:SPM 163 smtp5错误,抄送一份给自己,如下
// message.setRecipients(Message.RecipientType.CC, new InternetAddress[]{new InternetAddress(config.userName)});
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("2050911953@qq.com")});
message.setSubject("帐户密码重置");
message.setText("您使用了密码重置功能, 请通过下面的地址修改密码:www.baidu.com");
Transport transport = session.getTransport();
transport.connect(config.host,config.userName, config.passWord);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
transport.sendMessage(message, message.getAllRecipients());
System.out.println("");
transport.close();
}
}
ps:亲自测试已经成功,本人测试的时候,是新注册的163账号,出现了554 DT:SPM 163 smtp5错误(意思是发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;肯定是你发的邮件中有广告性信息,如:诚邀、免费注册之类的敏感词。当敏感词的比重达到一定数值时,就会被识别为垃圾邮件拒发。)
解决方法就是在发送邮件给他人的时候,抄送一份给自己
参考解决链接:https://jingyan.baidu.com/article/7f41ecec2cd807593c095c48.html