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 =

相关内容

热门资讯

第3分钟法门!智星德州插件(透... 第3分钟法门!智星德州插件(透视)好像真的有辅助辅助(哔哩哔哩)1、下载好智星德州插件脚本下载之后点...
1分钟机巧!wepoker到底... 1分钟机巧!wepoker到底有没有透视(透视)果然是有辅助开挂(哔哩哔哩)1、下载好wepoker...
7分钟学习!wepoker养号... 7分钟学习!wepoker养号规律(透视)总是有辅助辅助(哔哩哔哩)1)wepoker养号规律免费钻...
三分钟秘籍!hhpoker是真... 三分钟秘籍!hhpoker是真的假的(透视)都是是有辅助脚本(哔哩哔哩)1、操作简单,无需hhpok...
6分钟讲义!wepoker私局... 6分钟讲义!wepoker私局代打(透视)原来存在有辅助脚本(哔哩哔哩)1、全新机制【wepoker...
五分钟学习!来玩app破解(透... 五分钟学习!来玩app破解(透视)一直是真的有辅助透视(哔哩哔哩)1、来玩app破解透视辅助软件激活...
第7分钟举措!hhpoker是... 第7分钟举措!hhpoker是正规的吗(透视)都是是有辅助安装(哔哩哔哩)1、玩家可以在hhpoke...
第1分钟策略!德普之星透视辅助... 第1分钟策略!德普之星透视辅助软件是真的吗(透视)总是真的有辅助脚本(哔哩哔哩)运德普之星透视辅助软...
第一分钟积累!wpk真吗(透视... 第一分钟积累!wpk真吗(透视)都是真的是有辅助开挂(哔哩哔哩);小薇(辅助器软件下载)致您一封信;...
第7分钟经验!拱趴大菠萝有挂吗... 第7分钟经验!拱趴大菠萝有挂吗(透视)一直存在有辅助工具(哔哩哔哩)第7分钟经验!拱趴大菠萝有挂吗(...