AndroidStudio中如何解决javax.mail.AuthenticationFailedException?
创始人
2024-08-21 02:30:16
0
  1. 确认邮箱账号和密码是否正确。

  2. 确认邮箱服务商是否开启了IMAP/POP3服务。

  3. 检查代码中是否正确配置了SMTP/IMAP/POP3的相关参数。

参考代码:

使用JavaMail发送邮件:

String host = "smtp.gmail.com";
String port = "587";
String username = "your-email-address@gmail.com";
String password = "your-email-password";

Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");

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("your-email-address@gmail.com"));
    message.setRecipients(
            Message.RecipientType.TO,
            InternetAddress.parse("recipient-email-address@example.com")
    );
    message.setSubject("Testing JavaMail API");
    message.setText("Dear Recipient," +
            "\n\nThis is a test email sent using JavaMail API. " +
            "Please do not reply to this email." +
            "\n\nThank you." +
            "\n\nBest regards," +
            "\nYour Name");

    Transport.send(message);

    System.out.println("Email sent successfully.");

} catch (MessagingException e) {
    e.printStackTrace();
}

使用JavaMail接收邮件:

String host = "imap.gmail.com";
String port = "993";
String username = "your-email-address@gmail.com";
String password = "your-email-password";

Properties properties = new Properties();
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", port);
properties.put("mail.imap.starttls.enable", "true");
properties.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session session = Session.getInstance(properties,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

try {
    Store store =

相关内容

热门资讯

重磅来袭!天天川南悠闲辅助,佛... 重磅来袭!天天川南悠闲辅助,佛手十三道辅助工具(一贯真的是有挂)佛手十三道辅助工具是不是有人用挂微扑...
盘点十款!胡易辅助软件,wep... 盘点十款!胡易辅助软件,wepoker轻量版书签,科技教程(有挂猫腻);胡易辅助软件软件透明挂更新新...
总算明白!海豹3+1辅助器,桂... 总算明白!海豹3+1辅助器,桂林字牌辅助(都是有挂)小薇(辅助器软件下载)致您一封信;亲爱海豹3+1...
热点推荐!安卓闲逸辅助,wep... 热点推荐!安卓闲逸辅助,wepoker有辅助插件吗,科技教程(确实有挂);一、安卓闲逸辅助有挂的是的...
实测必看!盛世2私人辅助,79... 实测必看!盛世2私人辅助,798大菠萝辅助(好像是真的有挂)运盛世2私人辅助辅助工具,进入游戏界面。...
揭秘真相!江西微乐南昌有辅助,... 揭秘真相!江西微乐南昌有辅助,德州透视竞技联盟,解密教程(有挂猫腻);1.江西微乐南昌有辅助 ai辅...
交流学习经验!陕麻圈辅助器透视... 交流学习经验!陕麻圈辅助器透视开挂,福麻圈辅助(其实是有挂)陕麻圈辅助器透视开挂辅助器是一种具有地方...
玩家必看科普!白银胡乐亲友房,... 您好,白银胡乐亲友房这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...
玩家必看科普!多乐跑辅助,情怀... 玩家必看科普!多乐跑辅助,情怀游戏辅助器(好像真的有挂)1、玩家必看科普!多乐跑辅助,情怀游戏辅助器...
重大通报!边锋干瞪眼辅助,we... 您好:边锋干瞪眼辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...