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私局代打(起初真的有挂);亲,有的,ai轻松简单,又...
推荐一款!雀神麻将智能插件开挂... 推荐一款!雀神麻将智能插件开挂(辅助挂)德普之星透视辅助插件(竟然是真的有挂)相信很多朋友都在电脑上...
我来教大家!江汉晃晃辅助器(辅... 我来教大家!江汉晃晃辅助器(辅助挂)we poker游戏下(好像存在有挂);我来教大家!江汉晃晃辅助...
教学盘点!天天微友开控制多少钱... 教学盘点!天天微友开控制多少钱(辅助挂)wepoker透视版下载(最初有挂);1、不需要AI权限,帮...
最新技巧!微信大厅辅助挂(辅助... 最新技巧!微信大厅辅助挂(辅助挂)sohoopoker辅助(起初真的是有挂);最新技巧!微信大厅辅助...
科普攻略!科乐填大坑破解版安卓... 科普攻略!科乐填大坑破解版安卓(辅助挂)wepoker轻量版透视方法(先前存在有挂)是一款可以让一直...
今日百科!微乐河南小程序微乐辅... 今日百科!微乐河南小程序微乐辅助脚本(辅助挂)hhpoker是正品吗(起初存在有挂)是一款可以让一直...
今日重大通报!来来舟山麻将辅助... 今日重大通报!来来舟山麻将辅助(辅助挂)拱趴大菠萝自动计算机器人(都是真的有挂);相信小伙伴都知道这...
最新通报!微信小程序多乐辅助器... 最新通报!微信小程序多乐辅助器(辅助挂)epoker免费透视脚本(最初真的有挂);一、微信小程序多乐...