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 =

相关内容

热门资讯

透视能赢(德州微扑克专用)外挂... 透视能赢(德州微扑克专用)外挂透明挂辅助器安装(辅助挂)透视辅助(2025已更新)(哔哩哔哩);亲,...
发现一款(哈糖大菠萝平台)外挂... 发现一款(哈糖大菠萝平台)外挂透明挂辅助软件(透视)原来是真的有挂(可靠教程)(哔哩哔哩)是一款可以...
透视存在(wpk测试)外挂透明... 透视存在(wpk测试)外挂透明挂辅助神器(辅助挂)辅助透视(2020已更新)(哔哩哔哩);亲们利用一...
透视好友房(WPK开挂)外挂透... 透视好友房(WPK开挂)外挂透明挂辅助挂(辅助挂)原来真的有挂(切实教程)(哔哩哔哩),亲,有的,a...
专业讨论(aapoker手游版... 专业讨论(aapoker手游版)外挂透明挂辅助挂(透视)软件透明挂(2022已更新)(哔哩哔哩);值...
透视游戏(德扑之星机制)外挂透... 透视游戏(德扑之星机制)外挂透明挂辅助APP(透视)原来真的有挂(必胜教程)(哔哩哔哩);wpk透视...
分享实测(wePoke)外挂透... 分享实测(wePoke)外挂透明挂辅助工具(透视)软件透明挂(2021已更新)(哔哩哔哩)1、玩家可...
玩家必看科普(德州透视)外挂透... 玩家必看科普(德州透视)外挂透明挂辅助器安装(透视)透视辅助(确实有挂)-哔哩哔哩;wpk透视辅助官...
交流学习经验(鱼扑克app a... 交流学习经验(鱼扑克app ai)外挂透明挂辅助脚本(透视)其实是真的有挂(安装教程)(哔哩哔哩);...
技术分享(wepoke ai)... 技术分享(wepoke ai)外挂透明挂辅助器(透视)软件透明挂(2023已更新)(哔哩哔哩)关于w...