package com.lijianjun.test.wx;import javax.mail.*;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;public class Demo { public static void main(String[] args) throws MessagingException { String smtp="smtp.vip.163.com"; final String username="wangyi754345257@163.com"; final String password="TXJWKJAZPMEYJKXD"; Properties prop=new Properties(); //设置邮箱服务器 prop.put("mail.smtp.host",smtp); //设置主机端口 prop.put("mail.smtp.starttls.enable",false); prop.put("mail.smtp.port","465"); prop.put("mail.smtp.ssl.enable", true); //是否需要用户认证 prop.put("mail.smtp.auth",true); //是否启动TLS加密 prop.put("mail.smtp.starttls.enable",true); //获取session实例 Session session= Session.getInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); } }); //开启调试模式 session.setDebug(true); MimeMessage message=new MimeMessage(session); //设置发送方地址 message.setFrom(new InternetAddress("wangyi754345257@163.com")); message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("1436898698@qq.com")}); message.setSubject("hello","UTF-8"); message.setText("Hi 小李...","UTF-8"); Transport.send(message); }}
prop.put("mail.smtp.starttls.enable",false)
没有设置加密,使用465肯定报错
Sign in to make a reply
用户7427980155