Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
邮件类抛出ClassFormatError:对于javax/Mail/Authenticator_Java_Jakarta Mail - Fatal编程技术网

邮件类抛出ClassFormatError:对于javax/Mail/Authenticator

邮件类抛出ClassFormatError:对于javax/Mail/Authenticator,java,jakarta-mail,Java,Jakarta Mail,欢迎, 我写了一个简单的测试类表单示例,效果很好,但是如果我在没有main方法的情况下创建单独的类,比如: private class MAuthenticator extends javax.mail.Authenticator { public MAuthenticator() { super(); } @Override protected PasswordAuthentication getPasswordAuthentication(

欢迎,
我写了一个简单的测试类表单示例,效果很好,但是如果我在没有main方法的情况下创建单独的类,比如:

private class MAuthenticator extends javax.mail.Authenticator {

    public MAuthenticator() {
        super();
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        String userName = props.getProperty("username");
        String password = props.getProperty("password");
        LOG.log(logLevel, "User name [{0}], password [{1}]", new Object[]{userName, password});
        return new PasswordAuthentication(userName, password);
    }
}
import com.sedzisz.papersoccer.papersoccer;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.Properties;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.mail.Message;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.internet.mimessage;
公共类SendMail{
私有静态最终记录器LOG=Logger.getLogger(SendMail.class.getName());
私人最终级别日志级别;
私有字符串propertyFileName;
私人最终财产道具;
公共字符串getMessageText(){
返回消息文本;
}
public void setMessageText(字符串messageText){
this.messageText=messageText;
}
公共字符串getRecipientEmailAddress(){
返回收件人电子邮件地址;
}
public void setRecipientEmailAddress(字符串recipientEmailAddress){
this.recipientEmailAddress=recipientEmailAddress;
}
私有字符串消息文本;
私有字符串recipientEmailAddress;
public SendMail(字符串属性FileName){
this.logLevel=Level.INFO;
propertyFileName=“gmail.properties”;
if(propertyFileName!=null){
this.propertyFileName=propertyFileName;
}
LOG.LOG(Level.INFO,“设置属性文件名[{0}]”,propertyFileName);
LOG.setLevel(logLevel);
props=新属性();
loadProperties(propertyFileName);
}
公共布尔sendMessageTo()引发FileNotFoundException{
if(props.isEmpty()){
抛出新的FileNotFoundException(“无法加载邮件连接的属性”);
}否则{
会话=会话.getInstance(props,
新的javax.mail.Authenticator(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
字符串userName=props.getProperty(“用户名”);
字符串密码=props.getProperty(“密码”);
LOG.LOG(logLevel,“用户名[{0}],密码[{1}]”,新对象[]{userName,password});
返回新密码身份验证(用户名、密码);
}
});
//Message Message=新的mimessage(会话);
//
布尔成功=假;
//试一试{
//message.setFrom(新的InternetAddress(props.getProperty(“用户名”));
//message.setRecipients(message.RecipientType.TO,
//解析(recipientEmailAddress));
//message.setSubject(“测试邮件”);
//message.setText(“Weclome companion!,\n”+messageText
//+“\n您不会使用redtube.com的前置代码;)”;
//传输。发送(消息);
//成功=正确;
//}catch(AddressException ax){
//LOG.info(“blecośtamżle!!!”;
//}catch(MessaginException me){
//LOG.info(“Jak wyżej tylko chodzi o maila”);
//}最后{
回归成功;
//            }
}
}
私有void加载属性(字符串文件名){
ClassLoader=PaperSoccer.class.getClassLoader();
System.setProperty(“file.encoding”、“UTF-8”);
LOG.LOG(Level.INFO,“文件编码[{0}]”,System.getProperty(“File.encoding”);
LOG.LOG(Level.INFO,“文件名[{0}]”,文件名);
输入流为;
试一试{
getResource(文件名);
LOG.LOG(Level.INFO,“文件[{0}]的路径”,loader.getResource(文件名));
is=loader.getResourceAsStream(文件名);
道具载荷(is);
is.close();
如果(logLevel==Level.WARNING){
for(字符串键:props.stringPropertyNames()){
字符串值=props.getProperty(键);
LOG.LOG(Level.WARNING,“{0}=[{1}]”,新对象[]{key,value});
}
}
如果(props.size()>0){
LOG.LOG(logLevel,“属性有[{0}]个参数”,props.size());
}
}catch(filenotfounde异常){
LOG.LOG(Level.INFO,“未找到文件[{0}”,文件名);
}捕获(IOE异常){
LOG.LOG(Level.INFO,“无法读取文件[{0}]”,文件名);
}
}
}
Maven依赖项:

<!-- Java Mail -->
        <!-- 
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
            <type>jar</type>
        </dependency>
        -->

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
            <type>jar</type>
        </dependency>
 <!-- Java Mail -->
我的代码有什么问题

我可以这样做:

private class MAuthenticator extends javax.mail.Authenticator {

    public MAuthenticator() {
        super();
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        String userName = props.getProperty("username");
        String password = props.getProperty("password");
        LOG.log(logLevel, "User name [{0}], password [{1}]", new Object[]{userName, password});
        return new PasswordAuthentication(userName, password);
    }
}
但我也有同样的错误

我可以这样做:

    public boolean sendMessageTo(){
            Authenticator auth = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username", "password");
                }
            };

            Session session = Session.getInstance(props, auth);

    }
无结果,简单的单元测试,如:

    @Test
    public void testSendMail(){
            SendMail sm = new SendMail();
    }

抛出总是异常

我的错我没有给你们关于项目的所有信息我的pom有点胖,我依赖JavaEEWebAPI

现在我有另一个问题

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sedzisz.papersoccer.activationMail.SendMailTest
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail <init>
INFO: Set property file name [gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: File encoding [UTF-8]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: File name [gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: Path to file [file:/D:/source/java/NetBeansProjects/PaperSoccer/target/classes/gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail$1 getPasswordAuthentication
INFO: Create authenticator class and return PasswordAuthenticator
javax.mail.AuthenticationFailedException
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.042 sec <<< FAILURE! - in com.sedzisz.papersoccer.activationMail.SendMailTest
hello(com.sedzisz.papersoccer.activationMail.SendMailTest)  Time elapsed: 1.996 sec  <<< FAILURE!

第二个参数应该是密码而不是用户名!一切正常!再次感谢您抽出时间;)

构造函数应该用访问说明符声明。i、 e.
public-sendmail(String-propertyFileName)
True应该是,但我仍然发现了相同的错误。您是否试图实例化一个抽象类?@Newbie请查看。您不应该编辑您的帖子,将
[SOLVED]
包含在标题中。很抱歉,这种情况不会再次发生。
    <!-- Java Mail -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
        <type>jar</type>
    </dependency>
    <!-- Java Mail -->
public boolean sendMessageTo() {

    Authenticator auth = new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            LOG.info("Create authenticator class and return PasswordAuthenticator");
            return new PasswordAuthentication(props.getProperty("username"), props.getProperty("username"));
        }
    };

    Session session = Session.getInstance(props, auth);

    Message message = new MimeMessage(session);

    boolean succes = false;

    try {
        message.setFrom(new InternetAddress(props.getProperty("username")));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipientEmailAddress));
        message.setSubject("Testing mail");
        message.setText("Weclome companion!,\n" + messageText
                + "\n You wonn premmum code for redtube.com ;)");
        Transport.send(message);
        succes = true;
    } catch (AddressException ax) {
        System.err.println(ax);
    } catch (MessagingException me) {
        System.err.println(me);
    } finally {
        return succes;
    }
}
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sedzisz.papersoccer.activationMail.SendMailTest
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail <init>
INFO: Set property file name [gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: File encoding [UTF-8]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: File name [gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail loadProperties
INFO: Path to file [file:/D:/source/java/NetBeansProjects/PaperSoccer/target/classes/gmail.properties]
gru 27, 2013 11:42:22 AM com.sedzisz.papersoccer.activationMail.SendMail$1 getPasswordAuthentication
INFO: Create authenticator class and return PasswordAuthenticator
javax.mail.AuthenticationFailedException
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.042 sec <<< FAILURE! - in com.sedzisz.papersoccer.activationMail.SendMailTest
hello(com.sedzisz.papersoccer.activationMail.SendMailTest)  Time elapsed: 1.996 sec  <<< FAILURE!
return new PasswordAuthentication(props.getProperty("username"), props.getProperty("username"));