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 =

相关内容

热门资讯

盘点几款!微乐江苏自建房辅助a... 盘点几款!微乐江苏自建房辅助app!好像真的有辅助app(有挂存在)1、在微乐江苏自建房辅助app插...
一分钟教会你!上品斗地主游戏辅... 一分钟教会你!上品斗地主游戏辅助!真是确实有辅助软件(有挂头条)1、上品斗地主游戏辅助免费辅助多个强...
一分钟揭秘!点点长牌源码!真是... 一分钟揭秘!点点长牌源码!真是存在有辅助挂(有挂教程)1、点点长牌源码免费脚本咨询教程、点点长牌源码...
这一现象值得深思!广东闲来辅助... 这一现象值得深思!广东闲来辅助免费!原来存在有辅助软件(有挂实锤)进入游戏-大厅左侧-新手福利-激活...
玩家必看分享!万能脚本辅助器!... 玩家必看分享!万能脚本辅助器!确实确实有辅助挂(有挂秘笈)万能脚本辅助器破解侠是真的助透视。每个模型...
2024教程!随意玩房卡辅助!... 2024教程!随意玩房卡辅助!原来真的有辅助技巧(发现有挂)1、让任何用户在无需随意玩房卡辅助安装教...
技术分享!大头大菠萝辅助!都是... 技术分享!大头大菠萝辅助!都是是真的有辅助技巧(有挂总结)1、起透看视 大头大菠萝辅助辅助软件价格2...
围绕透视问题!菠萝辅助破解版!... 围绕透视问题!菠萝辅助破解版!一贯真的是有辅助攻略(有挂分享)围绕透视问题!菠萝辅助破解版!一贯真的...
玩家必看攻略!胡乐辅助脚本!确... 玩家必看攻略!胡乐辅助脚本!确实真的有辅助软件(有挂分析)1、实时胡乐辅助脚本透视辅助更新:用户可以...
一分钟了解!微信欢乐卡五星辅助... 一分钟了解!微信欢乐卡五星辅助!一直真的是有辅助软件(有挂讲解)1、这是跨平台的微信欢乐卡五星辅助轻...