java发送谷歌邮箱报错离谱

报错信息:

img


代码如下:

img

img


有没有开发过类似的或者相同的牛人可以指导一下

可以看看下述代码示例对您是否有帮助

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
    public static void main(String[] args) {
        // 设置邮件服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        // 获取邮箱登录凭证
        final String username = "your@gmail.com";
        final String password = "your_password";

        // 获取邮件会话对象
        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            // 创建邮件消息对象
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("to@gmail.com"));
            message.setSubject("邮件主题");
            message.setText("邮件正文");

            // 发送邮件
            Transport.send(message);

            System.out.println("邮件发送成功!");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败:" + e.getMessage());
        }
    }
}

谷歌邮箱被墙了吖,当然链接超时