title: Mail class throw ClassFormatError: for javax/mail/Authenticator
body: <p><b>Welcome,</b><br>
  I wrote simple test class form example <a href="http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/" rel="nofollow">a JavaMail API – Sending email - by  mkyong</a> works perfectly but if i make separate class without main method like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>import com.sedzisz.papersoccer.PaperSoccer;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Properties;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.mail.Message;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.internet.MimeMessage;
  
  public class SendMail {
  
  private static final Logger LOG = Logger.getLogger(SendMail.class.getName());
  
  private final Level logLevel;
  
  private String propertyFileName;
  
  private final Properties props;
  
  public String getMessageText() {
      return messageText;
  }
  
  public void setMessageText(String messageText) {
      this.messageText = messageText;
  }
  
  public String getRecipientEmailAddress() {
      return recipientEmailAddress;
  }
  
  public void setRecipientEmailAddress(String recipientEmailAddress) {
      this.recipientEmailAddress = recipientEmailAddress;
  }
  
  private String messageText;
  
  private String recipientEmailAddress;
  
  public SendMail(String propertyFileName) {
  
      this.logLevel = Level.INFO;
  
      propertyFileName = "gmail.properties";
      if (propertyFileName != null) {
          this.propertyFileName = propertyFileName;
      }
      LOG.log(Level.INFO, "Set property file name [{0}]", propertyFileName);
  
      LOG.setLevel(logLevel);
      props = new Properties();
      loadProperties(propertyFileName);
  }
  
  public boolean sendMessageTo() throws FileNotFoundException {
  
      if (props.isEmpty()) {
          throw new FileNotFoundException("Can't load propertis for mail connections");
      } else {
  
          Session session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                      @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);
                      }
                  });
  
   //  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) {
   //                LOG.info("Ble ble ble coś tam żle!!!");
   //            } catch (MessagingException me) {
   //                LOG.info("Jak wyżej tylko chodzi o maila");
   //            } finally {
              return succes;
    //            }
      }
  }
  
  private void loadProperties(String fileName) {
  
      ClassLoader loader = PaperSoccer.class.getClassLoader();
      System.setProperty("file.encoding", "UTF-8");
      LOG.log(Level.INFO, "File encoding [{0}]", System.getProperty("file.encoding"));
      LOG.log(Level.INFO, "File name [{0}]", fileName);
  
      InputStream is;
      try {
          loader.getResource(fileName);
  
          LOG.log(Level.INFO, "Path to file [{0}]", loader.getResource(fileName));
  
          is = loader.getResourceAsStream(fileName);
          props.load(is);
  
          is.close();
  
          if (logLevel == Level.WARNING) {
              for (String key : props.stringPropertyNames()) {
                  String value = props.getProperty(key);
                  LOG.log(Level.WARNING, "{0}= [{1}]", new Object[]{key, value});
              }
          }
  
          if (props.size() &gt; 0) {
              LOG.log(logLevel, "Properties have [{0}] parameters", props.size());
          }
  
      } catch (FileNotFoundException e) {
          LOG.log(Level.INFO, "File [{0}] not found", fileName);
      } catch (IOException e) {
          LOG.log(Level.INFO, "Cannt read file [{0}]", fileName);
      }
  
  }
  
  }
  </code></pre>
  
  <p>Maven dependency:</p>
  
  <pre><code>&lt;!-- Java Mail --&gt;
          &lt;!-- 
          &lt;dependency&gt;
              &lt;groupId&gt;javax.activation&lt;/groupId&gt;
              &lt;artifactId&gt;activation&lt;/artifactId&gt;
              &lt;version&gt;1.1.1&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
          &lt;/dependency&gt;
          --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.mail&lt;/groupId&gt;
              &lt;artifactId&gt;mail&lt;/artifactId&gt;
              &lt;version&gt;1.4.7&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
          &lt;/dependency&gt;
   &lt;!-- Java Mail --&gt;
  </code></pre>
  
  <p>I got this error:</p>
  
  <pre><code>    java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Authenticator
      at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at com.sedzisz.papersoccer.activationMail.SendMailTest.hello(SendMailTest.java:44)
  </code></pre>
  
  <p>What is wrong with my code?</p>
  
  <p>I tray do something like this:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>But i get this same error.</p>
  
  <p>I tray do this:</p>
  
  <pre><code>    public boolean sendMessageTo(){
              Authenticator auth = new Authenticator() {
                  @Override
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication("username", "password");
                  }
              };
  
              Session session = Session.getInstance(props, auth);
  
      }
  </code></pre>
  
  <p>no results, simple unit test like:</p>
  
  <pre><code>    @Test
      public void testSendMail(){
              SendMail sm = new SendMail();
      }
  </code></pre>
  
  <p>throw always exception</p>