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 =

相关内容

热门资讯

方法辅助挂!潘潘讲故事app辅... 方法辅助挂!潘潘讲故事app辅助(辅助)真是是有辅助挂(真实有挂)1、点击下载安装,潘潘讲故事app...
记者获悉!小程序蜀山西川血战辅... 记者获悉!小程序蜀山西川血战辅助(辅助)切实存在有辅助工具(有挂详情)小薇(辅助器软件下载)致您一封...
2026版规律!广东雀神智能插... 2026版规律!广东雀神智能插件辅助(辅助)一贯确实有辅助技巧(有挂透视)1)广东雀神智能插件辅助有...
2026版辅助挂!九九联盟后台... 2026版辅助挂!九九联盟后台可以操作吗(辅助)总是是有辅助方法(有挂透明挂)九九联盟后台可以操作吗...
方法辅助挂!黑科技辅助软件免费... 方法辅助挂!黑科技辅助软件免费(辅助)切实是真的有辅助工具(有挂秘籍)一、黑科技辅助软件免费可以开透...
此事备受玩家关注!雀神广东麻将... 此事备受玩家关注!雀神广东麻将透视视频(辅助)竟然存在有辅助攻略(讲解有挂)1、金币登录送、破产送、...
无独有偶!潮友会pp下载辅助器... 无独有偶!潮友会pp下载辅助器(辅助)好像真的是有辅助app(有挂秘籍)1、下载好潮友会pp下载辅助...
2026版方法!广东雀神祈福真... 2026版方法!广东雀神祈福真的有用吗(辅助)好像是有辅助技巧(有挂工具)1、进入到广东雀神祈福真的...
网友热议!欢聚水鱼神器(辅助)... 网友热议!欢聚水鱼神器(辅助)确实真的是有辅助工具(有挂秘笈)欢聚水鱼神器能透视中分为三种模型:欢聚...
方法辅助挂!欢乐达人葫芦鱼辅助... 方法辅助挂!欢乐达人葫芦鱼辅助(辅助)一贯真的有辅助挂(有挂解密)1)欢乐达人葫芦鱼辅助辅助插件:进...