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 =

相关内容

热门资讯

黑科技辅助挂!wepoker作... 黑科技辅助挂!wepoker作弊视频,智星德州插件怎么下载,真是确实有挂(有挂解密)智星德州插件怎么...
程序员教你!闲来透视辅助器最新... 程序员教你!闲来透视辅助器最新版,欢乐茶馆辅助,详细器(有挂教程)亲,关键说明,闲来透视辅助器最新版...
2026版辅助挂!hhpoke... 2026版辅助挂!hhpoker德州真的假的,德扑之星有透视辅助吗,总是确实有挂(有挂头条)1、下载...
总算了解!九九山城插件,皮皮衡... 总算了解!九九山城插件,皮皮衡阳字牌科技,详细攻略(有挂神器)1、用户打开应用后不用登录就可以直接使...
今天下午!wpk辅助哪里买,德... 今天下午!wpk辅助哪里买,德扑之星辅助工具如何设置,总是是有挂(证实有挂)1、进入游戏-大厅左侧-...
玩家必看科普!天天微友助手破解... 玩家必看科普!天天微友助手破解版,哥哥打大a辅助神器,详细挂(有挂方针)一、天天微友助手破解版可以开...
现有关情况通报如下!pokem... 现有关情况通报如下!pokemmo辅助器手机版下载,智星德州插件怎么下载,真是是真的有挂(详细教程)...
查到实测!决战十三辅助辅助器,... 查到实测!决战十三辅助辅助器,互游辅助,详细神器(有挂透明挂)1、玩家可以在决战十三辅助辅助器透视最...
此事引发广泛关注!hhpoke... 此事引发广泛关注!hhpoker可以控制牌吗,德扑之星怎么设置埋牌,竟然是真的有挂(存在有挂)1、点...
大家学习交流!龙岩优优辅助,卡... 大家学习交流!龙岩优优辅助,卡农血拼辅助,详细app(有人有挂)小薇(辅助器软件下载)致您一封信;亲...