public class Mail {
public static void main(String[] args) throws Exception {
//public static void sendMail(String toEmail) throws Exception{
Properties properties=new Properties();
properties.setProperty("mail.transport.protocol","smtp");//连接时的协议
properties.setProperty("mail.smtp.host","smtp.qq.com");//主机名
properties.setProperty("mail.smtp.port","465");//主机名和端口
properties.setProperty("mail.smtp.auth","true");//是否开启权限控制
properties.setProperty("mail.smtp.ssl.enable","true");//开启ssl安全认证
properties.setProperty("mail.debug","true");//如果设置为true则在发送邮件时会打印发送时的消息
//创建程序到邮件服务器之间的一次会话,即家到邮局的路
Session session=Session.getInstance(properties);
//session.setDebug(true);//调试
//获取邮件对象
Message message=new MimeMessage(session);//获取信封
message.setFrom(new InternetAddress("346103239@qq.com"));//发信人
message.setRecipients(MimeMessage.RecipientType.TO,new InternetAddress[]{new InternetAddress("346103239@qq.com")});//收信人,第二个参数是数组,因为收信人可以多个
message.setSubject("这是一封来自Java程序的测试邮件");//标题
message.setText("这是邮件的正文");//正文
//找到邮递员
Transport transport=session.getTransport();//创建快递员对象
transport.connect("346103239@qq.com","我的密码");//告诉邮递员你的用户名密码,证明你是用户
transport.sendMessage(message,message.getAllRecipients());//把信交给快递员,并告诉他收件人地址
transport.close();
}
}
qq邮箱设置没问题。请问为什么会有错误?
Could not connect to SMTP host: smtp.qq.com, port: 465