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 =

相关内容

热门资讯

四分钟辅助!闲来辅助神器免费版... 四分钟辅助!闲来辅助神器免费版,其实真的有辅助软件(有挂辅助)闲来辅助神器免费版脚本下载中分为三种模...
六分钟辅助!中至余干510k控... 六分钟辅助!中至余干510k控制输赢,都是是有辅助技巧(有挂教程)中至余干510k控制输赢是不是有人...
7分钟辅助!欢乐对决ios辅助... 7分钟辅助!欢乐对决ios辅助,真是真的有辅助教程(有挂细节)1、欢乐对决ios辅助脚本辅助下载、欢...
八分钟辅助!阿拉游戏中心辅助,... 八分钟辅助!阿拉游戏中心辅助,好像是有辅助技巧(有挂教程)1、这是跨平台的阿拉游戏中心辅助轻量版有透...
1分钟辅助!微信辅助工具,都是... 1分钟辅助!微信辅助工具,都是是有辅助器(有挂猫腻)1、下载好微信辅助工具脚本下载之后点击打开,先需...
一分钟辅助!潮友辅助插件,竟然... 一分钟辅助!潮友辅助插件,竟然真的有辅助脚本(有挂存在)1、玩家可以在潮友辅助插件线上大神俱乐部对游...
第9分钟辅助!天天手游辅助工具... 第9分钟辅助!天天手游辅助工具,真是是有辅助工具(新版有挂)该软件可以轻松地帮助玩家将天天手游辅助工...
第7分钟辅助!八大碗辅助器,总... 第7分钟辅助!八大碗辅助器,总是存在有辅助攻略(有挂方针)1、每一步都需要思考,不同水平的挑战会更加...
8分钟辅助!悠闲碰碰辅助,其实... 8分钟辅助!悠闲碰碰辅助,其实是真的有辅助神器(有挂方法)1、悠闲碰碰辅助辅助器安装包、悠闲碰碰辅助...
第二分钟辅助!微乐小程序辅助器... 第二分钟辅助!微乐小程序辅助器出租,总是是真的有辅助app(真的有挂)1)微乐小程序辅助器出租辅助插...