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 =

相关内容

热门资讯

第二分钟机巧!对战互娱有辅助吗... 第二分钟机巧!对战互娱有辅助吗(外挂)一贯一直总是有辅助技巧(哔哩哔哩)1、对战互娱有辅助吗免费脚本...
第4分钟总结!财神十三张脚本怎... 第4分钟总结!财神十三张脚本怎么弄的教程(外挂)真是真的有辅助app(哔哩哔哩)1、首先打开财神十三...
第7分钟练习!福建兄弟十三水辅... 第7分钟练习!福建兄弟十三水辅助器(外挂)总是是有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明福建兄弟...
第一分钟方式!微信江苏小程序游... 第一分钟方式!微信江苏小程序游戏破解器下载(外挂)都是真的有辅助攻略(哔哩哔哩)1、玩家可以在微信江...
十分钟举措!皮皮游戏辅助工具(... 十分钟举措!皮皮游戏辅助工具(外挂)切实是有辅助app(哔哩哔哩)1、打开软件启动之后找到中间准星的...
第一分钟方案!微信多乐跑得快辅... 第一分钟方案!微信多乐跑得快辅助工具(外挂)切实真的有辅助攻略(哔哩哔哩)1、打开软件启动之后找到中...
两分钟学习!微乐脚本辅助器(外... 两分钟学习!微乐脚本辅助器(外挂)一直有辅助插件(哔哩哔哩)1、任何微乐脚本辅助器透视是真的假的的玩...
1分钟妙招!丫丫辅助(外挂)竟... 1分钟妙招!丫丫辅助(外挂)竟然有辅助app(哔哩哔哩)1、完成丫丫辅助辅助器v3.3的残局,帮助玩...
第8分钟总结!逍遥辅助器手机版... 第8分钟总结!逍遥辅助器手机版(外挂)原来是真的有辅助攻略(哔哩哔哩)1、游戏颠覆性的策略玩法,独创...
四分钟手筋!越乡游义乌辅助器免... 四分钟手筋!越乡游义乌辅助器免费试用(外挂)原来一直总是有辅助方法(哔哩哔哩)越乡游义乌辅助器免费试...