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 =

相关内容

热门资讯

第6分钟了解!开心泉州作弊!果... 第6分钟了解!开心泉州作弊!果然是有辅助技巧(今日头条)-哔哩哔哩1、开心泉州作弊模拟器是什么优化,...
第十分钟了解!钱塘13水挂件!... 第十分钟了解!钱塘13水挂件!切实是有辅助插件(有挂教学)-哔哩哔哩钱塘13水挂件能透视中分为三种模...
第6分钟了解!兴动互娱脚本!都... 第6分钟了解!兴动互娱脚本!都是一直总是有辅助技巧(有挂透视)-哔哩哔哩1、该软件可以轻松地帮助玩家...
五分钟了解!三加一辅助器!切实... 五分钟了解!三加一辅助器!切实是有辅助软件(有挂教学)-哔哩哔哩1、三加一辅助器模拟器是什么优化,三...
3分钟了解!潮友潮汕木虱辅助!... 3分钟了解!潮友潮汕木虱辅助!都是真的有辅助方法(有挂方式)-哔哩哔哩1、这是跨平台的潮友潮汕木虱辅...
1分钟了解!约局吧脚本透视开挂... 1分钟了解!约局吧脚本透视开挂!竟然真的有辅助app(有挂存在)-哔哩哔哩1、约局吧脚本透视开挂透视...
第三分钟了解!pokemmo脚... 第三分钟了解!pokemmo脚本最新版!确实存在有辅助方法(有挂教学)-哔哩哔哩pokemmo脚本最...
七分钟了解!温州茶苑安卓手机版... 七分钟了解!温州茶苑安卓手机版辅助器!确实真的有辅助工具(有挂细节)-哔哩哔哩1、让任何用户在无需温...
第7分钟了解!哈糖大菠萝助手!... 第7分钟了解!哈糖大菠萝助手!一直一直都是有辅助方法(真是有挂)-哔哩哔哩1)哈糖大菠萝助手免费钻石...
第3分钟了解!赣南辅助是真的吗... 第3分钟了解!赣南辅助是真的吗!果然存在有辅助插件(了解有挂)-哔哩哔哩1、点击下载安装,赣南辅助是...