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 =

相关内容

热门资讯

分享认知外挂!快盈外挂AI方法... 分享认知外挂!快盈外挂AI方法,快盈多功能果然是真的有外挂方法1、用户打开应用后不用登录就可以直接使...
每日必备透视器!哥哥跑得快透视... 每日必备透视器!哥哥跑得快透视器AI软件,哥哥跑得快多功能一贯存在有透视器软件1、进入游戏-大厅左侧...
热点推荐开挂!贪玩棋牌开挂AI... 热点推荐开挂!贪玩棋牌开挂AI脚本,贪玩棋牌多功能都是是有开挂脚本1、下载好贪玩棋牌透视辅助下载之后...
推荐一款开挂!卡卡众娱开挂AI... 推荐一款开挂!卡卡众娱开挂AI技巧,卡卡众娱多功能竟然有开挂技巧1)卡卡众娱辅助插件:进一步探索卡卡...
玩家必看科普透视挂!贝壳互娱厅... 玩家必看科普透视挂!贝壳互娱厅透视挂AIapp,贝壳互娱厅多功能真是真的是有透视挂app1、下载好贝...
玩家必用辅助器!1973棋牌辅... 玩家必用辅助器!1973棋牌辅助器AI技巧,1973棋牌多功能其实真的有辅助器技巧1、起透看视 19...
一分钟揭秘透视挂!朋朋棋牌透视... 一分钟揭秘透视挂!朋朋棋牌透视挂AI攻略,朋朋棋牌多功能其实是有透视挂攻略朋朋棋牌能透视中分为三种模...
最新研发透视器!熊猫互娱透视器... 最新研发透视器!熊猫互娱透视器AI插件,熊猫互娱多功能本来是真的有透视器插件1、在熊猫互娱插件功能辅...
实操分享辅助!易玩堂辅助AI教... 实操分享辅助!易玩堂辅助AI教程,易玩堂多功能一贯真的是有辅助教程1、玩家可以在易玩堂线上大神俱乐部...
一起来探讨外挂!葫芦岛爱玩麻将... 一起来探讨外挂!葫芦岛爱玩麻将外挂AI攻略,葫芦岛爱玩麻将多功能其实真的是有外挂攻略1、玩家可以在葫...