Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问父项';Java中的s保护方法_Java_Email_Jakarta Mail - Fatal编程技术网

无法访问父项';Java中的s保护方法

无法访问父项';Java中的s保护方法,java,email,jakarta-mail,Java,Email,Jakarta Mail,我有一个问题: 包含内部类的java类。内部类(Authenticator)扩展了javax.mail.Authenticator(注意类和子类的名称相同) 问题是我无法访问受保护的方法getPasswordAuthentication,除非内部类与扩展类具有相同的名称(我的意思是Authenticator extensed javax.mail.Authenticator)。 如果我编写私有类SMTPAuthenticator extensed javax.mail.Authenticator

我有一个问题:

包含内部类的java类。内部类(
Authenticator
)扩展了
javax.mail.Authenticator
(注意类和子类的名称相同)

问题是我无法访问受保护的方法
getPasswordAuthentication
,除非内部类与扩展类具有相同的名称(我的意思是
Authenticator extensed javax.mail.Authenticator
)。 如果我编写
私有类SMTPAuthenticator extensed javax.mail.Authenticator
,那么我将无法再访问受保护的
getPasswordAuthentication
。 见下面的代码:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailWithPasswordAuthentication {
    public static void main(String[] args) throws MessagingException {
        new MailWithPasswordAuthentication().run();
    }

    private void run() throws MessagingException {
        Message message = new MimeMessage(getSession());

        message.addRecipient(RecipientType.TO, new InternetAddress("to@example.com"));
        message.addFrom(new InternetAddress[] { new InternetAddress("from@example.com") });

        message.setSubject("the subject");
        message.setContent("the body", "text/plain");

        Transport.send(message);
    }

    private Session getSession() {
        Authenticator authenticator = new Authenticator();

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        properties.setProperty("mail.smtp.auth", "true");

        properties.setProperty("mail.smtp.host", "mail.example.com");
        properties.setProperty("mail.smtp.port", "25");

        return Session.getInstance(properties, authenticator);
    }

    private class Authenticator extends javax.mail.Authenticator {
        private PasswordAuthentication authentication;

        public Authenticator() {
            String username = "auth-user";
            String password = "auth-password";
            authentication = new PasswordAuthentication(username, password);
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }
    }
}
谢谢,
A

如果将验证器类重命名为SMTPAuthenticator,还应更改行:

Authenticator authenticator = new Authenticator();


出现错误的原因是只有
SMTPAuthenticator
类包含
getPasswordAuthentication()
方法,不是基本的
验证器

如果您可以访问
验证器扩展javax.mail.Authenticatore
,那么您应该能够访问
类SMTPAuthenticator扩展javax.mail.Authenticator
。如果不是,那么您的观察是错误的。

您得到的错误是什么?类的名称不会决定方法的访问级别。我同意,类的名称不会决定方法的访问级别。这就是我问这个问题的原因。请看下面我的评论。错误是:类型验证器中的getPasswordAuthentication()方法不可见这可能是一个很好的答案,有兴趣了解OP的实际错误是什么。是的,我认为你是对的,我应该使用SMTPAuthenticator Authenticator=new SMTPAuthenticator()而不是Authenticator Authenticator=new SMTPAuthenticator()。我把界面弄糊涂了,在这里你可以使用“界面=新的实现”。谢谢你,Authenticator Authenticator=new SMTPAuthenticator()我得到了一个正确的错误:“类型验证器中的getPasswordAuthentication()方法不可见”@AdrianT我希望我的编辑澄清了出现错误的原因是,再次感谢…它不是我所认为的接口的实现,而是另一个类的扩展。
SMTPAuthenticator authenticator = new SMTPAuthenticator();