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 =

相关内容

热门资讯

事发当天!决战血流辅助器!一贯... 事发当天!决战血流辅助器!一贯真的有辅助插件(有挂神器)-哔哩哔哩所有人都在同一条线上,像星星一样排...
这一问题亟待解决!打哈儿脚本免... 这一问题亟待解决!打哈儿脚本免费!确实是真的辅助app(确实有挂)-哔哩哔哩1、起透看视 打哈儿脚本...
出现新变化!创思维激k看底牌辅... 您好,创思维激k看底牌辅助开发商这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
据了解!鱼虾蟹玩法辅助脚本!果... 据了解!鱼虾蟹玩法辅助脚本!果然真的有辅助脚本(有挂解密)-哔哩哔哩1、每一步都需要思考,不同水平的...
为切实保障!纳祥游戏科技!竟然... 为切实保障!纳祥游戏科技!竟然真的有辅助挂(有挂助手)-哔哩哔哩1、下载好纳祥游戏科技脚本下载之后点...
无独有偶!微信小程序打哈儿脚本... 无独有偶!微信小程序打哈儿脚本下载!确实真的有辅助工具(有挂透明挂)-哔哩哔哩1、起透看视 微信小程...
长期以来!兴动互娱脚本免费最新... 长期以来!兴动互娱脚本免费最新版本!一贯真的有辅助修改器(有挂辅助)-哔哩哔哩暗藏猫腻,小编详细说明...
相较于以往!皇豪互众智能辅助器... 相较于以往!皇豪互众智能辅助器破解!本来存在有辅助下载(有挂秘笈)-哔哩哔哩1、皇豪互众智能辅助器破...
随着!微乐小程序辅助教程!真是... 您好,微乐小程序辅助教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
近日!约战沙城脚本!总是是有辅... 近日!约战沙城脚本!总是是有辅助平台(有挂分享)-哔哩哔哩1、约战沙城脚本公共底牌简单,约战沙城脚本...