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... 实操分享开挂!新版白金岛麻将ai开挂多功能插件,好像真的有开挂下载烘培;近期一款 AI 黑科技工具火...
玩家必看分享辅助挂!闲来安徽麻... 很多人使用云端 AI 时,都担心闲来安徽麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技...
2027版外挂!昆明文山麻将外... 2027版外挂!昆明文山麻将外挂多功能AI插件,昆明文山麻将好像真的是有外挂插件1、玩家可以在昆明文...
最新技巧!透视!星悦山东麻将a... 您好,星悦山东麻将这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款...
2027版外挂!中天棋牌外挂多... 2027版外挂!中天棋牌外挂多功能AI方法,中天棋牌好像是真的有外挂方法一、中天棋牌可以开透视的定义...
实测必看辅助!紫阳麻将大众ai... 您好:紫阳麻将大众这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
2027版透视挂!I Star... 2027版透视挂!I Star透视挂多功能AI脚本,I Star总是真的是有透视挂脚本I Star是...
玩家必知教程!透视挂!长歌休闲... 玩家必知教程!透视挂!长歌休闲ai透视挂多功能插件,确实真的有透视挂教程课程主打本地运行的 AI 黑...
2027版透视器!云龙干瞪眼雀... 2027版透视器!云龙干瞪眼雀牌透视器多功能AIapp,云龙干瞪眼雀牌总是存在有透视器app;1、进...
前缀收集词辅助!闲来广东麻将a... 前缀收集词辅助!闲来广东麻将ai辅助多功能插件,果然是有辅助插件学习AI 是一款可以让一直输的玩家修...